Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python...

28
Welcome to Week 2: Flow Control

Transcript of Week 2: Flow Control Welcome to · Statement & Multiline Python Statement - A single line of python...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?