Jump to content

Introduction to Python Programming/Python Programming - Control Structures

From Wikibooks, open books for an open world

4. Control Structures

[edit | edit source]

A set of actions define the flow of events as decided by the flow chart. Within Python programming, none “None” values return True while variables with “None” values return False.

4.1. If statements

[edit | edit source]

The simplest control if statements return the value True when a condition is met. There is the simple one line if condition, followed by the if-else, and continued by the if-elif statements.

   >>> var = 100
   >>> if var==100:

print "yay! the variable is truly 100!!!"

   yay! the variable is truly 100!!!
   >>>
   >>> 
   >>> if (var==100): print "value is 100, its looks great!"
   value is 100, its looks great!
   >>>
   var = 100
   if var == 100:
       print "yay! the variable is truly 100"
   else:
       print "Nope! Better luck next time!!!!"
   >>>var=10
   >>> if var == 100:
           print "yay! the variable is truly 100"
       else:
           print "Nope! Better luck next time!!!!"
   Nope! Better luck next time!!!!

4.2. For Loops

[edit | edit source]

The “for” loops in Python executes a certain block of code for a known number of iterations. The specialty of “for” loop in Python is that the block of code can be iterated for the number of items existing within a list, dictionary, string variable, over a particular range of numbers in counted number of steps, and for the number of items existing in a tuple.

   >>> a=(10,20,30,40,50)
   >>> for b in a:
   print "square of " + str(b) + " is " +str(b*b)
   square of 10 is 100
   square of 20 is 400
   square of 30 is 900
   square of 40 is 1600
   square of 50 is 2500
   >>> c=["new", "string", "in", "python"]
   >>> for d in c:
           print "the iteration of string is %s" %d
   the iteration of string is new
   the iteration of string is string
   the iteration of string is in
   the iteration of string is python
   >>>
   >>> for i in range(10):

print "the value of "+str(i)+" is "+str(i)

   the value of 0 is 0
   the value of 1 is 1
   the value of 2 is 2
   the value of 3 is 3
   the value of 4 is 4
   the value of 5 is 5
   the value of 6 is 6
   the value of 7 is 7
   the value of 8 is 8
   the value of 9 is 9
   >>> a={'a':10,'b':20,'c':30}
   >>> for k in a:
       	print "the key is %s and the value is %d" %(k,a[k])
   the key is a and the value is 10
   the key is c and the value is 30
   the key is b and the value is 20

4.3. While Loop

[edit | edit source]

The while loop is executed till a conditional statement returns true. The conditional statement is evaluated every time before a block of code is executed, and the execution stops the moment the conditional statement returns false.

   >>> count = 0
   >>> while (count<9):
       print "the count is at iteration %d" %count
       count+=1
   the count is at iteration 0
   the count is at iteration 1
   the count is at iteration 2
   the count is at iteration 3
   the count is at iteration 4
   the count is at iteration 5
   the count is at iteration 6
   the count is at iteration 7
   the count is at iteration 8