Introduction To Python: If Statements: NCSS Workshop

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

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 >>> x = 3
2 >>> if x == 3:
3 ... print 'x is equal to 3'
4 ...
5 x is equal to 3
6 >>>

• 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 >>> x = 10
2 >>> if x > 3:
3 ... print 'x is bigger than 3'
4 ... if x > 6:
5 ... print 'x is also bigger than 6'
6 ...
7 x is bigger than 3
8 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 >>> x = 4
2 >>> if x == 3:
3 ... print 'x is equal to 3'
4 ... else:
5 ... print 'x is not equal to 3'
6 x is not equal to 3
7 >>>

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 >>> if x < 3:
2 ... print "x is less than three"
3 ... else:
4 ... if x == 3:
5 ... print "x is equal to three"
6 ... else:
7 ... print "x is greater than three"
8 ...
9 >>>
• 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 >>> if x < 3:
2 ... print "x is less than three"
3 ... elif x == 3:
4 ... print "x is equal to three"
5 ... else:
6 ... print "x is greater than three"
7 ...
8 >>>
• 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 >>> if name == "David" or name == "Paul":
2 ... 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 >>> i = 0
2 >>> while i < 3:
3 ... print i, 'is less than 3'
4 ... i += 1
5 ...
6 0 is less than 3
7 1 is less than 3
8 2 is less than 3
9 >>>

• 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 >>> i = 0
2 >>> while i < 3:
3 ... print i, 'is less than 3'
4 ... i += 1
5 ...
6 0 is less than 3
7 1 is less than 3
8 2 is less than 3
9 >>>

• 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 >>> i = 0
2 >>> while i < 3:
3 ... print i, 'is less than 3'
4 ... i += 1
5 ...
6 0 is less than 3
7 1 is less than 3
8 2 is less than 3
9 >>>

• 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 >>> i = 0
2 >>> while i < 3:
3 ... print i, 'is less than 3'
4 ...
5 0 is less than 3
6 0 is less than 3
7 ...
• 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 >>> i = 1
2 >>> while i < 12:
3 ... print i, 'times 5 =', i*5
4 ... i += 1
5 ...
6 1 times 5 = 5
7 2 times 5 = 10
8 ...
9 >>>

• 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 >>> number = int(raw_input('Enter a number? '))


2 >>> while number != 73:
3 ... print 'Wrong! try again.'
4 ... number = int(raw_input('Enter a number? '))
5 ...
6 >>>
• 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!

NCSS Workshop Introductory Python Girls’ Programming Network

You might also like