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