Professional Documents
Culture Documents
Pygame 2
Pygame 2
NCSS Workshop
Control Structures
if statements
while loops
Practice!
Outline
Control Structures
if statements
while loops
Practice!
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
,)
,WLVUDLQLQJ
7UXH
)DOVH
3LFNXSXPEUHOOD
3XWRQDKDW
*RRXWVLGH
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
HOVH
3XWRQDKDW
*RRXWVLGH
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
>>> x = 3
>>> if x == 3:
...
print 'x is equal to 3'
...
x is equal to 3
>>>
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
>>> 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
Control Structures
if statements
while loops
Practice!
>>> 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
Control Structures
if statements
while loops
Practice!
>>> 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"
...
>>>
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
>>> 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"
...
>>>
Introductory Python
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
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
11
>>> 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
>>>
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
11
>>> 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
>>>
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
12
>>> 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
>>>
Introductory Python
Control Structures
if statements
while loops
Practice!
13
>>> i = 0
>>> while i < 3:
...
print i, 'is less than 3'
...
0 is less than 3
0 is less than 3
...
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
14
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
15
>>> i = 1
>>> while i < 12:
...
print i, 'times 5 =', i*5
...
i += 1
...
1 times 5 = 5
2 times 5 = 10
...
>>>
Introductory Python
Control Structures
if statements
while loops
Practice!
16
NCSS Workshop
Introductory Python
Control Structures
if statements
while loops
Practice!
17
NCSS Workshop
Introductory Python