176
|
1 |
Hello friends and welcome to the tutorial on Basic Data types and
|
|
2 |
operators in Python.
|
|
3 |
{{{ Show the slide containing title }}}
|
|
4 |
|
|
5 |
{{{ Show the slide containing the outline slide }}}
|
|
6 |
|
186
|
7 |
In this tutorial, we shall look at::
|
176
|
8 |
|
|
9 |
* Various Datatypes in Python
|
|
10 |
* Operators with a little hands-on on how they can be applied to
|
|
11 |
the different data types.
|
|
12 |
|
|
13 |
Since this a hands on session, you will require python installed in your
|
|
14 |
computer.
|
186
|
15 |
.. #[Nishanth]: this line is not required
|
176
|
16 |
|
|
17 |
First we will explore python data structures in the domain of numbers.
|
|
18 |
There are three built-in data structures in python to represent numbers.
|
|
19 |
|
186
|
20 |
.. #[Nishanth]: Did you mean data types when you said data structures??
|
|
21 |
Data structures is used for lists and others.
|
|
22 |
|
176
|
23 |
{{{ A slide to make a memory note of this }}}
|
|
24 |
|
|
25 |
These are:
|
|
26 |
|
|
27 |
* Integers
|
|
28 |
* Complex and
|
|
29 |
* Boolean
|
|
30 |
|
|
31 |
Lets first talk about integers. ::
|
|
32 |
|
186
|
33 |
a = 13
|
|
34 |
|
|
35 |
.. #[Nishanth]: give a space before and after the = sign
|
176
|
36 |
|
186
|
37 |
Thats it, there we have our first integer variable a.
|
176
|
38 |
|
186
|
39 |
.. #[Nishanth]: Show the value of a
|
176
|
40 |
|
|
41 |
If we now see ::
|
|
42 |
|
186
|
43 |
type(a)
|
|
44 |
<type 'int'>
|
176
|
45 |
|
186
|
46 |
This means that a is a type of int. Being an int data structure
|
176
|
47 |
in python means that there are various functions that this variable
|
|
48 |
has to manipulate it different ways. You can explore these by doing,
|
|
49 |
|
186
|
50 |
a.<Tab>
|
176
|
51 |
|
186
|
52 |
.. #[Nishanth]: a.<Tab> is not a good idea for int or float
|
176
|
53 |
|
|
54 |
Lets see the limits of this int.
|
|
55 |
|
186
|
56 |
b = 99999999999999999999
|
|
57 |
b
|
176
|
58 |
|
|
59 |
As you can see even when we put a value of 9 repeated 20 times
|
|
60 |
python did not complain. However when you asked python to print
|
|
61 |
the number again it put a capital L at the end. Now if you check
|
|
62 |
the type of this variable b, ::
|
|
63 |
|
186
|
64 |
type(b)
|
176
|
65 |
<type 'long'>
|
|
66 |
|
|
67 |
|
|
68 |
The reason for this is that python recognizes large integer numbers
|
186
|
69 |
by the data type long. However long type and integer type share there
|
176
|
70 |
functions and properties.
|
|
71 |
|
|
72 |
Lets now try out the second type in list called float.
|
|
73 |
|
|
74 |
Decimal numbers in python are recognized by the term float ::
|
|
75 |
|
186
|
76 |
p = 3.141592
|
|
77 |
p
|
176
|
78 |
|
|
79 |
If you notice the value of output of p isn't exactly equal to p. This
|
|
80 |
is because computer saves floating point values in a specific
|
186
|
81 |
format. There is always an aproximationation. This is why we should
|
176
|
82 |
never rely on equality of floating point numbers in a program.
|
|
83 |
|
|
84 |
The last data structure in the list is complex number ::
|
|
85 |
|
186
|
86 |
c = 3.2+4.6j
|
176
|
87 |
|
|
88 |
as simple as that so essentialy its just a combination of two floats the
|
|
89 |
imaginary part being define by j notation usually used in electrical
|
|
90 |
engineering. Complex numbers have a lot of functions specific to them.
|
|
91 |
Lets check these ::
|
|
92 |
|
186
|
93 |
c.<Tab>
|
|
94 |
|
|
95 |
.. #[Nishanth]: rephrase the "j used in electrical engineering"
|
|
96 |
Its ok if you skip it also. Just say that here
|
|
97 |
j is used and not i
|
176
|
98 |
|
|
99 |
Lets try some of them ::
|
|
100 |
|
186
|
101 |
c.real
|
|
102 |
c.imag
|
176
|
103 |
|
|
104 |
c.real gives the real part of the number and c.imag the imaginary.
|
|
105 |
|
|
106 |
We can get the absolute value using the function ::
|
|
107 |
|
186
|
108 |
abs(c)
|
176
|
109 |
|
|
110 |
Python also has Boolean as a built-in type.
|
|
111 |
|
|
112 |
Try it out just type ::
|
|
113 |
|
186
|
114 |
t = True
|
176
|
115 |
|
|
116 |
note that T in true is capitalized.
|
|
117 |
|
|
118 |
You can apply different Boolean operations on t now for example ::
|
|
119 |
|
186
|
120 |
f = not t
|
176
|
121 |
In[]: f
|
|
122 |
In[]: f or t
|
|
123 |
In[]: f and t
|
186
|
124 |
|
|
125 |
.. #[Nishanth]: remove In[]: and include spaces before and after = symbol
|
|
126 |
I don't want to edit it everywhere in the script
|
176
|
127 |
|
|
128 |
The results explanotary in themselves.
|
|
129 |
|
|
130 |
The usage of boolean brings us to an interesting question of precendence.
|
|
131 |
What if you want to apply one operator before another.
|
|
132 |
|
186
|
133 |
Well you can use parenthesis for precedence.
|
176
|
134 |
|
|
135 |
Lets write some piece of code to check this out.
|
|
136 |
|
|
137 |
In[]: a=False
|
|
138 |
In[]: b=True
|
|
139 |
In[]: c=True
|
|
140 |
|
|
141 |
To check how precedence changes with parenthesis. We will try two
|
|
142 |
expressions and their evaluation.
|
|
143 |
|
|
144 |
one ::
|
|
145 |
|
|
146 |
In[]: (a and b) or c
|
|
147 |
|
|
148 |
This expression gives the value True
|
|
149 |
|
|
150 |
where as the expression ::
|
|
151 |
|
|
152 |
In[]: a and (b or c)
|
|
153 |
|
|
154 |
gives the value False.
|
|
155 |
|
|
156 |
Lets now discuss sequence data structures in python. Sequence
|
|
157 |
datatypes are those in which elements are kept in a sequential
|
186
|
158 |
order. All the elements accessed using index.
|
176
|
159 |
|
|
160 |
{{{ slide to for memory aid }}}
|
|
161 |
|
|
162 |
The sequence datatypes in python are ::
|
186
|
163 |
|
176
|
164 |
* list
|
|
165 |
* string
|
|
166 |
* tuple
|
|
167 |
|
|
168 |
The list type is a container that holds a number of other
|
|
169 |
objects, in the given order.
|
|
170 |
|
|
171 |
We create our first list by typing ::
|
|
172 |
|
|
173 |
In[]: num = [1, 2, 3, 4]
|
|
174 |
|
186
|
175 |
.. #[Nishanth]: Show the value of the variable
|
176
|
176 |
Items enclosed in square brackets separated by comma
|
|
177 |
constitutes a list.
|
|
178 |
|
186
|
179 |
Lists can store data of any type in them.
|
176
|
180 |
|
|
181 |
We can have a list something like ::
|
186
|
182 |
|
176
|
183 |
In[]: var = [1, 1.2, [1,2]]
|
186
|
184 |
|
|
185 |
.. #[Nishanth]: Show the value of the variable
|
176
|
186 |
print var
|
|
187 |
|
|
188 |
Now we will have a look at strings
|
|
189 |
|
|
190 |
type ::
|
|
191 |
|
|
192 |
In[]: w="hello"
|
|
193 |
|
186
|
194 |
.. #[Nishanth]: Show the value of the variable
|
176
|
195 |
w is now a string variable with the value "hello"
|
|
196 |
|
|
197 |
{{{ Memory Aid Slide }}}
|
|
198 |
|
|
199 |
Python strings can actually be defined in three different ways ::
|
|
200 |
|
|
201 |
In[]: k='Single quote'
|
|
202 |
In[]: l="Double quote contain's single quote"
|
|
203 |
In[]: m='''"Contain's both"'''
|
|
204 |
|
186
|
205 |
Thus, single quotes are used as delimiters usually.
|
|
206 |
When a string contains a single quote, double quotes are used as delimiters.
|
|
207 |
When a string quote contains both single and double quotes, triple quotes are
|
|
208 |
used as delimiters.
|
176
|
209 |
|
|
210 |
The last in the list of sequence data types is tuple.
|
|
211 |
|
|
212 |
To create a tuple we use normal brackets '('
|
|
213 |
unlike '[' for lists.::
|
|
214 |
|
|
215 |
In[]: t = (1, 2, 3, 4, 5, 6, 7, 8)
|
|
216 |
|
|
217 |
Because of their sequential property there are certain functions and
|
|
218 |
operations we can apply to all of them.
|
|
219 |
|
|
220 |
{{{ Slide for memory aid }}}
|
|
221 |
|
|
222 |
The first one is accessing.
|
|
223 |
|
|
224 |
They can be accessed using index numbers ::
|
|
225 |
|
|
226 |
In[]: num[2]
|
|
227 |
In[]: num[-1]
|
|
228 |
In[]: w[1]
|
|
229 |
In[]: w[3]
|
|
230 |
In[]: w[-2]
|
|
231 |
In[]: t[2]
|
|
232 |
In[]: t[-3]
|
|
233 |
|
|
234 |
Negative indices can be used to access in reverse
|
|
235 |
|
186
|
236 |
.. #[Nishanth]: Elaborate on indexing.
|
|
237 |
Indexing starts from 0 when moving from left to right
|
|
238 |
Indexing starts from -1 when moving from right to left
|
|
239 |
|
176
|
240 |
Addition gives a new sequence containing both sequences ::
|
|
241 |
|
|
242 |
In[]: num+var
|
|
243 |
In[]: p="another string"
|
|
244 |
In[]: w+p
|
|
245 |
In[]: t2=(3,4,6,7)
|
|
246 |
In[]: t+t2
|
|
247 |
|
|
248 |
len function gives the length ::
|
|
249 |
|
|
250 |
In[]: len(num)
|
|
251 |
In[]: len(w)
|
|
252 |
In[]: len(t)
|
|
253 |
|
186
|
254 |
Prints the length the variable.
|
|
255 |
|
|
256 |
We can check the containership of an element using the 'in' keyword ::
|
176
|
257 |
|
|
258 |
In[]: 3 in num
|
|
259 |
In[]: 'H' in w
|
|
260 |
In[]: 2 in t
|
|
261 |
|
186
|
262 |
We see that it gives True and False accordingly.
|
|
263 |
|
|
264 |
Find maximum using max function and minimum using min::
|
176
|
265 |
|
|
266 |
In[]: max(t)
|
|
267 |
In[]: min(w)
|
|
268 |
|
|
269 |
Get a sorted list and reversed list using sorted and reversed function ::
|
|
270 |
|
|
271 |
In[]: sorted(num)
|
|
272 |
In[]: reversed(w)
|
|
273 |
|
186
|
274 |
As a consequence of the order one we access a group of elements together.
|
|
275 |
This is called slicing and striding.
|
176
|
276 |
|
|
277 |
First Slicing
|
|
278 |
|
|
279 |
Given a list ::
|
|
280 |
|
|
281 |
In[]:j=[1,2,3,4,5,6]
|
|
282 |
|
186
|
283 |
Lets say we want elements starting from 2 and ending in 5.
|
176
|
284 |
|
|
285 |
For this we can do ::
|
|
286 |
|
|
287 |
In[]: j[1:4]
|
|
288 |
|
|
289 |
The syntax for slicing is sequence variable name square bracket
|
186
|
290 |
first element index, colon, second element index.::
|
|
291 |
.. #[nishanth]: specify that the last element is not included
|
176
|
292 |
|
|
293 |
In[]: j[:4]
|
|
294 |
|
|
295 |
If first element is left blank default is from beginning and if last
|
|
296 |
element is left blank it means till the end.
|
|
297 |
|
|
298 |
In[]: j[1:]
|
|
299 |
|
|
300 |
In[]: j[:]
|
|
301 |
|
|
302 |
This effectively is the whole list.
|
|
303 |
|
186
|
304 |
Striding is similar to slicing except that the step size here is not one.
|
176
|
305 |
|
|
306 |
Lets see by example ::
|
|
307 |
|
|
308 |
In[]: z=[1,2,3,4,5,6,7,8,9,10]
|
|
309 |
In[]: z[1:8:2]
|
|
310 |
Out[]:[2, 4, 6, 8]
|
|
311 |
|
186
|
312 |
The colon two added in the end signifies all the alternate elements. This is why we call this concept
|
176
|
313 |
striding because we move through the list with a particular stride or step. The step in this example
|
|
314 |
being 2.
|
|
315 |
|
186
|
316 |
We have talked about many similar features of lists, strings and tuples. But there are many important
|
|
317 |
features in lists that differ from strings and tuples. Lets see this by example.::
|
176
|
318 |
|
|
319 |
In[]: z[1]=9
|
|
320 |
In[]: w[1]='k'
|
|
321 |
|
186
|
322 |
{{{ slide to show the error }}}
|
176
|
323 |
|
186
|
324 |
.. #[Nishanth]: Use sensible variable names. At this point no one will remember
|
|
325 |
that z is a list and w is tuple.
|
|
326 |
for example you can use names like some_list, a_tuple etc.
|
|
327 |
or you can also use l for list, t for tuple and s for string
|
|
328 |
|
176
|
329 |
As you can see while the first command executes with out a problem there is an error on the second one.
|
|
330 |
|
|
331 |
Now lets try ::
|
186
|
332 |
|
176
|
333 |
In[]: t[1]=5
|
|
334 |
|
|
335 |
Its the same error. This is because strings and tuples share the property of being immutable.
|
|
336 |
We cannot change the value at a particular index just by assigning a new value at that position.
|
186
|
337 |
|
176
|
338 |
In case of strings we have special functions to appy relacement and other things while tuples cannot
|
|
339 |
be changed at all.
|
|
340 |
|
186
|
341 |
.. #[Nishanth]: Even in strings also the special functions do not modify the
|
|
342 |
original string. A new string is created instead. These have
|
|
343 |
been provided for string manipulation.
|
|
344 |
hence I don't think you have to mention this.
|
|
345 |
|
176
|
346 |
We have looked at different types but we need to convert one data type into another. Well lets one
|
|
347 |
by one go through methods by which we can convert one data type to other:
|
|
348 |
|
|
349 |
We can convert all the number data types to one another ::
|
|
350 |
|
|
351 |
In[]: i=34
|
|
352 |
In[]: d=float(i)
|
|
353 |
|
186
|
354 |
Python has built in functions int, float and complex to convert one number type
|
176
|
355 |
data structure to another.
|
|
356 |
|
|
357 |
In[]: dec=2.34
|
|
358 |
In[]: dec_con=int(dec)
|
|
359 |
|
186
|
360 |
.. #[Nishanth]: Show the value of the variables
|
|
361 |
|
|
362 |
As you can see the decimal part of the number is simply stripped to get the integer.::
|
176
|
363 |
|
|
364 |
In[]: com=2.3+4.2j
|
|
365 |
In[]: float(com)
|
186
|
366 |
|
|
367 |
In case of complex number to floating point only the real value of complex number is taken.
|
176
|
368 |
|
|
369 |
Similarly we can convert list to tuple and tuple to list ::
|
|
370 |
|
|
371 |
In[]: lst=[3,4,5,6]
|
|
372 |
In[]: tup=tuple(lst)
|
|
373 |
In[]: tupl=(3,23,4,56)
|
|
374 |
In[]: lst=list(tuple)
|
|
375 |
|
|
376 |
However string to list and list to string is an interesting problem.
|
|
377 |
Lets say we have a string ::
|
|
378 |
|
|
379 |
In: somestring="Is there a way to split on these spaces."
|
|
380 |
In: somestring.split()
|
|
381 |
|
186
|
382 |
.. #[Nishanth]: Did you try list(somestring). What does it give??
|
|
383 |
|
176
|
384 |
This produces a list with the string split at whitespace.
|
|
385 |
similarly we can split on some other character.
|
|
386 |
|
|
387 |
In: otherstring="Tim,Amy,Stewy,Boss"
|
|
388 |
|
|
389 |
How do we split on comma , simply pass it as argument ::
|
|
390 |
|
|
391 |
In: otherstring.split(',')
|
|
392 |
|
|
393 |
join function does the opposite. Joins a list to make a string.::
|
|
394 |
|
|
395 |
In[]:','.join['List','joined','on','commas']
|
|
396 |
|
|
397 |
Thus we get a list joined on commas. Similarly we can do spaces.::
|
|
398 |
|
|
399 |
In[]:' '.join['Now','on','spaces']
|
|
400 |
|
|
401 |
Note that the list has to be a list of strings to apply join operation.
|
|
402 |
|
186
|
403 |
.. #[Nishanth]: string to list is fine. But list to string can be left for
|
|
404 |
string manipulations. Just say it requires some string
|
|
405 |
manipulations and leave it there.
|
|
406 |
|
|
407 |
.. #[Nishanth]: Where is the summary
|
|
408 |
There are no exercises in the script
|
|
409 |
|
176
|
410 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
411 |
|
|
412 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
413 |
|
|
414 |
Hope you have enjoyed and found it useful.
|
|
415 |
|
|
416 |
Thank You.
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
Author : Amit Sethi
|
186
|
421 |
Internal Reviewer 1 : Nishanth
|
176
|
422 |
Internal Reviewer 2 :
|
|
423 |
External Reviewer
|