data-types.org
author Puneeth Chaganti <punchagan@gmail.com>
Wed, 21 Apr 2010 14:28:38 +0530
changeset 94 57ae1f75b7e0
parent 92 fa26bdda8f32
permissions -rw-r--r--
Minor edits to data-file.org.
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
    
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
    In this tutorial we shall look at data types available in Python and 
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
    how to perform simple Input and Output operations. 
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    13
    for 'Numbers' we have: int, float, complex datatypes
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    14
    for Text content we have strings.
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    15
    For conditional statements, 'Booleans'.
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    17
    Lets get started by opening IPython interpreter. 
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    18
    Lets start with  'numbers'
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    19
    All integers irrespective of how big they are, are of 'int'
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
    data type
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    21
    Now we will create a variable, say
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    22
    x = 13
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)
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    28
    y = 999999999999
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    29
    print y
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
    
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    31
    Floating point numbers comes under 'float'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    32
    p = 3.141592
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    33
    type(p)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    34
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    35
    Python by default provides support for complex numbers. 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    36
    c = 3+4j 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    37
    c is a complex number. 'j' is used to specify the imaginary part.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    38
    type(c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    39
    Python also provides basic functions for their manipulations like
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    40
    abs(c) will return the absolute value of c(sqrt(a^2 + b^2))
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    41
    c.imag returns imaginary part and c.real gives the real part. 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    42
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
    43
    Next we will look at Boolean datatype:
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    44
    Its a primitive datatype having one of two values: True or False.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    45
    t = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    46
    print t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    47
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    48
    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
    49
    true with 't' would be a variable. 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    50
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    51
    f = not True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    52
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    53
    we can do binary operation like 'or' and 'not' with these variables
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    54
    f or t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    55
    f and t
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    56
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    57
    in case of multiple binary operations to make sure of precedence use 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    58
    'brackets ()'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    59
    a = False
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    60
    b = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    61
    c = True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    62
    (a and b) or c    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    63
    True
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    64
    first a and b is evaluated and then the 'or' statement
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    65
    a and (b or c)
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    66
    False
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    68
    Now we shall look at Python Strings.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    69
    In python anything enclosed inside quotes(single or double) is a string
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    70
    so 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    71
    a = 'This is a string'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    72
    print a
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    73
    b = "This too!"
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    74
    print b
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    75
    c = '''This one too!'''
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    76
    print c
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    77
    d = """And one more."""
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    78
    print d
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    79
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    80
    Similar to lists we covered earlier even string elements can be accessed 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    81
    via index numbers starting from 0
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    82
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    83
    print a[0]    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    84
    print a[5]
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    85
    will 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    86
    To access last element we can use a[-1] which is one of Pythons feature.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    87
    print a[-1]
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    88
    len function works with the strings also as it does with the arrays and 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    89
    returns length of the string.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    90
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    91
    One thing to notice about the string variables is that they are 
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    92
    immutable, that is
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    93
    a[0] = 't'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    94
    will throw an error
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    95
    
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    96
    Some of methods available for string are:
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    97
    a.startswith('Thi')
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    98
    returns true if initial of the string is same
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
    99
    similarly there is endswith
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   100
    a.endswith('ING')
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
   101
    a.upper() returns a string with all letters capitalized.
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   102
    and a.lower() returns a string with all smaller case letters.
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   103
    As we have seen earlier use of split function, it returns the list after
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   104
    splitting the string, so
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   105
    a.split()
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   106
    will give list with three elements.
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
   107
    we also have 'join' function, which does the opposite of what
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
   108
    split does. 
92
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   109
    ''.join(['a','b','c']) will return a joined string of the list we pass
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   110
    to it. Since join is performed on '' that is empty string we get 'abc'
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   111
    if we do something like
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   112
    '-'.join(['a','b','c'])
fa26bdda8f32 Data types covered.
Shantanu <shantanu@fossee.in>
parents: 91
diff changeset
   113
    
94
57ae1f75b7e0 Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 92
diff changeset
   114
    we come to the end of this tutorial on introduction of Data types in
91
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
    Python. In this tutorial we have learnt what are supported data types, 
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
    supported operations and performing simple IO operations in Python.
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
eb15f55e4689 Started work on day2 session 1.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   118
*** Notes