24 \begin{center} |
24 \begin{center} |
25 \LARGE{Python: Basics}\\ |
25 \LARGE{Python: Basics}\\ |
26 \large{FOSSEE} |
26 \large{FOSSEE} |
27 \end{center} |
27 \end{center} |
28 \section{Data types} |
28 \section{Data types} |
29 Complex Numbers |
29 \subsection{int and float} |
|
30 A whole number is a \typ{int} variable. |
30 \begin{lstlisting} |
31 \begin{lstlisting} |
31 In []: c = 3+4j |
32 In []: a = 13 |
32 In []: abs(c) |
33 In []: type(a) |
|
34 Out[]: <type 'int'> |
|
35 In []: b = -2 |
|
36 In []: type(b) |
|
37 Out[]: <type 'int'> |
|
38 In []: c = 500000000 |
|
39 In []: type(c) |
|
40 Out[]: <type 'int'> |
|
41 \end{lstlisting} |
|
42 A number with decimal is a \typ{float}. |
|
43 \begin{lstlisting} |
|
44 In []: p = 3.141592 |
|
45 In []: type(p) |
|
46 Out[]: <type 'float'> |
|
47 \end{lstlisting} |
|
48 \subsection{Complex Numbers} |
|
49 \begin{lstlisting} |
|
50 In []: c = 3+4j #coeff of j specifies imaginary part |
|
51 In []: abs(c) #absolute value of complex number |
33 Out[]: 5.0 |
52 Out[]: 5.0 |
34 In []: c.imag |
53 In []: c.imag #accessing imaginary part of c |
35 Out[]: 4.0 |
54 Out[]: 4.0 |
36 In []: c.real |
55 In []: c.real #accessing real part of c |
37 Out[]: 3.0 |
56 Out[]: 3.0 |
38 \end{lstlisting} |
57 \end{lstlisting} |
39 Boolean |
58 \newpage |
|
59 \subsection{Boolean} |
40 \begin{lstlisting} |
60 \begin{lstlisting} |
41 In []: a = False |
61 In []: a = False |
42 In []: b = True |
62 In []: b = True |
43 In []: c = True |
63 In []: c = True |
44 In []: (a and b) or c |
64 In []: (a and b) or c #Boolean operations |
45 Out[]: True |
65 Out[]: True |
46 \end{lstlisting} |
66 \end{lstlisting} |
47 Strings |
67 \textbf{Note:} Python is case sensitive language, \typ{True} is \typ{bool} type, but \typ{true} would be a variable. and hence following assignment fails:\\ |
|
68 \typ{In []: a = true}\\ |
|
69 \subsection{Strings} |
48 \begin{lstlisting} |
70 \begin{lstlisting} |
49 In []: w = "hello" |
71 In []: w = "hello" #w is string variable |
50 In []: print w[0] + w[2] + w[-1] |
72 In []: print w[1] |
51 Out[]: hlo |
73 Out[]: e |
52 In []: len(w) |
74 In []: print w[-1] #last character of string |
|
75 Out[]: o |
|
76 \end{lstlisting} |
|
77 \textbf{Note:} For a string variable, individual elements can be accessed using indices. |
|
78 \begin{lstlisting} |
|
79 In []: len(w) #function to calculate length of string |
53 Out[]: 5 |
80 Out[]: 5 |
54 In []: w[0] = 'H' # ERROR: Strings are immutable |
81 In []: w[0] = 'H' # ERROR: Strings are immutable |
55 \end{lstlisting} |
82 \end{lstlisting} |
56 String methods |
83 \subsection{String methods} |
57 \begin{lstlisting} |
84 \begin{lstlisting} |
58 In []: a = 'Hello World' |
85 In []: a = 'Hello World' |
59 In []: a.startswith('Hell') # 'a' starts with 'Hell' |
86 In []: a.startswith('Hell') # 'a' starts with 'Hell' |
|
87 Out[]: True |
60 In []: a.endswith('ld') # 'a' ends with 'ld' |
88 In []: a.endswith('ld') # 'a' ends with 'ld' |
|
89 Out[]: True |
61 In []: a.upper() # all characters to upper case |
90 In []: a.upper() # all characters to upper case |
|
91 Out[]: 'HELLO WORLD' |
62 In []: a.lower() # all characters to lower case |
92 In []: a.lower() # all characters to lower case |
|
93 Out[]: 'hello world' |
63 In []: ''.join(['a', 'b', 'c']) |
94 In []: ''.join(['a', 'b', 'c']) |
64 Out[]: 'abc' |
95 Out[]: 'abc' |
65 \end{lstlisting} |
96 \end{lstlisting} |
66 String formatting |
97 \typ{join} function joins all the list member passed as argument with the string it is called upon. In above case it is \typ{empty string}. |
|
98 \begin{lstlisting} |
|
99 In []: ' '.join(['a','b','c']) |
|
100 Out[]: 'a b c' |
|
101 In []: ','.join(['a','b','c']) |
|
102 Out[]: 'a,b,c' |
|
103 \end{lstlisting} |
|
104 \subsection{String formatting} |
67 \begin{lstlisting} |
105 \begin{lstlisting} |
68 In []: x, y = 1, 1.234 |
106 In []: x, y = 1, 1.234 #initializing two variables |
69 In []: 'x is %s, y is %s' %(x, y) |
107 In []: 'x is %s, y is %s' %(x, y) |
70 Out[]: 'x is 1, y is 1.234' |
108 Out[]: 'x is 1, y is 1.234' |
71 \end{lstlisting} |
109 \end{lstlisting} |
72 Arithmetic Operators |
110 \textbf{Note:} \typ{\%s} used in above fomatting specifies \typ{'str'} representation of variables. One can also try:\\ |
|
111 \typ{\%d} for \typ{int} representation\\ |
|
112 \typ{\%f} for \typ{float} representation |
|
113 \begin{lstlisting} |
|
114 In []: 'x is %f, y is %f' %(x, y) |
|
115 Out[]: 'x is 1.000000, y is 1.234000' |
|
116 |
|
117 In []: 'x is %d, y is %d' %(x, y) |
|
118 Out[]: 'x is 1, y is 1' |
|
119 \end{lstlisting} |
|
120 \subsection{Arithmetic Operators} |
73 \begin{lstlisting} |
121 \begin{lstlisting} |
74 In []: 45 % 2 # Modulo operator |
122 In []: 45 % 2 # Modulo operator |
75 Out[]: 1 |
123 Out[]: 1 |
76 In []: 1234567891234567890 ** 3 # Power |
124 In []: 5 ** 3 # Power |
|
125 Out[]: 125 |
77 In []: a = 5 |
126 In []: a = 5 |
78 In []: a += 1 |
127 In []: a += 1 #increment by 1, translates to a = a + 1 |
79 In []: a *= 2 |
128 In []: a *= 2 |
80 \end{lstlisting} |
129 \end{lstlisting} |
81 String Operations |
130 \subsection{String Operations} |
82 \begin{lstlisting} |
131 \begin{lstlisting} |
83 In []: s = 'Hello' |
132 In []: s = 'Hello' |
84 In []: p = 'World' |
133 In []: p = 'World' |
85 In []: s + p |
134 In []: s + p #concatenating two strings |
86 Out[]: 'HelloWorld' |
135 Out[]: 'HelloWorld' |
87 In []: s * 4 |
136 In []: s * 4 #repeating string for given num of times |
88 Out[]: 'HelloHelloHelloHello' |
137 Out[]: 'HelloHelloHelloHello' |
89 \end{lstlisting} |
138 \end{lstlisting} |
90 Relational and Logical Operators |
139 \subsection{Relational and Logical Operators} |
91 \begin{lstlisting} |
140 \begin{lstlisting} |
92 In []: p, z, n = 1, 0, -1 |
141 In []: p, z, n = 1, 0, -1 #initializing three variables |
93 In []: p == n |
142 In []: p == n #equivalency check |
94 Out[]: False |
143 Out[]: False |
95 In []: p >= n |
144 In []: p >= n |
96 Out[]: True |
145 Out[]: True |
97 In []: n < z < p |
146 In []: n < z < p #finding largest number among three |
98 Out[]: True |
147 Out[]: True |
99 In []: p + n != z |
148 In []: p + n != z |
100 Out[]: False |
149 Out[]: False |
101 \end{lstlisting} |
150 \end{lstlisting} |
102 Built-ins |
151 \subsection{Built-ins} |
103 \begin{lstlisting} |
152 \begin{lstlisting} |
104 In []: int(17 / 2.0) |
153 In []: int(17 / 2.0) #converts arguments to integer |
105 Out[]: 8 |
154 Out[]: 8 |
106 In []: float(17 / 2) |
155 In []: float(17 / 2) #argument is already integer(17 / 2 = 8) |
107 Out[]: 8.0 |
156 Out[]: 8.0 |
108 In []: str(17 / 2.0) |
157 In []: str(17 / 2.0) #converts to string |
109 Out[]: '8.5' |
158 Out[]: '8.5' |
110 In []: round( 7.5 ) |
159 In []: round( 7.5 ) |
111 Out[]: 8.0 |
160 Out[]: 8.0 |
112 \end{lstlisting} |
161 \end{lstlisting} |
113 Console Input |
162 \subsection{Console Input} |
114 \begin{lstlisting} |
163 \begin{lstlisting} |
115 In []: a = raw_input('Enter a value: ') |
164 In []: a = raw_input('Enter a value: ') |
116 Enter a value: 5 |
165 Enter a value: 5 |
117 \end{lstlisting} |
166 \end{lstlisting} |
|
167 \textbf{Note:} \typ{raw_input} always returns string representation of user input and hence: |
|
168 \begin{lstlisting} |
|
169 In []: type(a) |
|
170 Out[]: <type 'str'> |
|
171 \end{lstlisting} |
|
172 To get integer or floating point of this input, one has to perform type conversion:\\ |
|
173 \typ{In []: b = int(a)} |
118 \section{Conditionals} |
174 \section{Conditionals} |
119 \typ{if} |
175 \typ{if} |
120 \begin{lstlisting} |
176 \begin{lstlisting} |
121 In []: x = int(raw_input("Enter an integer:")) |
177 In []: x = int(raw_input("Enter an integer:")) |
122 In []: if x < 0: |
178 In []: if x < 0: |