back to course main page

L4 – Control Structures

In this part of the Python course we will show you some examples of the typical control structures used in Python.  These are just like the control structures you will find in most programming languages

IF statement

In this section several different versions of an IF statement are presented, starting with a single IF statement up to having multiple ELIF (else if) statements. It is important to have a blank line at the end of your IF statement to indicate end.

# simple IF statement
temp = 20
if temp < 10:
   print "It is cold!"
# IF...ELSE statement
temp = 20
if temp < 10:
   print "It is cold!"
else:
   print "It feels good!"
# if...elif...else
temp = 20
if temp < 10:
   print "It is cold!"
elif temp > 30:
   print "It is hot!"
else:
   print "It feels good!"

FOR statement

for i in [0,1,2,3,4]:
   print(i)

WHILE statement
i = 0
while (i < 5):
   i = i + 1
   print(i)

FOR statement

for item in [1,3,6,2,5]:
   print(item)

 

back to course main page