6 ******* Arsenal Required |
6 ******* Arsenal Required |
7 ********* None |
7 ********* None |
8 *** Script |
8 *** Script |
9 Welcome friends. |
9 Welcome friends. |
10 |
10 |
11 In this tutorial we shall look at data types available in Python and |
11 In this tutorial we shall look at data types in Python and |
12 how to perform simple Input and Output operations. |
12 mathematical operators available. |
13 for 'Numbers' we have: int, float, complex datatypes |
13 For 'Numbers' we have: int, float, complex datatypes |
14 for Text content we have strings. |
|
15 For conditional statements, 'Booleans'. |
14 For conditional statements, 'Booleans'. |
16 |
15 |
17 Lets get started by opening IPython interpreter. |
16 Lets get started by opening IPython interpreter. |
18 Lets start with 'numbers' |
17 Lets start with 'numbers' |
19 All integers irrespective of how big they are, are of 'int' |
18 All integers irrespective of how big they are of 'int' data |
20 data type |
19 type. Now we will create a variable, say |
21 Now we will create a variable, say |
|
22 x = 13 |
20 x = 13 |
23 print x |
21 print x |
24 |
22 |
25 To check the data type of any variable Python provides 'type' function |
23 To check the data type of any variable Python provides 'type' function |
26 type(x) |
24 type(x) |
27 |
25 |
|
26 lets create one more variable |
28 y = 999999999999 |
27 y = 999999999999 |
|
28 (what about large value? and it is Long and not int, should we mention |
|
29 that?) |
29 print y |
30 print y |
30 |
31 |
31 Floating point numbers comes under 'float' |
32 Floating point numbers come under 'float' |
32 p = 3.141592 |
33 p = 3.141592 |
33 type(p) |
34 type(p) |
34 |
35 |
35 Python by default provides support for complex numbers. |
36 Python by default provides support for complex numbers. |
36 c = 3+4j |
37 c = 3+4j |
37 c is a complex number. 'j' is used to specify the imaginary part. |
38 creates a complex number c. Here 'j' is used to specify the imaginary |
|
39 part. |
38 type(c) |
40 type(c) |
39 Python also provides basic functions for their manipulations like |
41 Python also provides basic functions for their manipulations like |
40 abs(c) will return the absolute value of c(sqrt(a^2 + b^2)) |
42 abs(c) will return the absolute value of c(sqrt(a^2 + b^2)) |
41 c.imag returns imaginary part and c.real gives the real part. |
43 c.imag returns imaginary part and c.real gives the real part. |
|
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 |
42 |
54 |
|
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 |
43 Next we will look at Boolean datatype: |
87 Next we will look at Boolean datatype: |
44 Its a primitive datatype having one of two values: True or False. |
88 Its a primitive datatype having one of two values: True or False. |
45 t = True |
89 t = True |
46 print t |
90 print t |
47 |
91 |
48 Python is case sensitive language, so True with 'T' is boolean type but |
92 Python is case sensitive language, so True with 'T' is boolean type but |
49 true with 't' would be a variable. |
93 true with 't' would be a variable. |
50 |
94 |
51 f = not True |
95 f = not True |
52 |
96 |
53 we can do binary operation like 'or' and 'not' with these variables |
97 we can do binary operation like 'or', 'and', 'not' with these variables |
54 f or t |
98 f or t |
55 f and t |
99 f and t |
56 |
100 |
57 in case of multiple binary operations to make sure of precedence use |
101 in case of multiple binary operations to make sure of precedence use |
58 'brackets ()' |
102 'brackets ()' |
63 True |
107 True |
64 first a and b is evaluated and then the 'or' statement |
108 first a and b is evaluated and then the 'or' statement |
65 a and (b or c) |
109 a and (b or c) |
66 False |
110 False |
67 |
111 |
68 we come to the end of this tutorial on introduction of Data types in |
112 We also have support for relational and logical operators. Lets try some |
69 Python. In this tutorial we have learnt what are supported data types, |
113 examples: |
70 supported operations and performing simple IO operations in Python. |
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. |
71 |
145 |
72 *** Notes |
146 *** Notes |