author | Shantanu <shantanu@fossee.in> |
Wed, 21 Apr 2010 17:47:40 +0530 | |
changeset 98 | 8e02b76cf068 |
parent 95 | fddcfd83e4f0 |
child 102 | 84e1dcb52908 |
permissions | -rw-r--r-- |
91 | 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 |
||
98 | 11 |
In this tutorial we shall look at data types in Python and |
12 |
mathematical operators available. |
|
13 |
For 'Numbers' we have: int, float, complex datatypes |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
14 |
For conditional statements, 'Booleans'. |
91 | 15 |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
16 |
Lets get started by opening IPython interpreter. |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
17 |
Lets start with 'numbers' |
98 | 18 |
All integers irrespective of how big they are of 'int' data |
19 |
type. Now we will create a variable, say |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
20 |
x = 13 |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
21 |
print x |
91 | 22 |
|
23 |
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
|
24 |
type(x) |
91 | 25 |
|
98 | 26 |
lets create one more variable |
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
27 |
y = 999999999999 |
98 | 28 |
(what about large value? and it is Long and not int, should we mention |
29 |
that?) |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
30 |
print y |
91 | 31 |
|
98 | 32 |
Floating point numbers come under 'float' |
92 | 33 |
p = 3.141592 |
34 |
type(p) |
|
35 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
36 |
Python by default provides support for complex numbers. |
92 | 37 |
c = 3+4j |
98 | 38 |
creates a complex number c. Here 'j' is used to specify the imaginary |
39 |
part. |
|
92 | 40 |
type(c) |
41 |
Python also provides basic functions for their manipulations like |
|
42 |
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
|
43 |
c.imag returns imaginary part and c.real gives the real part. |
98 | 44 |
|
45 |
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 |
|
47 |
cares of returning 'right' answer and we dont have to worry about |
|
48 |
boundary conditions. |
|
49 |
x + y |
|
50 |
||
51 |
% operator is for modulo operations |
|
52 |
1786 % 12 gives 10 |
|
53 |
45 % 2 returns 1 |
|
92 | 54 |
|
98 | 55 |
Same as additions multiplication also works just right: |
56 |
3124 * 126789 |
|
57 |
396088836 |
|
58 |
||
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 |
|
63 |
variable with another integer result is also integer and decimal |
|
64 |
value is truncated. So |
|
65 |
17 / 2 returns 8 and not 8.5 |
|
66 |
||
67 |
but |
|
68 |
17 / 2.0 will return the correct 8.5, similarly |
|
69 |
17.0 / 2 will also give correct answer. |
|
70 |
||
71 |
Other operators which comes handy are: |
|
72 |
+= |
|
73 |
lets create one variable a with |
|
74 |
a = 7546 |
|
75 |
now |
|
76 |
a += 1 will increment the value of 'a' by 1 |
|
77 |
similarly |
|
78 |
a -= 1 will decrement. |
|
79 |
we can also use |
|
80 |
a *= 2 |
|
81 |
a |
|
82 |
answer is multiplied by 2 |
|
83 |
||
84 |
a /= 5 |
|
85 |
a is divided by 5 |
|
86 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
87 |
Next we will look at Boolean datatype: |
92 | 88 |
Its a primitive datatype having one of two values: True or False. |
89 |
t = True |
|
90 |
print t |
|
91 |
||
92 |
Python is case sensitive language, so True with 'T' is boolean type but |
|
93 |
true with 't' would be a variable. |
|
94 |
||
95 |
f = not True |
|
96 |
||
98 | 97 |
we can do binary operation like 'or', 'and', 'not' with these variables |
92 | 98 |
f or t |
99 |
f and t |
|
100 |
||
101 |
in case of multiple binary operations to make sure of precedence use |
|
102 |
'brackets ()' |
|
103 |
a = False |
|
104 |
b = True |
|
105 |
c = True |
|
106 |
(a and b) or c |
|
107 |
True |
|
108 |
first a and b is evaluated and then the 'or' statement |
|
109 |
a and (b or c) |
|
110 |
False |
|
91 | 111 |
|
98 | 112 |
We also have support for relational and logical operators. Lets try some |
113 |
examples: |
|
114 |
We start with initializing three variables by: |
|
115 |
p, z, n = 1, 0, -1 |
|
116 |
To check equivalency of two variables use '==' |
|
117 |
p == z |
|
118 |
False |
|
119 |
p >= n |
|
120 |
True |
|
121 |
||
122 |
We can check for multiple logical operations in one statement itself. |
|
123 |
n < z < p |
|
124 |
True. |
|
125 |
This statement checks if 'z' is smaller then 'p' and greater then 'n' |
|
126 |
For inequality testing we use '!' |
|
127 |
p + n != z will add 'p' and 'n' and check the equivalency with z |
|
128 |
||
129 |
We have already covered briefly in some of the previous sessions, |
|
130 |
conversion of data among different types. |
|
131 |
int(17 / 2.0) will convert result to integer type and we get |
|
132 |
8 as answer and not 8.5 |
|
133 |
But if we try something like |
|
134 |
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. |
|
136 |
To rounding off a floating number to a given precision 'round' function |
|
137 |
can be used. |
|
138 |
round(7.5) returns |
|
139 |
8. |
|
140 |
||
141 |
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 |
|
143 |
supported data types, supported operations and operators. How to convert |
|
144 |
one data type to other. |
|
91 | 145 |
|
146 |
*** Notes |