numbers.org
author Shantanu <shantanu@fossee.in>
Wed, 28 Apr 2010 16:33:18 +0530
changeset 115 d35eccbf206d
parent 105 7722d269ff82
permissions -rw-r--r--
Added dictionary.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
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    11
    This session is about numbers and mathematical operations
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    12
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    13
    In this tutorial we shall be covering data types, operators and
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    14
    type conversion.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    15
    To represent 'Numbers' in python, we have int, float, complex
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    16
    datatypes     
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    17
    For conditional statements, we have 'Bool' datatype
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    18
       
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    19
    type ipython on terminal to start the interpreter.
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    20
    Lets start with  'numbers'
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    21
    Now we will create a variable, say
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    22
    x = 13 lets confirm the value of x by
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    23
    print x
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
    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
    26
    type(x)
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    27
    which tells us that the x is of type 'int'
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
    
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    29
    lets create one more variable
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    30
    y = 999999999999
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    31
    print y
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    32
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    33
    Python can store any integer however big it is.    
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    35
    Floating point numbers come under 'float' datatype
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    36
    p = 3.141592
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    37
    type(p)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    38
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    39
    Python by default provides support for complex numbers also.
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    40
    c = 3+4j 
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    41
    creates a complex number c with real part 3 and imaginary part 4.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    42
    Please note that here 'j' is used to specify the imaginary 
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    43
    part and not i.
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    44
    type(c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    45
    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
    46
    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
    47
    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
    48
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    49
    All the basic operators work with Python data types, without any
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    50
    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
    51
    cares of returning 'right' answer 
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    52
     
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    53
    print x + y gives sum of x and y
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    54
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    55
    Same as additions multiplication also works just right:
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    56
    123 * 4567
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    57
    gives you the product of both numbers
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    58
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    59
    Integer division in Python truncates, which means, when we divide an integer 
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    60
    with another integer result is also integer and decimal 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    61
    value is truncated. So
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    62
    17 / 2 returns 8 and not 8.5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    63
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    64
    but int and float value operations like
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    65
    17 / 2.0 will return the correct 8.5, similarly
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    66
    17.0 / 2 will also give correct answer.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    67
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    68
    in python x ** y returns x raised to power y. For example lets try:
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    69
    2 ** 3 and we get 2 raised to power 3 which is 8
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    70
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    71
    now lets try power operation involving a big number
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    72
    big = 1234567891234567890 ** 3
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    73
    As we know, any number irrespective of its size can be represented in python.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
    74
    hence big is a really big number and print big prints the value of big.
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    75
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    76
    % operator is for modulo operations
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    77
    1786 % 12 gives 10
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    78
    45 % 2 returns 1
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    79
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    80
    Other operators which comes handy are:
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    81
    += 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    82
    lets create one variable a with
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    83
    a =  7546
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    84
    now
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    85
    a += 1 will increment the value of 'a' by 1
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    86
    similarly 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    87
    a -= 1 will decrement.
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    88
    we can also use 
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    89
    a *= a
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    90
    a 
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
    91
    a is multiplied by itself.
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    92
    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    93
    a /= 5    
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    94
    a is divided by 5
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
    95
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    96
    Next we will look at Boolean datatype:
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    97
    Its a primitive datatype having one of two values: True or False.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    98
    t = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    99
    print 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
    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
   102
    true with 't' would be a variable. 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   103
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   104
    f = not True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   105
    
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   106
    we can do binary operations like 'or', 'and', 'not' with these variables
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   107
    f or t is false or true and hence we get true
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   108
    f and t is flase and true which gives false
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   109
    
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   110
    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
   111
    'parenthesis ()'
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   112
    a = False
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   113
    b = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   114
    c = True
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   115
    if we need the result of a and b orred with c, we do
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   116
    (a and b) or c
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   117
    first a and b is evaluated and then the result is orred with c
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   118
    we get True
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   119
    but if we do 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   120
    a and (b or c)
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   121
    there is a change in precedence and we get False
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   123
    Python also has support for relational and logical operators. Lets try some
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   124
    examples:
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   125
    We start with initializing three variables by typing
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   126
    p, z, n = 1, 0, -1 
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   127
    To check equivalency of two variables use '=='
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   128
    p == z checks if 1 is equal to 0 which is False
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   129
    p >= n checks if 1 is greater than or equal to -1 which is  True
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   130
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   131
    We can also check for multiple logical operations in one statement itself.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   132
    n < z < p gives True.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   133
    This statement checks if 'z' is smaller than 'p' and greater than 'n'
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   134
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   135
    For inequality testing we use '!'
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   136
    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
   137
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   138
    We have already covered conversion between datatypes  in some of the previous sessions, briefly.
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   139
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   140
    Lets look at converting one data type to another
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   141
    lets create a float by typing z = 8.5
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   142
    and convert it to int using
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   143
    i = int(z)
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   144
    lets see what is in i by typing print i
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   145
    and we get 8
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   146
    we can even check the datatype of i by typing type(i)
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   147
    and we get int
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   148
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   149
    similarly float(5) gives 5.0 which is a float
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   150
    
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   151
    type float_a = 2.0 and int_a = 2
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   152
    17 / float_a gives 8.5
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   153
    and int( 17 / float_a ) gives you 8 since int function truncates the decimal value of the result
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   154
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   155
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   156
    float(17 / int_a ) we get 8.0 and not 8.5 since 17/2 is already truncated to 8
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   157
    and converting that to float wont restore the lost decimal digits.
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   158
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   159
    To get correct answer from such division try    
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   160
    17 / float(a)
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   161
104
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   162
    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
   163
    used. 
5cfcbd65ff1d minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 102
diff changeset
   164
    round(7.5) returns 8.
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   165
    
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   166
    This brings us to the end of tutorial on introduction to Data types 
98
8e02b76cf068 Number data types scripts.
Shantanu <shantanu@fossee.in>
parents: 95
diff changeset
   167
    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
   168
    supported data types for numbers, operations and operators and how to 
105
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   169
    convert one data type to other. 
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   170
7722d269ff82 edits by nishanth to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents: 104
diff changeset
   171
    Hope you have enjoyed the tutorial and found it useful.Thank you!
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   172
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   173
*** Notes