Schulich Ignite
Session 4
1
Session Overview
2
Discord
Join us on Discord!
Discord - Sign Up For Private Group Channel
!group set <day>-<group number>
NOTE: bot commands must be in lowercase
Example:
!group set monday-3
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
DECODR: Software Screenshot
6
Review
7
If Statements
Use if statements to run code based on some condition!
8
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”
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
Logical Operators Review
and - True only if all the boolean statements are true
or - True if any of the boolean statements are true
not - True only if the boolean statement is false
11
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)
Lists
13
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" |
How Do I Make A List?
� # Create a list of numbers � random_numbers = [101, -2, 303, 44]
# Create a list of student names� students = ["Henry", "Alice", "Jack", "Alex"]
15
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!
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
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 |
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)
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
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
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
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
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. |
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
Exercise #1: Lively Bar Graph - Prompting Questions
Which argument in initializing rect(x, y, width, length) should be included in your list?
26
Loops
(The basics)
27
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
Loop Analogy
A dentist checking teeth is like a loop!
29
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
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)
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!
#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)
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)
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
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
Exercise #2: Falling Circles - Prompting Questions
X = 30
X = 270
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
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
Exercises
39
50
1
2
Exercises - Bug Busters
40
50
1
2
Exercises - Pythonic Lava
41
50
3
4
Exercises - just for fun
42
3
4
Exercises - just for fun
43
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