numbers.org
changeset 128 fa5c77536e4e
parent 127 76fd286276f7
child 129 dcb9b50761eb
child 146 b92b4e7ecd7b
equal deleted inserted replaced
127:76fd286276f7 128:fa5c77536e4e
     1 * Data Types
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* What are we going to do?
       
     5 ******* How are we going to do?
       
     6 ******* Arsenal Required
       
     7 ********* None
       
     8 *** Script
       
     9     Welcome friends. 
       
    10     
       
    11     This session is about numbers and mathematical operations
       
    12 
       
    13     In this tutorial we shall be covering data types, operators and
       
    14     type conversion.
       
    15     To represent 'Numbers' in python, we have int, float, complex
       
    16     datatypes     
       
    17     For conditional statements, we have 'Bool' datatype
       
    18        
       
    19     type ipython on terminal to start the interpreter.
       
    20     Lets start with  'numbers'
       
    21     Now we will create a variable, say
       
    22     x = 13 lets confirm the value of x by
       
    23     print x
       
    24 
       
    25     To check the data type of any variable Python provides 'type' function
       
    26     type(x)
       
    27     which tells us that the x is of type 'int'
       
    28     
       
    29     lets create one more variable
       
    30     y = 999999999999
       
    31     print y
       
    32 
       
    33     Python can store any integer however big it is.    
       
    34     
       
    35     Floating point numbers come under 'float' datatype
       
    36     p = 3.141592
       
    37     type(p)
       
    38 
       
    39     Python by default provides support for complex numbers also.
       
    40     c = 3+4j 
       
    41     creates a complex number c with real part 3 and imaginary part 4.
       
    42     Please note that here 'j' is used to specify the imaginary 
       
    43     part and not i.
       
    44     type(c)
       
    45     Python also provides basic functions for their manipulations like
       
    46     abs(c) will return the absolute value of c.
       
    47     c.imag returns imaginary part and c.real gives the real part. 
       
    48     
       
    49     All the basic operators work with Python data types, without any
       
    50     surprises. When we try to add two numbers like x and y Python takes 
       
    51     cares of returning 'right' answer 
       
    52      
       
    53     print x + y gives sum of x and y
       
    54     
       
    55     Same as additions multiplication also works just right:
       
    56     123 * 4567
       
    57     gives you the product of both numbers
       
    58     
       
    59     Integer division in Python truncates, which means, when we divide an integer 
       
    60     with another integer result is also integer and decimal 
       
    61     value is truncated. So
       
    62     17 / 2 returns 8 and not 8.5
       
    63 
       
    64     but int and float value operations like
       
    65     17 / 2.0 will return the correct 8.5, similarly
       
    66     17.0 / 2 will also give correct answer.
       
    67     
       
    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
       
    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.
       
    75 
       
    76     % operator is for modulo operations
       
    77     1786 % 12 gives 10
       
    78     45 % 2 returns 1
       
    79 
       
    80     Other operators which comes handy are:
       
    81     += 
       
    82     lets create one variable a with
       
    83     a =  7546
       
    84     now
       
    85     a += 1 will increment the value of 'a' by 1
       
    86     similarly 
       
    87     a -= 1 will decrement.
       
    88     we can also use 
       
    89     a *= a
       
    90     a 
       
    91     a is multiplied by itself.
       
    92     
       
    93     a /= 5    
       
    94     a is divided by 5
       
    95     
       
    96     Next we will look at Boolean datatype:
       
    97     Its a primitive datatype having one of two values: True or False.
       
    98     t = True
       
    99     print t
       
   100 
       
   101     Python is case sensitive language, so True with 'T' is boolean type but
       
   102     true with 't' would be a variable. 
       
   103     
       
   104     f = not True
       
   105     
       
   106     we can do binary operations like 'or', 'and', 'not' with these variables
       
   107     f or t is false or true and hence we get true
       
   108     f and t is flase and true which gives false
       
   109     
       
   110     in case of multiple binary operations to make sure of precedence use
       
   111     'parenthesis ()'
       
   112     a = False
       
   113     b = True
       
   114     c = True
       
   115     if we need the result of a and b orred with c, we do
       
   116     (a and b) or c
       
   117     first a and b is evaluated and then the result is orred with c
       
   118     we get True
       
   119     but if we do 
       
   120     a and (b or c)
       
   121     there is a change in precedence and we get False
       
   122 
       
   123     Python also has support for relational and logical operators. Lets try some
       
   124     examples:
       
   125     We start with initializing three variables by typing
       
   126     p, z, n = 1, 0, -1 
       
   127     To check equivalency of two variables use '=='
       
   128     p == z checks if 1 is equal to 0 which is False
       
   129     p >= n checks if 1 is greater than or equal to -1 which is  True
       
   130     
       
   131     We can also check for multiple logical operations in one statement itself.
       
   132     n < z < p gives True.
       
   133     This statement checks if 'z' is smaller than 'p' and greater than 'n'
       
   134 
       
   135     For inequality testing we use '!'
       
   136     p + n != z will add 'p' and 'n' and check the equivalence with z
       
   137 
       
   138     We have already covered conversion between datatypes  in some of the previous sessions, briefly.
       
   139 
       
   140     Lets look at converting one data type to another
       
   141     lets create a float by typing z = 8.5
       
   142     and convert it to int using
       
   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
       
   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 
       
   162     To round off a float to a given precision 'round' function can be
       
   163     used. 
       
   164     round(7.5) returns 8.
       
   165     
       
   166     This brings us to the end of tutorial on introduction to Data types 
       
   167     related to numbers in Python. In this tutorial we have learnt what are 
       
   168     supported data types for numbers, operations and operators and how to 
       
   169     convert one data type to other. 
       
   170 
       
   171     Hope you have enjoyed the tutorial and found it useful.Thank you!
       
   172 
       
   173 *** Notes