author | Nishanth <nishanth@fossee.in> |
Wed, 06 Oct 2010 16:15:42 +0530 | |
changeset 221 | 7cd975ff5f0d |
parent 160 | f394adb5b00e |
permissions | -rw-r--r-- |
160
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
1 |
.. 3.2 LO: getting started with =for= (2) [anoop] |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
2 |
.. ----------------------------------------------- |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
3 |
.. * blocks in python |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
4 |
.. + (indentation) |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
5 |
.. * blocks in ipython |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
6 |
.. + ... prompt |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
7 |
.. + hitting enter |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
8 |
.. * =for= with a list |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
9 |
.. * =range= function |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
10 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
11 |
============================= |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
12 |
Getting started with for loop |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
13 |
============================= |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
14 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
15 |
{{{ show welcome slide }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
16 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
17 |
Hello and welcome to the tutorial getting started with ``for`` loop. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
18 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
19 |
{{{ switch to next slide, outline slide }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
20 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
21 |
In this tutorial we will see ``for`` loops in python, and also cover |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
22 |
the basics of indenting code in python. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
23 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
24 |
{{{ switch to next slide, about whitespaces }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
25 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
26 |
In Python whitespace is significant, and the blocks are visually |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
27 |
separated rather than using braces or any other mechanisms for |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
28 |
defining blocks. And by this method Python forces the programmers to |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
29 |
stick on to one way of writing or beautifying the code rather than |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
30 |
debating over where to place the braces. This way it produces uniform |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
31 |
code than obscure or unreadable code. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
32 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
33 |
A block may be defined by a suitable indentation level which can be |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
34 |
either be a tab or few spaces. And the best practice is to indent the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
35 |
code using four spaces. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
36 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
37 |
Now let us move straight into ``for`` loop. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
38 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
39 |
{{{ switch to next slide, problem statement of exercise 1 }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
40 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
41 |
Write a for loop which iterates through a list of numbers and find the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
42 |
square root of each number. Also make a new list with the square roots |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
43 |
and print it at the end. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
44 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
45 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
46 |
numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
47 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
48 |
For the problem, first we need to create a ``list`` of numbers and |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
49 |
then iterate over the list and find the square root of each element in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
50 |
it. And let us create a script, rather than typing it out in the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
51 |
interpreter itself. Create a script called list_roots.py and type the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
52 |
following. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
53 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
54 |
{{{ open the text editor and paste the following code there }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
55 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
56 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
57 |
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
58 |
square_roots = [] |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
59 |
for each in numbers: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
60 |
sq_root = sqrt(each) |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
61 |
print "Square root of", each, "is", sq_root |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
62 |
square_roots.append(sq_root) |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
63 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
64 |
print square_roots |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
65 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
66 |
{{{ save the script }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
67 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
68 |
Now save the script, and run it from your IPython interpreter. I |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
69 |
assume that you have started your IPython interpreter using ``-pylab`` |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
70 |
option. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
71 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
72 |
Run the script as, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
73 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
74 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
75 |
%run -i list_roots.py |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
76 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
77 |
{{{ run the script }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
78 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
79 |
So that was easy! We didn't have to find the length of the string nor |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
80 |
address of each element of the list one by one. All what we did was |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
81 |
iterate over the list element by element and then use the element for |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
82 |
calculation. Note that here we used three variables. One the variable |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
83 |
``numbers``, which is a list, another one ``each``, which is the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
84 |
element of list under consideration in each cycle of the ``for`` loop, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
85 |
and then a variable ``sq_root`` for storing the square root in each |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
86 |
cycle of the ``for`` loop. The variable names can be chosen by you. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
87 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
88 |
{{{ show the script which was created }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
89 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
90 |
Note that three lines after ``for`` statement, are indented using four |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
91 |
spaces. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
92 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
93 |
{{{ highlight the threee lines after for statement }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
94 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
95 |
It means that those three lines are part of the for loop. And it is |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
96 |
called a block of statements. And the seventh line or the immediate |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
97 |
line after the third line in the ``for`` loop is not indented, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
98 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
99 |
{{{ highlight the seventh line - the line just after for loop }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
100 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
101 |
it means that it is not part of the ``for`` loop and the lines after |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
102 |
that doesn't fall in the scope of the ``for`` loop. Thus each block is |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
103 |
separated by the indentation level. Thus marking the importance of |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
104 |
white-spaces in Python. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
105 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
106 |
{{{ switch to the slide which shows the problem statement of the first |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
107 |
problem to be tried out }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
108 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
109 |
Now a question for you to try, from the given numbers make a list of |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
110 |
perfect squares and a list of those which are not. The numbers are, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
111 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
112 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
113 |
7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916 |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
114 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
115 |
{{{ switch to next slide, problem statement of second problem in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
116 |
solved exercie}}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
117 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
118 |
Now let us try a simple one, to print the square root of numbers in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
119 |
the list. And this time let us do it right in the IPython |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
120 |
interpreter. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
121 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
122 |
{{{ switch focus to the IPython interpreter }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
123 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
124 |
So let us start with making a list. Type the following |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
125 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
126 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
127 |
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
128 |
for each in numbers: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
129 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
130 |
and now you will notice that, as soon as you press the return key |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
131 |
after for statement, the prompt changes to four dots and the cursor is |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
132 |
not right after the four dots but there are four spaces from the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
133 |
dots. The four dots tell you that you are inside a block. Now type the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
134 |
rest of the ``for`` loop, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
135 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
136 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
137 |
sq_root = sqrt(each) |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
138 |
print "Square root of", each, "is", sq_root |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
139 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
140 |
Now we have finished the statements in the block, and still the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
141 |
interpreter is showing four dots, which means you are still inside the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
142 |
block. To exit from the block press return key or the enter key twice |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
143 |
without entering anything else. It printed the square root of each |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
144 |
number in the list, and that is executed in a ``for`` loop. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
145 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
146 |
Now, let us generate the multiplication table of 10 from one to |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
147 |
ten. But this time let us try it in the vanilla version of Python |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
148 |
interpreter. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
149 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
150 |
Start the vanilla version of Python interpreter by issuing the command |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
151 |
``python`` in your terminal. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
152 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
153 |
{{{ open the python interpreter in the terminal using the command |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
154 |
python to start the vanilla Python interpreter }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
155 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
156 |
Start with, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
157 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
158 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
159 |
for i in range(1,11): |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
160 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
161 |
and press enter once, and we will see that this time it shows four |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
162 |
dots, but the cursor is close to the dots, so we have to intend the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
163 |
block. So enter four spaces there and then type the following |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
164 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
165 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
166 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
167 |
print "10 *",i,"=",i*10 |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
168 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
169 |
Now when we hit enter, we still see the four dots, to get out of the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
170 |
block type enter once. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
171 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
172 |
Okay! so the main thing here we learned is how to use Python |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
173 |
interpreter and IPython interpreter to specify blocks. But while we |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
174 |
were generating the multiplication table we used something new, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
175 |
``range()`` function. ``range()`` is an inbuilt function in Python |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
176 |
which can be used to generate a ``list`` of integers from a starting |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
177 |
range to an ending range. Note that the ending number that you specify |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
178 |
will not be included in the ``list``. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
179 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
180 |
Now, let us print all the odd numbers from 1 to 50. Let us do it in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
181 |
our IPython interpreter for ease of use. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
182 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
183 |
{{{ switch focus to ipython interpreter }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
184 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
185 |
{{{ switch to next slide, problem statement of the next problem in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
186 |
solved exercises }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
187 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
188 |
Print the list of odd numbers from 1 to 50. It will be better if |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
189 |
you can try it out yourself. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
190 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
191 |
It is a very trivial problem and can be solved as, |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
192 |
:: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
193 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
194 |
print range(1,51,2) |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
195 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
196 |
This time we passed three parameters to ``range()`` function unlike |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
197 |
the previous case where we passed only two parameters. The first two |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
198 |
parameters are the same in both the cases. The first parameter is the |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
199 |
starting number of the sequence and the second parameter is the end of |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
200 |
the range. Note that the sequence doesn't include the ending |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
201 |
number. The third parameter is for stepping through the sequence. Here |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
202 |
we gave two which means we are skipping every alternate element. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
203 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
204 |
{{{ switch to next slide, recap slide }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
205 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
206 |
Thus we come to the end of this tutorial. We learned about blocks in |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
207 |
Python, indentation, blocks in IPython, for loop, iterating over a |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
208 |
list and then the ``range()`` function. |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
209 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
210 |
{{{ switch to next slide, thank you slide }}} |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
211 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
212 |
Thank you! |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
213 |
|
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
214 |
.. Author: Anoop Jacob Thomas <anoop@fossee.in> |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
215 |
Reviewer 1: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
216 |
Reviewer 2: |
f394adb5b00e
added savefig, dictionaries, getting_started_with_for and other_type_of_plots.
anoop
parents:
diff
changeset
|
217 |
External reviewer: |