numbers.org
author Shantanu <shantanu@fossee.in>
Wed, 21 Apr 2010 17:47:40 +0530
changeset 98 8e02b76cf068
parent 95 fddcfd83e4f0
child 102 84e1dcb52908
permissions -rw-r--r--
Number data types scripts.
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'
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    18
    All integers irrespective of how big they are of 'int' data 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    19
    type. 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
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    28
    (what about large value? and it is Long and not int, should we mention
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    29
    that?)
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    30
    print y
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    32
    Floating point numbers come under 'float'
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    33
    p = 3.141592
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    34
    type(p)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    35
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    36
    Python by default provides support for complex numbers. 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    37
    c = 3+4j 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    38
    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
    39
    part.
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    40
    type(c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    41
    Python also provides basic functions for their manipulations like
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    42
    abs(c) will return the absolute value of c(sqrt(a^2 + b^2))
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    43
    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
    44
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    45
    All the basic operators work with Python data types, without any
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    46
    surprises. When we try to add two numbers like x and y Python takes 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    47
    cares of returning 'right' answer and we dont have to worry about
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    48
    boundary conditions.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    49
    x + y
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    50
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    51
    % operator is for modulo operations
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    52
    1786 % 12 gives 10
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    53
    45 % 2 returns 1
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    54
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    55
    Same as additions multiplication also works just right:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    56
    3124 * 126789
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    57
    396088836
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    58
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    59
    x ** y returns x raised to power y. For example lets try:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    60
    big = 1234567891234567890 ** 3
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    61
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    62
    Division in Python truncates, that is, when we divide a integer 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    63
    variable with another integer result is also integer and decimal 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    64
    value is truncated. So
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    65
    17 / 2 returns 8 and not 8.5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    66
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    67
    but
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    68
    17 / 2.0 will return the correct 8.5, similarly
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    69
    17.0 / 2 will also give correct answer.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    70
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    71
    Other operators which comes handy are:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    72
    += 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    73
    lets create one variable a with
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    74
    a =  7546
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    75
    now
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    76
    a += 1 will increment the value of 'a' by 1
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    77
    similarly 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    78
    a -= 1 will decrement.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    79
    we can also use 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    80
    a *= 2
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    81
    a 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    82
    answer is multiplied by 2
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    83
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    84
    a /= 5    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    85
    a is divided by 5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    86
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    87
    Next we will look at Boolean datatype:
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    88
    Its a primitive datatype having one of two values: True or False.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    89
    t = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    90
    print t
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
    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
    93
    true with 't' would be a variable. 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    94
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    95
    f = not True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    96
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    97
    we can do binary operation like 'or', 'and', 'not' with these variables
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    98
    f or t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    99
    f and t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   100
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   101
    in case of multiple binary operations to make sure of precedence use 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   102
    'brackets ()'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   103
    a = False
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   104
    b = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   105
    c = True
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
    True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   108
    first a and b is evaluated and then the 'or' statement
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   109
    a and (b or c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   110
    False
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   112
    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
   113
    examples:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   114
    We start with initializing three variables by:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   115
    p, z, n = 1, 0, -1 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   116
    To check equivalency of two variables use '=='
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   117
    p == z 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   118
    False
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   119
    p >= n
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   120
    True
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   121
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   122
    We can check for multiple logical operations in one statement itself.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   123
    n < z < p
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   124
    True.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   125
    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
   126
    For inequality testing we use '!'
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   127
    p + n != z will add 'p' and 'n' and check the equivalency with z
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   128
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   129
    We have already covered briefly in some of the previous sessions, 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   130
    conversion of data among different types.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   131
    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
   132
    8 as answer and not 8.5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   133
    But if we try something like 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   134
    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
   135
    and converting that to float wont restore the lost decimal digits.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   136
    To rounding off a floating number to a given precision 'round' function
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   137
    can be used.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   138
    round(7.5) returns 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   139
    8.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   140
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   141
    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
   142
    related to numbers in Python. In this tutorial we have learnt what are 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   143
    supported data types, supported operations and operators. How to convert 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   144
    one data type to other.
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
*** Notes