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