numbers.org
changeset 104 5cfcbd65ff1d
parent 102 84e1dcb52908
child 105 7722d269ff82
equal deleted inserted replaced
102:84e1dcb52908 104:5cfcbd65ff1d
    13     For 'Numbers' we have: int, float, complex datatypes    
    13     For 'Numbers' we have: int, float, complex datatypes    
    14     For conditional statements, 'Booleans'.
    14     For conditional statements, 'Booleans'.
    15     
    15     
    16     Lets get started by opening IPython interpreter. 
    16     Lets get started by opening IPython interpreter. 
    17     Lets start with  'numbers'
    17     Lets start with  'numbers'
    18     All integers irrespective of how big they are of 'int' data 
    18     All integers are of 'int' data type, irrespective of how big they
    19     type. Now we will create a variable, say
    19     are. Now we will create a variable, say
    20     x = 13
    20     x = 13
    21     print x
    21     print x
    22 
    22 
    23     To check the data type of any variable Python provides 'type' function
    23     To check the data type of any variable Python provides 'type' function
    24     type(x)
    24     type(x)
    25     
    25     
    26     lets create one more variable
    26     lets create one more variable
    27     y = 999999999999
    27     y = 999999999999
    28     (what about large value? and it is Long and not int, should we mention
       
    29     that?)
       
    30     print y
    28     print y
    31     
    29     
    32     Floating point numbers come under 'float'
    30     Floating point numbers come under 'float'
    33     p = 3.141592
    31     p = 3.141592
    34     type(p)
    32     type(p)
    37     c = 3+4j 
    35     c = 3+4j 
    38     creates a complex number c. Here 'j' is used to specify the imaginary 
    36     creates a complex number c. Here 'j' is used to specify the imaginary 
    39     part.
    37     part.
    40     type(c)
    38     type(c)
    41     Python also provides basic functions for their manipulations like
    39     Python also provides basic functions for their manipulations like
    42     abs(c) will return the absolute value of c(sqrt(a^2 + b^2))
    40     abs(c) will return the absolute value of c.
    43     c.imag returns imaginary part and c.real gives the real part. 
    41     c.imag returns imaginary part and c.real gives the real part. 
    44     
    42     
    45     All the basic operators work with Python data types, without any
    43     All the basic operators work with Python data types, without any
    46     surprises. When we try to add two numbers like x and y Python takes 
    44     surprises. When we try to add two numbers like x and y Python takes 
    47     cares of returning 'right' answer and we dont have to worry about
    45     cares of returning 'right' answer 
    48     boundary conditions.
       
    49     x + y
    46     x + y
    50     
    47     
    51     % operator is for modulo operations
       
    52     1786 % 12 gives 10
       
    53     45 % 2 returns 1
       
    54 
       
    55     Same as additions multiplication also works just right:
    48     Same as additions multiplication also works just right:
    56     3124 * 126789
    49     3124 * 126789
    57     396088836
    50     396088836
    58     
    51     
    59     x ** y returns x raised to power y. For example lets try:
       
    60     big = 1234567891234567890 ** 3
       
    61 
       
    62     Division in Python truncates, that is, when we divide a integer 
    52     Division in Python truncates, that is, when we divide a integer 
    63     variable with another integer result is also integer and decimal 
    53     variable with another integer result is also integer and decimal 
    64     value is truncated. So
    54     value is truncated. So
    65     17 / 2 returns 8 and not 8.5
    55     17 / 2 returns 8 and not 8.5
    66 
    56 
    67     but
    57     but
    68     17 / 2.0 will return the correct 8.5, similarly
    58     17 / 2.0 will return the correct 8.5, similarly
    69     17.0 / 2 will also give correct answer.
    59     17.0 / 2 will also give correct answer.
    70     
    60     
       
    61     x ** y returns x raised to power y. For example lets try:
       
    62     big = 1234567891234567890 ** 3
       
    63 
       
    64     % operator is for modulo operations
       
    65     1786 % 12 gives 10
       
    66     45 % 2 returns 1
       
    67 
    71     Other operators which comes handy are:
    68     Other operators which comes handy are:
    72     += 
    69     += 
    73     lets create one variable a with
    70     lets create one variable a with
    74     a =  7546
    71     a =  7546
    75     now
    72     now
    76     a += 1 will increment the value of 'a' by 1
    73     a += 1 will increment the value of 'a' by 1
    77     similarly 
    74     similarly 
    78     a -= 1 will decrement.
    75     a -= 1 will decrement.
    79     we can also use 
    76     we can also use 
    80     a *= 2
    77     a *= a
    81     a 
    78     a 
    82     answer is multiplied by 2
    79     a is multiplied by itself.
    83     
    80     
    84     a /= 5    
    81     a /= 5    
    85     a is divided by 5
    82     a is divided by 5
    86     
    83     
    87     Next we will look at Boolean datatype:
    84     Next we will look at Boolean datatype:
    92     Python is case sensitive language, so True with 'T' is boolean type but
    89     Python is case sensitive language, so True with 'T' is boolean type but
    93     true with 't' would be a variable. 
    90     true with 't' would be a variable. 
    94     
    91     
    95     f = not True
    92     f = not True
    96     
    93     
    97     we can do binary operation like 'or', 'and', 'not' with these variables
    94     we can do binary operations like 'or', 'and', 'not' with these variables
    98     f or t
    95     f or t
    99     f and t
    96     f and t
   100     
    97     
   101     in case of multiple binary operations to make sure of precedence use 
    98     in case of multiple binary operations to make sure of precedence use
   102     'brackets ()'
    99     'parenthesis ()'
   103     a = False
   100     a = False
   104     b = True
   101     b = True
   105     c = True
   102     c = True
   106     (a and b) or c    
   103     (a and b) or c    
   107     True
   104     True
   122     We can check for multiple logical operations in one statement itself.
   119     We can check for multiple logical operations in one statement itself.
   123     n < z < p
   120     n < z < p
   124     True.
   121     True.
   125     This statement checks if 'z' is smaller then 'p' and greater then 'n'
   122     This statement checks if 'z' is smaller then 'p' and greater then 'n'
   126     For inequality testing we use '!'
   123     For inequality testing we use '!'
   127     p + n != z will add 'p' and 'n' and check the equivalency with z
   124     p + n != z will add 'p' and 'n' and check the equivalence with z
   128 
   125 
   129     We have already covered briefly in some of the previous sessions, 
   126     We have already covered briefly in some of the previous sessions, 
   130     conversion of data among different types.
   127     conversion of data among different types.
   131     int(17 / 2.0) will convert result to integer type and we get
   128     int(17 / 2.0) will convert result to integer type and we get
   132     8 as answer and not 8.5
   129     8 as answer and not 8.5
   133     But if we try something like 
   130     But if we try something like 
   134     float(17 / 2) we get 8.0 as 17/2 is already truncated to int
   131     float(17 / 2) we get 8.0 as 17/2 is already truncated to int
   135     and converting that to float wont restore the lost decimal digits.
   132     and converting that to float wont restore the lost decimal digits.
   136     To rounding off a floating number to a given precision 'round' function
   133     To round off a float to a given precision 'round' function can be
   137     can be used.
   134     used. 
   138     round(7.5) returns 
   135     round(7.5) returns 8.
   139     8.
       
   140     
   136     
   141     This brings us to the end of tutorial on introduction of Data types 
   137     This brings us to the end of tutorial on introduction of Data types 
   142     related to numbers in Python. In this tutorial we have learnt what are 
   138     related to numbers in Python. In this tutorial we have learnt what are 
   143     supported data types for numbers, operations and operators and how to 
   139     supported data types for numbers, operations and operators and how to 
   144     convert one data type to other. Thank you!
   140     convert one data type to other. Thank you!