1 of 44

Schulich Ignite

Session 4

1

2 of 44

Session Overview

  • Review of if/elif/else statements
  • Review of Logical operators
  • Introduction to Lists
  • Introduction to Loops

2

3 of 44

Discord

Join us on Discord!

  • Dedicated channel for your group
  • Ask your mentor questions outside of class time
  • Show off any cool projects!

4 of 44

Discord - Sign Up For Private Group Channel

  1. Check your email for your day of week and group number
  2. Then on Discord, in the #bot-commands channel, type in:

!group set <day>-<group number>

NOTE: bot commands must be in lowercase

Example:

!group set monday-3

5 of 44

DECODR: Making gene repairing safer

CRISPR allows scientists to edit genes at precise locations, which can be used to permanently living cells and organisms. One potential use is to treat genetic diseases in humans.

However, gene editing can lead to unintended changes to the DNA. Researchers may use softwares like DECODR, an analysis software to quantify the changes from gene editing, to assess the potential risks.

5

6 of 44

DECODR: Software Screenshot

6

7 of 44

Review

7

8 of 44

If Statements

Use if statements to run code based on some condition!

8

9 of 44

Example

if your house is cold, then your thermostat turns on your furnace

if house_temp < 20:� furnace_on = True� �

if house_temp > 25:� furnace_on = False

9

Don’t forget the colons at the end!

Indented by one “tab”

10 of 44

if … elif … else Statement

if mouseY < 500: # Top half of the screen fill(255, 0, 0)�� elif mouseX < 500: # Lower left of screen� fill(0, 255, 0)�� else: # Lower right of screen

fill(0, 0, 255)

ellipse(mouseX, mouseY, 150, 150)� �

10

11 of 44

Logical Operators Review

and - True only if all the boolean statements are true

  • 5 > 3 and 6 > 3 # True
  • 5 > 3 and 6 < 3 # False

or - True if any of the boolean statements are true

  • 5 > 3 or 6 < 3 # True
  • 5 < 3 or 6 < 3 # False

not - True only if the boolean statement is false

  • not 6 < 3 # True
  • not 6 < 3 # False

11

12 of 44

Logical Expressions (not + And) Example

12

top = mouseY < 200

bottom = mouseY > 200

left = mouseX < 200

# Colours

if top and left:

fill(255, 0, 0)

else:

fill(0, 0, 255)

# Shapes

if bottom and not left:

rect(mouseX, mouseY, 50, 50)

else:

ellipse(mouseX, mouseY, 50, 50)

13 of 44

Lists

13

14 of 44

What’s A List?

A list is a collection of values.

The values should be related to the name of the list.

14

Students

"Henry"

"Alice"

"Jack"

"Alex"

"Carrie"

15 of 44

How Do I Make A List?

  • [] tells the computer that we want to make a list
  • Lists can hold any type of data: numbers, strings, and booleans!

# Create a list of numbers random_numbers = [101, -2, 303, 44]

# Create a list of student names� students = ["Henry", "Alice", "Jack", "Alex"]

15

16 of 44

Getting a value Inside of A List

To get a value from a list, use its index!

my_numbers = [101, -2, 303, 44]�To get the third value in my_numbers, use index 2

print(my_numbers[2])

16

Index

Value

0

101

1

-2

2

303

3

44

Reminder: Lists start at index 0!

17 of 44

Using Lists

We can use the values of a list similarly to how we used variables:

numbers = [75, 100, 50, 40]

ellipse(numbers[0], numbers[1], numbers[2],numbers[3])

Note: Using ellipse(numbers) will not work! We need to access each value individually!

17

18 of 44

Modifying Lists

We can change the values inside of a list:

# Let’s create a list

numbers = [2, 5, 6, 8]

# Change index 0 value to 3

numbers[0] = 3

18

Index

0

1

2

3

Value

2

5

6

8

Index

0

1

2

3

Value

3

5

6

8

19 of 44

Quick Question #1

What will be the diameter of the ellipse() drawn in setup()?

Hint: Remember the index at which lists begin!

19

10

x = [20, 40, 60, 80]�x[1] = x[1] + 10�x[3] = x[1] * 2

def setup():

size(300, 300)�ellipse(15, 150, x[3], x[3])

TopHat join codes: | 537247(Thursday) | 239170(Saturday)

20 of 44

Making Our List Bigger

We can also add items to an existing list.

shopping_list = ["eggs", "ham", "cheese", "onions"]

20

Index

0

1

2

3

Value

"eggs"

"ham"

"cheese"

"onions"

shopping_list

21 of 44

Making Our List Bigger

Use .append() to add items to the end of the list:��

shopping_list.append("milk")

shopping_list.append("bread")

21

Index

0

1

2

3

4

5

Value

"eggs"

"ham"

"cheese"

"onions"

"milk"

"bread"

shopping_list

22 of 44

Removing Items From List

Remove specific items from the list with .remove():��

shopping_list.remove("ham")

shopping_list.remove("onions")

22

Index

0

1

2

3

Value

"eggs"

"cheese"

"milk"

"bread"

shopping_list

23 of 44

Getting The Length Of Our Lists

We can use len() to get the length of the list.�

students = ["Henry", "Alice", "Jack", "Alex"]��amount = len(students)print(amount) # This will print out: 4

23

24 of 44

24

Common Lists Methods

Method

Description

append(item)

Adds an item to the end of the list.

remove(item)

Removes the first item whose value is equal to item.

len()

Returns the length of a list.

index(item)

Returns the index of the first item whose value is equal to item.

sort()

Sort the items of the list in place (from the lowest value to the highest value).

reverse()

Reverse the elements of the list in place.

25 of 44

Exercise #1 - Lively Bar Graph

Use a list to draw 4 rectangles that all grow with your mouseX.

Hint: The list should store the values that are different for each bar.

25

26 of 44

Exercise #1: Lively Bar Graph - Prompting Questions

Which argument in initializing rect(x, y, width, length) should be included in your list?

  • x is the same for all bars
  • Width is determined by the mouse coordinate
  • Height is the same for all bars

26

27 of 44

Loops

(The basics)

27

28 of 44

What Are Loops?

Loops repeat code a certain number of times.

Start of Loop

# Code…

End of Loop

Loops are great for working with lists.

Today we’ll learn the most common type of loops: for loops

28

29 of 44

Loop Analogy

A dentist checking teeth is like a loop!

  1. Dentist begins at the first tooth
  2. Checks to see if tooth is healthy
  3. Moves on to the next tooth
  4. Repeats this loop until all teeth have been checked!

29

30 of 44

Combining Loops With Lists

We can go through each item in the list, and print each value using a for loop:

shopping_list = ["eggs", "ham", "cheese", "onions"]

for item in shopping_list:

# Code here repeated for all items in shopping_list

print(item)

item == "eggs" in loop 1

item == "ham" in loop 2

item == "cheese" in loop 3

item == "onions" in loop 4

30

31 of 44

Quick Question #2

31

10

What will we print with this piece of code?

numbers = [2, 3, 5]

x = 0

for number in numbers:

x += number

print(x)

TopHat join codes: | 537247(Thursday) | 239170(Saturday)

32 of 44

When To Use Loops

y_list = [50, 135, 275, 400]

Let’s look at our code from Exercise 1 again...

That’s long. And repetitive!

  • What if we had 20 values?

#1

#2

#3

#4

32

rect(0, y_list[0], mouseX, 30)

rect(0, y_list[1], mouseX, 30)

rect(0, y_list[2], mouseX, 30)

rect(0, y_list[3], mouseX, 30)

33 of 44

When To Use Loops

y_list = [50, 135, 275, 400]

We can replace our repeating�code with a loop.

#1

#2

#3

#4

33

rect(0, y_list[0], mouseX, 30)

rect(0, y_list[1], mouseX, 30)

rect(0, y_list[2], mouseX, 30)

rect(0, y_list[3], mouseX, 30)

y_list = [50, 135, 275, 400]

for y in y_list:

rect(0, y, mouseX, 30)

34 of 44

Changing Repetition Into Loops

y_list = [50, 135, 275, 400]

34

n = 3

rect(0, y, mouseX, 30)

for y in y_list:

y is 50

y is 135

y is 275

# Done looping

y is 400

35 of 44

Exercise #2 - Falling Circles

Use a list and a for loop to draw 5 circles falling down the screen at the same time.

Hint: Think about how a loop helps us draw the circles so much faster!

35

36 of 44

36

Exercise #2: Falling Circles - Prompting Questions

  • For each circle, the center (the x position) is different, think about a way to store the multiple x positions.
    • Then repeat the process for the number of circles using a for loop.

X = 30

X = 270

37 of 44

37

Mixing For Loops And If Statements

You can put if statements and other logic blocks inside for loops!

for x in x_pos:� if x > 150:� fill()� else:� fill("yellow")� ellipse(x, y, 30, 30)

38 of 44

38

for x in x_pos: .if x > 150:� fill(255, 0, 0)� else:� fill(255, 255, 0)

ellipse(x, y, 30, 30)

x_pos = [40, 80, 120, 160, 200]

Mixing For Loops And If Statements

39 of 44

Exercises

39

50

1

2

40 of 44

Exercises - Bug Busters

40

50

1

2

41 of 44

Exercises - Pythonic Lava

41

50

3

4

42 of 44

Exercises - just for fun

42

3

4

43 of 44

Exercises - just for fun

43

44 of 44

Submitting Exercises

Submit the link to your code to the submission form:

https://docs.google.com/forms/d/1H59Zvw9gNdKFATBSTlPXxLumVWx5eSylphy8pIvEep8/edit

Submissions are due a week after your session

Full rules and details can be found here