Introduction to Python: if statements NCSS Workshop
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
2
Outline
1
Control Structures
2
if statements
3
while loops
4
Practice!
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
3
Control structures change program execution
,) ,WLVUDLQLQJ 7UXH
)DOVH
3LFNXSXPEUHOOD
3XWRQDKDW
*RRXWVLGH
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
4
Flow charts work in code too LILVBUDLQLQJ 3LFNXSXPEUHOOD
HOVH 3XWRQDKDW
*RRXWVLGH
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
5
A Python if statement in action 1 2 3 4 5 6
>>> x = 3 >>> if x == 3: ... print 'x is equal to 3' ... x is equal to 3 >>>
• if keyword and colon • conditional expression x == 3
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
6
A block can contain one or more statements 1 2 3 4 5 6 7 8
>>> x = 10 >>> if x > 3: ... print 'x is bigger than 3' ... if x > 6: ... print 'x is also bigger than 6' ... x is bigger than 3 x is also bigger than 6
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
7
The else clause is executed on False • if statements can have an else clause: 1 2 3 4 5 6 7
>>> x = 4 >>> if x == 3: ... print 'x is equal to 3' ... else: ... print 'x is not equal to 3' x is not equal to 3 >>>
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
8
Deeply nested ifs quickly becomes messy • If we want to consider more alternatives we must nest ifs: 1 2 3 4 5 6 7 8 9
>>> if x < 3: ... print "x is less than three" ... else: ... if x == 3: ... print "x is equal to three" ... else: ... print "x is greater than three" ... >>>
• This can get ugly once there are many alternatives
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
9
elif clauses avoid nested ifs • We can write the previous example more elegantly as: 1 2 3 4 5 6 7 8
>>> if x < 3: ... print "x is less than three" ... elif x == 3: ... print "x is equal to three" ... else: ... print "x is greater than three" ... >>>
• Each conditional expression is evaluated until one is True • The corresponding block is then executed • If none of the if or elif conditionals are True the else block is run NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
10
and and or • There are two additional keywords that are quite useful, and and or. 1 2
>>> if name == "David" or name == "Paul": ... print "That's my brother's name!"
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
11
A Python while loop in action 1 2 3 4 5 6 7 8 9
>>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>>
• A while loop starts with the keyword while
Remember, all Python control structures start with a keyword • Next is the conditional expression i < 3 and then a colon
Watch out for missing the colon!
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
11
A Python while loop in action 1 2 3 4 5 6 7 8 9
>>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>>
• A conditional expression is either evaluates to True or False • Here the comparison operator < tests if i is less than 3
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
12
A while loop simply a repeating if statement 1 2 3 4 5 6 7 8 9
>>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>>
• If the condition is True, run the loop body again • The body is indented just like the body in an if statement • The i += 1 statement is the same as i = i + 1 • It adds one to the value in i each time it is run NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
13
The body must change the condition! • If not, we’ll never ever stop looping 1 2 3 4 5 6 7
>>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... 0 is less than 3 0 is less than 3 ...
• We’ve removed the i += 1, so the condition doesn’t change • This is called an infinite loop • You can interrupt an infinite loop by pressing Ctrl-C
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
14
General pattern for making while loops • Your while loops will (usually) do the following: 1 Setup the loop variable(s) (the initialisation) 2 Test whether to continue running loop (the condition) 3 Run the body if the condition is true (the body) 4 Update the loop variable (the update) • You should check each time to make sure they’re all there!
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
15
We’ve seen looping over a list of numbers 1 2 3 4 5 6 7 8 9
>>> i = 1 >>> while i < 12: ... print i, 'times 5 =', i*5 ... i += 1 ... 1 times 5 = 5 2 times 5 = 10 ... >>>
• i is the loop variable and is initialised to 1 • i < 12 is the condition • print i, 'times = 5', i*5 is the body • i += 1 is the update step NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
16
We can also loop over user input • use raw_input inside the loop as the update: 1 2 3 4 5 6
>>> number = int(raw_input('Enter a number? ')) >>> while number != 73: ... print 'Wrong! try again.' ... number = int(raw_input('Enter a number? ')) ... >>>
• The raw_input lines are both initialiser and update
NCSS Workshop
Introductory Python
Girls’ Programming Network
Control Structures
if statements
while loops
Practice!
17
Now put this into practice! • You should now be able to write Python if/elif/else statements and use while loops! • Now you can do the NCSS Challenge checkpoint questions on
if statements and, when you get up to it later, the checkpoint on while loops!