cond-loops.org
changeset 108 89668e9251cf
child 109 381da0ddc07b
equal deleted inserted replaced
107:dd6973b09679 108:89668e9251cf
       
     1 * Control statements
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* What are we going to do?
       
     5 ******* How are we going to do?
       
     6 ******* Arsenal Required
       
     7 ********* working knowledge of arrays
       
     8 
       
     9 *** Script
       
    10     Welcome. 
       
    11     
       
    12     In this tutorial we shall be covering conditional statements and
       
    13     control loops. We have used them already in some of our previous
       
    14     sessions with brief introduction. We shall be covering 'if-else' 
       
    15     statement, 'while' and 'for' loops.
       
    16     For understanding of conditional statement we will write a python
       
    17     script. Open Scite text editor by going to Applications->
       
    18     Programming->Scite:
       
    19     First we prompt user for entering a integer by using raw_input
       
    20     x = int(raw_input("Enter an integer: "))
       
    21     we convert the input string to 'int'
       
    22     if x < 0:
       
    23     We check if number is less then zero
       
    24         if condition is true we print 
       
    25         print 'Be positive!'	
       
    26 	note the indentation
       
    27     elif x == 0:
       
    28         This is else-if condition and corresponding message
       
    29 	end of previous indentation indicates ending of a block
       
    30         print 'Zero'
       
    31     elif x == 1:
       
    32         print 'Single'
       
    33     else:
       
    34         This is else block which is executed when all if, and else
       
    35 	if statements fails.
       
    36         print 'More'
       
    37     Save this script in home folder with name '.py'	
       
    38     To run this script inside IPython we first start interpreter and 
       
    39     type 
       
    40     %run '.py'
       
    41     It will prompt us to enter a integer and based on our input it 
       
    42     prints appropriate message. 
       
    43 
       
    44     Python supports only if-elif-else conditional constructs, 
       
    45     switch-case statements are not available/supported in Python.
       
    46     We can use binary operators like and/or/not to check for multiple
       
    47     conditions.
       
    48 
       
    49     Now lets look at loop constructs available. Python supports 'while'
       
    50     and 'for' statements. We will write a script to understand 'while'
       
    51     statement. In Scite click on 'new' file shortcut to open a new tab
       
    52     We shall write a script for printing all fabonacci numbers less then
       
    53     10. In this series Sum of previous two elements defines the next 
       
    54     element.
       
    55     
       
    56     First we initialize two variable to first and second number of 
       
    57     series
       
    58     a, b = 0, 1
       
    59     while b < 10:
       
    60         This block will be executed till this condition holds True
       
    61         print b,
       
    62 	Note ',' here for printing values in one continues line.
       
    63 	a, b = b, a+b
       
    64 	This is one powerful feature of Python, swapping and assigning
       
    65 	new values at the same time. After this statement a will have 
       
    66 	present 'b' value and b will have value of 'a+b'(phew this can be close)
       
    67 	
       
    68     Save this file as 'fabonacci.py' and lets run it from IPython 
       
    69     interpreter by
       
    70     %run fabonacci.py
       
    71 
       
    72     'for' in python works any kind of iterable objects. In our 
       
    73     previous sessions we used 'for' to iterate through files and lists.
       
    74     So in case we want to get square of say first five numbers using 
       
    75     'for' loop, we will have to create a list. For this we will use 
       
    76     'range' function available. Lets take a look at documentation 
       
    77     available for 'range' function by typing
       
    78     range?
       
    79     It takes three arguments, first being the start/initial value
       
    80     second one being stop/last value and third being the step size. 
       
    81     Out of these 'start' and 'step' arguments are optional.
       
    82     So if we use range to get first five number it would be 
       
    83     range(5)
       
    84     Note here that last/stop value is not included in resulting 
       
    85     list. So to get square of first five number all we have to do is
       
    86     iterate over this list.
       
    87     for i in range(5):
       
    88     ....print i, i*i
       
    89     ....
       
    90     ....
       
    91     Similarly to get square of all odd numbers from 3 to 9 we can do 
       
    92     something like
       
    93 
       
    94     for i in range(3, 10, 2):
       
    95     so the list returned from range this time will start from 3 and 
       
    96     end at 10(excluding it) with step size of 2 so we get odd numbers
       
    97     only
       
    98     ....print i, i*i
       
    99     ....
       
   100     ....
       
   101         
       
   102     That brings us to the end of this tutorial. We have covered solution
       
   103     of linear equations, finding roots of polynomials and non-linear
       
   104     equations. We have also learnt how to define functions and call
       
   105     them. 
       
   106 
       
   107     Thank you!
       
   108 
       
   109 *** Notes
       
   110