numbers.org
changeset 105 7722d269ff82
parent 104 5cfcbd65ff1d
equal deleted inserted replaced
104:5cfcbd65ff1d 105:7722d269ff82
     6 ******* Arsenal Required
     6 ******* Arsenal Required
     7 ********* None
     7 ********* None
     8 *** Script
     8 *** Script
     9     Welcome friends. 
     9     Welcome friends. 
    10     
    10     
    11     In this tutorial we shall look at data types in Python and 
    11     This session is about numbers and mathematical operations
    12     mathematical operators available.
    12 
    13     For 'Numbers' we have: int, float, complex datatypes    
    13     In this tutorial we shall be covering data types, operators and
    14     For conditional statements, 'Booleans'.
    14     type conversion.
    15     
    15     To represent 'Numbers' in python, we have int, float, complex
    16     Lets get started by opening IPython interpreter. 
    16     datatypes     
       
    17     For conditional statements, we have 'Bool' datatype
       
    18        
       
    19     type ipython on terminal to start the interpreter.
    17     Lets start with  'numbers'
    20     Lets start with  'numbers'
    18     All integers are of 'int' data type, irrespective of how big they
    21     Now we will create a variable, say
    19     are. Now we will create a variable, say
    22     x = 13 lets confirm the value of x by
    20     x = 13
       
    21     print x
    23     print x
    22 
    24 
    23     To check the data type of any variable Python provides 'type' function
    25     To check the data type of any variable Python provides 'type' function
    24     type(x)
    26     type(x)
       
    27     which tells us that the x is of type 'int'
    25     
    28     
    26     lets create one more variable
    29     lets create one more variable
    27     y = 999999999999
    30     y = 999999999999
    28     print y
    31     print y
       
    32 
       
    33     Python can store any integer however big it is.    
    29     
    34     
    30     Floating point numbers come under 'float'
    35     Floating point numbers come under 'float' datatype
    31     p = 3.141592
    36     p = 3.141592
    32     type(p)
    37     type(p)
    33 
    38 
    34     Python by default provides support for complex numbers. 
    39     Python by default provides support for complex numbers also.
    35     c = 3+4j 
    40     c = 3+4j 
    36     creates a complex number c. Here 'j' is used to specify the imaginary 
    41     creates a complex number c with real part 3 and imaginary part 4.
    37     part.
    42     Please note that here 'j' is used to specify the imaginary 
       
    43     part and not i.
    38     type(c)
    44     type(c)
    39     Python also provides basic functions for their manipulations like
    45     Python also provides basic functions for their manipulations like
    40     abs(c) will return the absolute value of c.
    46     abs(c) will return the absolute value of c.
    41     c.imag returns imaginary part and c.real gives the real part. 
    47     c.imag returns imaginary part and c.real gives the real part. 
    42     
    48     
    43     All the basic operators work with Python data types, without any
    49     All the basic operators work with Python data types, without any
    44     surprises. When we try to add two numbers like x and y Python takes 
    50     surprises. When we try to add two numbers like x and y Python takes 
    45     cares of returning 'right' answer 
    51     cares of returning 'right' answer 
    46     x + y
    52      
       
    53     print x + y gives sum of x and y
    47     
    54     
    48     Same as additions multiplication also works just right:
    55     Same as additions multiplication also works just right:
    49     3124 * 126789
    56     123 * 4567
    50     396088836
    57     gives you the product of both numbers
    51     
    58     
    52     Division in Python truncates, that is, when we divide a integer 
    59     Integer division in Python truncates, which means, when we divide an integer 
    53     variable with another integer result is also integer and decimal 
    60     with another integer result is also integer and decimal 
    54     value is truncated. So
    61     value is truncated. So
    55     17 / 2 returns 8 and not 8.5
    62     17 / 2 returns 8 and not 8.5
    56 
    63 
    57     but
    64     but int and float value operations like
    58     17 / 2.0 will return the correct 8.5, similarly
    65     17 / 2.0 will return the correct 8.5, similarly
    59     17.0 / 2 will also give correct answer.
    66     17.0 / 2 will also give correct answer.
    60     
    67     
    61     x ** y returns x raised to power y. For example lets try:
    68     in python x ** y returns x raised to power y. For example lets try:
       
    69     2 ** 3 and we get 2 raised to power 3 which is 8
       
    70 
       
    71     now lets try power operation involving a big number
    62     big = 1234567891234567890 ** 3
    72     big = 1234567891234567890 ** 3
       
    73     As we know, any number irrespective of its size can be represented in python.
       
    74     hence big is a really big number and print big prints the value of big.
    63 
    75 
    64     % operator is for modulo operations
    76     % operator is for modulo operations
    65     1786 % 12 gives 10
    77     1786 % 12 gives 10
    66     45 % 2 returns 1
    78     45 % 2 returns 1
    67 
    79 
    90     true with 't' would be a variable. 
   102     true with 't' would be a variable. 
    91     
   103     
    92     f = not True
   104     f = not True
    93     
   105     
    94     we can do binary operations like 'or', 'and', 'not' with these variables
   106     we can do binary operations like 'or', 'and', 'not' with these variables
    95     f or t
   107     f or t is false or true and hence we get true
    96     f and t
   108     f and t is flase and true which gives false
    97     
   109     
    98     in case of multiple binary operations to make sure of precedence use
   110     in case of multiple binary operations to make sure of precedence use
    99     'parenthesis ()'
   111     'parenthesis ()'
   100     a = False
   112     a = False
   101     b = True
   113     b = True
   102     c = True
   114     c = True
   103     (a and b) or c    
   115     if we need the result of a and b orred with c, we do
   104     True
   116     (a and b) or c
   105     first a and b is evaluated and then the 'or' statement
   117     first a and b is evaluated and then the result is orred with c
       
   118     we get True
       
   119     but if we do 
   106     a and (b or c)
   120     a and (b or c)
   107     False
   121     there is a change in precedence and we get False
   108 
   122 
   109     We also have support for relational and logical operators. Lets try some
   123     Python also has support for relational and logical operators. Lets try some
   110     examples:
   124     examples:
   111     We start with initializing three variables by:
   125     We start with initializing three variables by typing
   112     p, z, n = 1, 0, -1 
   126     p, z, n = 1, 0, -1 
   113     To check equivalency of two variables use '=='
   127     To check equivalency of two variables use '=='
   114     p == z 
   128     p == z checks if 1 is equal to 0 which is False
   115     False
   129     p >= n checks if 1 is greater than or equal to -1 which is  True
   116     p >= n
       
   117     True
       
   118     
   130     
   119     We can check for multiple logical operations in one statement itself.
   131     We can also check for multiple logical operations in one statement itself.
   120     n < z < p
   132     n < z < p gives True.
   121     True.
   133     This statement checks if 'z' is smaller than 'p' and greater than 'n'
   122     This statement checks if 'z' is smaller then 'p' and greater then 'n'
   134 
   123     For inequality testing we use '!'
   135     For inequality testing we use '!'
   124     p + n != z will add 'p' and 'n' and check the equivalence with z
   136     p + n != z will add 'p' and 'n' and check the equivalence with z
   125 
   137 
   126     We have already covered briefly in some of the previous sessions, 
   138     We have already covered conversion between datatypes  in some of the previous sessions, briefly.
   127     conversion of data among different types.
   139 
   128     int(17 / 2.0) will convert result to integer type and we get
   140     Lets look at converting one data type to another
   129     8 as answer and not 8.5
   141     lets create a float by typing z = 8.5
   130     But if we try something like 
   142     and convert it to int using
   131     float(17 / 2) we get 8.0 as 17/2 is already truncated to int
   143     i = int(z)
       
   144     lets see what is in i by typing print i
       
   145     and we get 8
       
   146     we can even check the datatype of i by typing type(i)
       
   147     and we get int
       
   148 
       
   149     similarly float(5) gives 5.0 which is a float
       
   150     
       
   151     type float_a = 2.0 and int_a = 2
       
   152     17 / float_a gives 8.5
       
   153     and int( 17 / float_a ) gives you 8 since int function truncates the decimal value of the result
       
   154 
       
   155 
       
   156     float(17 / int_a ) we get 8.0 and not 8.5 since 17/2 is already truncated to 8
   132     and converting that to float wont restore the lost decimal digits.
   157     and converting that to float wont restore the lost decimal digits.
       
   158 
       
   159     To get correct answer from such division try    
       
   160     17 / float(a)
       
   161 
   133     To round off a float to a given precision 'round' function can be
   162     To round off a float to a given precision 'round' function can be
   134     used. 
   163     used. 
   135     round(7.5) returns 8.
   164     round(7.5) returns 8.
   136     
   165     
   137     This brings us to the end of tutorial on introduction of Data types 
   166     This brings us to the end of tutorial on introduction to Data types 
   138     related to numbers in Python. In this tutorial we have learnt what are 
   167     related to numbers in Python. In this tutorial we have learnt what are 
   139     supported data types for numbers, operations and operators and how to 
   168     supported data types for numbers, operations and operators and how to 
   140     convert one data type to other. Thank you!
   169     convert one data type to other. 
       
   170 
       
   171     Hope you have enjoyed the tutorial and found it useful.Thank you!
   141 
   172 
   142 *** Notes
   173 *** Notes