numbers.org
author Shantanu <shantanu@fossee.in>
Thu, 22 Apr 2010 13:08:06 +0530
changeset 102 84e1dcb52908
parent 98 8e02b76cf068
child 104 5cfcbd65ff1d
permissions -rw-r--r--
changes to numbers org file.

* Data Types
*** Outline
***** Introduction
******* What are we going to do?
******* How are we going to do?
******* Arsenal Required
********* None
*** Script
    Welcome friends. 
    
    In this tutorial we shall look at data types in Python and 
    mathematical operators available.
    For 'Numbers' we have: int, float, complex datatypes    
    For conditional statements, 'Booleans'.
    
    Lets get started by opening IPython interpreter. 
    Lets start with  'numbers'
    All integers irrespective of how big they are of 'int' data 
    type. Now we will create a variable, say
    x = 13
    print x

    To check the data type of any variable Python provides 'type' function
    type(x)
    
    lets create one more variable
    y = 999999999999
    (what about large value? and it is Long and not int, should we mention
    that?)
    print y
    
    Floating point numbers come under 'float'
    p = 3.141592
    type(p)

    Python by default provides support for complex numbers. 
    c = 3+4j 
    creates a complex number c. Here 'j' is used to specify the imaginary 
    part.
    type(c)
    Python also provides basic functions for their manipulations like
    abs(c) will return the absolute value of c(sqrt(a^2 + b^2))
    c.imag returns imaginary part and c.real gives the real part. 
    
    All the basic operators work with Python data types, without any
    surprises. When we try to add two numbers like x and y Python takes 
    cares of returning 'right' answer and we dont have to worry about
    boundary conditions.
    x + y
    
    % operator is for modulo operations
    1786 % 12 gives 10
    45 % 2 returns 1

    Same as additions multiplication also works just right:
    3124 * 126789
    396088836
    
    x ** y returns x raised to power y. For example lets try:
    big = 1234567891234567890 ** 3

    Division in Python truncates, that is, when we divide a integer 
    variable with another integer result is also integer and decimal 
    value is truncated. So
    17 / 2 returns 8 and not 8.5

    but
    17 / 2.0 will return the correct 8.5, similarly
    17.0 / 2 will also give correct answer.
    
    Other operators which comes handy are:
    += 
    lets create one variable a with
    a =  7546
    now
    a += 1 will increment the value of 'a' by 1
    similarly 
    a -= 1 will decrement.
    we can also use 
    a *= 2
    a 
    answer is multiplied by 2
    
    a /= 5    
    a is divided by 5
    
    Next we will look at Boolean datatype:
    Its a primitive datatype having one of two values: True or False.
    t = True
    print t

    Python is case sensitive language, so True with 'T' is boolean type but
    true with 't' would be a variable. 
    
    f = not True
    
    we can do binary operation like 'or', 'and', 'not' with these variables
    f or t
    f and t
    
    in case of multiple binary operations to make sure of precedence use 
    'brackets ()'
    a = False
    b = True
    c = True
    (a and b) or c    
    True
    first a and b is evaluated and then the 'or' statement
    a and (b or c)
    False

    We also have support for relational and logical operators. Lets try some
    examples:
    We start with initializing three variables by:
    p, z, n = 1, 0, -1 
    To check equivalency of two variables use '=='
    p == z 
    False
    p >= n
    True
    
    We can check for multiple logical operations in one statement itself.
    n < z < p
    True.
    This statement checks if 'z' is smaller then 'p' and greater then 'n'
    For inequality testing we use '!'
    p + n != z will add 'p' and 'n' and check the equivalency with z

    We have already covered briefly in some of the previous sessions, 
    conversion of data among different types.
    int(17 / 2.0) will convert result to integer type and we get
    8 as answer and not 8.5
    But if we try something like 
    float(17 / 2) we get 8.0 as 17/2 is already truncated to int
    and converting that to float wont restore the lost decimal digits.
    To rounding off a floating number to a given precision 'round' function
    can be used.
    round(7.5) returns 
    8.
    
    This brings us to the end of tutorial on introduction of Data types 
    related to numbers in Python. In this tutorial we have learnt what are 
    supported data types for numbers, operations and operators and how to 
    convert one data type to other. Thank you!

*** Notes