numbers.org
author Puneeth Chaganti <punchagan@gmail.com>
Thu, 22 Apr 2010 13:28:13 +0530
changeset 104 5cfcbd65ff1d
parent 102 84e1dcb52908
child 105 7722d269ff82
permissions -rw-r--r--
minor edits to numbers org file.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
* Data Types
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
*** Outline
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
***** Introduction
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
******* What are we going to do?
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
******* How are we going to do?
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
******* Arsenal Required
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
********* None
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
*** Script
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
    Welcome friends. 
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    11
    In this tutorial we shall look at data types in Python and 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    12
    mathematical operators available.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    13
    For 'Numbers' we have: int, float, complex datatypes    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    14
    For conditional statements, 'Booleans'.
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    16
    Lets get started by opening IPython interpreter. 
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    17
    Lets start with  'numbers'
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    18
    All integers are of 'int' data type, irrespective of how big they
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    19
    are. Now we will create a variable, say
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    20
    x = 13
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    21
    print x
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
    To check the data type of any variable Python provides 'type' function
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    24
    type(x)
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    26
    lets create one more variable
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    27
    y = 999999999999
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    28
    print y
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    30
    Floating point numbers come under 'float'
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    31
    p = 3.141592
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    32
    type(p)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    33
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    34
    Python by default provides support for complex numbers. 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    35
    c = 3+4j 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    36
    creates a complex number c. Here 'j' is used to specify the imaginary 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    37
    part.
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    38
    type(c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    39
    Python also provides basic functions for their manipulations like
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    40
    abs(c) will return the absolute value of c.
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    41
    c.imag returns imaginary part and c.real gives the real part. 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    42
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    43
    All the basic operators work with Python data types, without any
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    44
    surprises. When we try to add two numbers like x and y Python takes 
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    45
    cares of returning 'right' answer 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    46
    x + y
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    47
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    48
    Same as additions multiplication also works just right:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    49
    3124 * 126789
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    50
    396088836
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    51
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    52
    Division in Python truncates, that is, when we divide a integer 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    53
    variable with another integer result is also integer and decimal 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    54
    value is truncated. So
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    55
    17 / 2 returns 8 and not 8.5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    56
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    57
    but
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    58
    17 / 2.0 will return the correct 8.5, similarly
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    59
    17.0 / 2 will also give correct answer.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    60
    
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    61
    x ** y returns x raised to power y. For example lets try:
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    62
    big = 1234567891234567890 ** 3
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    63
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    64
    % operator is for modulo operations
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    65
    1786 % 12 gives 10
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    66
    45 % 2 returns 1
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    67
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    68
    Other operators which comes handy are:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    69
    += 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    70
    lets create one variable a with
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    71
    a =  7546
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    72
    now
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    73
    a += 1 will increment the value of 'a' by 1
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    74
    similarly 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    75
    a -= 1 will decrement.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    76
    we can also use 
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    77
    a *= a
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    78
    a 
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    79
    a is multiplied by itself.
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    80
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    81
    a /= 5    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    82
    a is divided by 5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    83
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    84
    Next we will look at Boolean datatype:
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    85
    Its a primitive datatype having one of two values: True or False.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    86
    t = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    87
    print t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    88
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    89
    Python is case sensitive language, so True with 'T' is boolean type but
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    90
    true with 't' would be a variable. 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    91
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    92
    f = not True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    93
    
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    94
    we can do binary operations like 'or', 'and', 'not' with these variables
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    95
    f or t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    96
    f and t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    97
    
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    98
    in case of multiple binary operations to make sure of precedence use
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    99
    'parenthesis ()'
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   100
    a = False
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   101
    b = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   102
    c = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   103
    (a and b) or c    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   104
    True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   105
    first a and b is evaluated and then the 'or' statement
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   106
    a and (b or c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   107
    False
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   109
    We also have support for relational and logical operators. Lets try some
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   110
    examples:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   111
    We start with initializing three variables by:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   112
    p, z, n = 1, 0, -1 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   113
    To check equivalency of two variables use '=='
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   114
    p == z 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   115
    False
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   116
    p >= n
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   117
    True
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   118
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   119
    We can check for multiple logical operations in one statement itself.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   120
    n < z < p
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   121
    True.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   122
    This statement checks if 'z' is smaller then 'p' and greater then 'n'
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   123
    For inequality testing we use '!'
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   124
    p + n != z will add 'p' and 'n' and check the equivalence with z
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   125
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   126
    We have already covered briefly in some of the previous sessions, 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   127
    conversion of data among different types.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   128
    int(17 / 2.0) will convert result to integer type and we get
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   129
    8 as answer and not 8.5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   130
    But if we try something like 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   131
    float(17 / 2) we get 8.0 as 17/2 is already truncated to int
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   132
    and converting that to float wont restore the lost decimal digits.
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   133
    To round off a float to a given precision 'round' function can be
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   134
    used. 
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   135
    round(7.5) returns 8.
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   136
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   137
    This brings us to the end of tutorial on introduction of Data types 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   138
    related to numbers in Python. In this tutorial we have learnt what are 
102
84e1dcb52908 changes to numbers org file.
Shantanu <shantanu@fossee.in>
parents: 98
diff changeset
   139
    supported data types for numbers, operations and operators and how to 
84e1dcb52908 changes to numbers org file.
Shantanu <shantanu@fossee.in>
parents: 98
diff changeset
   140
    convert one data type to other. Thank you!
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   141
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   142
*** Notes