summaryrefslogtreecommitdiff
path: root/numbers.org
blob: 52dd32144f3be0223e00ee1072b0b9ca1d4a5c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
* 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 are of 'int' data type, irrespective of how big they
    are. 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
    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.
    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 
    x + y
    
    Same as additions multiplication also works just right:
    3124 * 126789
    396088836
    
    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.
    
    x ** y returns x raised to power y. For example lets try:
    big = 1234567891234567890 ** 3

    % operator is for modulo operations
    1786 % 12 gives 10
    45 % 2 returns 1

    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 *= a
    a 
    a is multiplied by itself.
    
    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 operations 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
    'parenthesis ()'
    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 equivalence 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 round off a float 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