Sunday, 13 May 2012

Program Flow Control

Output from Control_example.py
Code Listing:
#  Control_example.py
print "if | elif | else statements code examples"  # Selecting actions
name = 'Bamm-Bamm'
if name == 'Barney':              
    print "Flintstones' neighbours"
elif name != 'Betty':
    print name
else:
    son = 'Bamm-Bamm'
   
print "while statement code example"
z = 3
while z>0:                          # General Loops
    print z
    z = z-1
   
print "for code example"            # Sequence iteration
for x in ['this', 'is', 'a','sequence']:  
    print x
 
print "range code example"
for y in range(5):                  # Python count 0 .. 4
    print y

print "Breaking out of a loop: code example"
a_list = ['Jill', 1, "Bob", 'Brown', 'Jones', 333, 333, 1234.5]
for name in a_list:
    if name == 'Jones':
         break
    print name

Online Python Tutor .. Data Illustrated


No comments:

Post a Comment