author | Puneeth Chaganti <punchagan@gmail.com> |
Thu, 22 Apr 2010 13:28:13 +0530 | |
changeset 104 | 5cfcbd65ff1d |
parent 102 | 84e1dcb52908 |
child 105 | 7722d269ff82 |
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' |
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
18 |
All integers are of 'int' data type, irrespective of how big they |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
19 |
are. 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 |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
28 |
print y |
91 | 29 |
|
98 | 30 |
Floating point numbers come under 'float' |
92 | 31 |
p = 3.141592 |
32 |
type(p) |
|
33 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
34 |
Python by default provides support for complex numbers. |
92 | 35 |
c = 3+4j |
98 | 36 |
creates a complex number c. Here 'j' is used to specify the imaginary |
37 |
part. |
|
92 | 38 |
type(c) |
39 |
Python also provides basic functions for their manipulations like |
|
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
40 |
abs(c) will return the absolute value of c. |
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. |
98 | 42 |
|
43 |
All the basic operators work with Python data types, without any |
|
44 |
surprises. When we try to add two numbers like x and y Python takes |
|
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
45 |
cares of returning 'right' answer |
98 | 46 |
x + y |
47 |
||
48 |
Same as additions multiplication also works just right: |
|
49 |
3124 * 126789 |
|
50 |
396088836 |
|
51 |
||
52 |
Division in Python truncates, that is, when we divide a integer |
|
53 |
variable with another integer result is also integer and decimal |
|
54 |
value is truncated. So |
|
55 |
17 / 2 returns 8 and not 8.5 |
|
56 |
||
57 |
but |
|
58 |
17 / 2.0 will return the correct 8.5, similarly |
|
59 |
17.0 / 2 will also give correct answer. |
|
60 |
||
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
61 |
x ** y returns x raised to power y. For example lets try: |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
62 |
big = 1234567891234567890 ** 3 |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
63 |
|
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
64 |
% operator is for modulo operations |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
65 |
1786 % 12 gives 10 |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
66 |
45 % 2 returns 1 |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
67 |
|
98 | 68 |
Other operators which comes handy are: |
69 |
+= |
|
70 |
lets create one variable a with |
|
71 |
a = 7546 |
|
72 |
now |
|
73 |
a += 1 will increment the value of 'a' by 1 |
|
74 |
similarly |
|
75 |
a -= 1 will decrement. |
|
76 |
we can also use |
|
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
77 |
a *= a |
98 | 78 |
a |
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
79 |
a is multiplied by itself. |
98 | 80 |
|
81 |
a /= 5 |
|
82 |
a is divided by 5 |
|
83 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
84 |
Next we will look at Boolean datatype: |
92 | 85 |
Its a primitive datatype having one of two values: True or False. |
86 |
t = True |
|
87 |
print t |
|
88 |
||
89 |
Python is case sensitive language, so True with 'T' is boolean type but |
|
90 |
true with 't' would be a variable. |
|
91 |
||
92 |
f = not True |
|
93 |
||
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
94 |
we can do binary operations like 'or', 'and', 'not' with these variables |
92 | 95 |
f or t |
96 |
f and t |
|
97 |
||
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
98 |
in case of multiple binary operations to make sure of precedence use |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
99 |
'parenthesis ()' |
92 | 100 |
a = False |
101 |
b = True |
|
102 |
c = True |
|
103 |
(a and b) or c |
|
104 |
True |
|
105 |
first a and b is evaluated and then the 'or' statement |
|
106 |
a and (b or c) |
|
107 |
False |
|
91 | 108 |
|
98 | 109 |
We also have support for relational and logical operators. Lets try some |
110 |
examples: |
|
111 |
We start with initializing three variables by: |
|
112 |
p, z, n = 1, 0, -1 |
|
113 |
To check equivalency of two variables use '==' |
|
114 |
p == z |
|
115 |
False |
|
116 |
p >= n |
|
117 |
True |
|
118 |
||
119 |
We can check for multiple logical operations in one statement itself. |
|
120 |
n < z < p |
|
121 |
True. |
|
122 |
This statement checks if 'z' is smaller then 'p' and greater then 'n' |
|
123 |
For inequality testing we use '!' |
|
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
124 |
p + n != z will add 'p' and 'n' and check the equivalence with z |
98 | 125 |
|
126 |
We have already covered briefly in some of the previous sessions, |
|
127 |
conversion of data among different types. |
|
128 |
int(17 / 2.0) will convert result to integer type and we get |
|
129 |
8 as answer and not 8.5 |
|
130 |
But if we try something like |
|
131 |
float(17 / 2) we get 8.0 as 17/2 is already truncated to int |
|
132 |
and converting that to float wont restore the lost decimal digits. |
|
104
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
133 |
To round off a float to a given precision 'round' function can be |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
134 |
used. |
5cfcbd65ff1d
minor edits to numbers org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
102
diff
changeset
|
135 |
round(7.5) returns 8. |
98 | 136 |
|
137 |
This brings us to the end of tutorial on introduction of Data types |
|
138 |
related to numbers in Python. In this tutorial we have learnt what are |
|
102 | 139 |
supported data types for numbers, operations and operators and how to |
140 |
convert one data type to other. Thank you! |
|
91 | 141 |
|
142 |
*** Notes |