week 2: flow control welcome to · statement & multiline python statement - a single line of...
TRANSCRIPT
![Page 1: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/1.jpg)
Welcome toWeek 2: Flow Control
![Page 2: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/2.jpg)
Today’s Lesson
▸ Statements▸ Comments▸ Operators▸ If Statements▸ Examples
2
![Page 3: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/3.jpg)
Statement & Multiline
▸ Python Statement - A single line of python code
▸ Can be single line, or multilined
▸ Line continuation character: \
3
![Page 4: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/4.jpg)
Statement Examples
x = 1 + 2 + \
3 + 4 \
+ 5
print(x)
4
![Page 5: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/5.jpg)
Statement & Single Line
▸ Lines can be combined (not recommended for most statements)
▸ Line combination character: ;
5
![Page 6: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/6.jpg)
Statement Examples
x = 1; y = 2; z = 3
print(x, y, z)
6
![Page 7: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/7.jpg)
Comments
▸ Helps quickly understand code
▸ English language, doesn’t change program
▸ Commenting Symbol: #
7
![Page 8: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/8.jpg)
Pre-Knowledge
▸ Boolean: Variable with True or False value
▸ Operators basics (and, or, not)
8
![Page 9: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/9.jpg)
Logical Operators (in detail)
▸ And▹ It is raining and I have an umbrella▹ True and False▹ True and True▹ False and False
9
![Page 10: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/10.jpg)
Logical Operators (in detail)
▸ Or▹ It is raining or I have an umbrella▹ True or False▹ True or True▹ False or False
10
![Page 11: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/11.jpg)
Logical Operators (in detail)
▸ Not▹ Typically put first▹ Not (raining) ▹ Not (I have an umbrella)▹ Not True▹ Not False
11
![Page 12: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/12.jpg)
Logical Operators List(Best to Memorize)
12
![Page 13: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/13.jpg)
Relational Operators▸ Operations performed on two numbers
▸ True/False Value 13
![Page 14: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/14.jpg)
Common Mistake!
▸ = is not equal to ==▹ = is for assignment▹ == is for comparison
14
a = 2
if a = 1:
print (a)
![Page 15: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/15.jpg)
Examples of Relational Operators1. 10 > 4
a. True2. 2 >= 2
a. True3. 4 == 4.2
a. False4. 5 != 2
a. True
15
![Page 16: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/16.jpg)
If Statements
▸ If statement: only executing a block of code if a condition is true
▸ Condition - operators!
▸ Indentation is important!
16
if (something happens):
(do this)
![Page 17: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/17.jpg)
Indentation
x = 1
if (x > 0):
print (x)
print(“Hi”)
17
x = 1
if (x > 0):
print (x)
print(“Hi”)
vs
![Page 18: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/18.jpg)
If/Else Statement
▸ Else: only execute code when condition is NOT true.
18
if (something happens):
(do this)
else:
(do this)
![Page 19: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/19.jpg)
Nested If Statements
▸ More than one condition
▸ Leads to different blocks of code
19
if (something happens):
(do this)
elif (something else happens):
(do this)
else:
(do this)
![Page 20: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/20.jpg)
Nested If Statementsif (something happens):
(do this)
else:
if (something else happens):
(do this)
else:
(do this)
20
if (something happens):
(do this)
elif (something else happens):
(do this)
else:
(do this)
Is the Same As
![Page 21: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/21.jpg)
Examples of If Statements
![Page 22: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/22.jpg)
x = 3
if x > 3:
print(“x is greater than 3”)
elif x == 3:
print(“x is equal to 3”)
else:
print(“x is less than 3”)
![Page 23: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/23.jpg)
x = 4
if x > 3:
print(“x is greater than 3”)
elif x == 3:
print(“x is equal to 3”)
else:
print(“x is less than 3”)
![Page 24: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/24.jpg)
x = 1
if x > 3:
print(“x is greater than 3”)
elif x == 3:
print(“x is equal to 3”)
else:
print(“x is less than 3”)
![Page 25: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/25.jpg)
Examples and Problems!Type Your Answers in the Chat
![Page 26: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/26.jpg)
Let’s try logical operators with if statements! What does this print?
x = True
y = False
if (x and y):
print (“Yes!”)
else:
print(“No!”)
What does this print?
![Page 27: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/27.jpg)
hiking = False
running = True
if hiking and running:
print (“So much exercise!”)
elif hiking or running:
print (“Just a bit…”)
else:
print (“No exercise!”)
What does this print?
![Page 28: Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python code Can be single line, or multilined Line continuation character: \ 3](https://reader030.vdocuments.net/reader030/viewer/2022041116/5f276b0267021e34332c3b58/html5/thumbnails/28.jpg)
today = “Tuesday”
raining = True
if today == “Monday”:
print (“Workout”)
elif today == “Tuesday”:
if raining == True:
print (“Ping Pong”)
else:
print (“Tennis”)
Else:
print (“Homework”)
What does this print?