author | Puneeth Chaganti <punchagan@fossee.in> |
Thu, 07 Oct 2010 12:28:12 +0530 | |
changeset 242 | a33e942379d7 |
parent 206 | 8835b2c071e6 |
permissions | -rw-r--r-- |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
1 |
.. 3.2 LO: getting started with =for= (2) [anoop] |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
2 |
.. ----------------------------------------------- |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
3 |
.. * blocks in python |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
4 |
.. + (indentation) |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
5 |
.. * blocks in ipython |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
6 |
.. + ... prompt |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
7 |
.. + hitting enter |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
8 |
.. * =for= with a list |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
9 |
.. * =range= function |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
10 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
11 |
============================= |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
12 |
Getting started with for loop |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
13 |
============================= |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
14 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
15 |
{{{ show welcome slide }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
16 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
17 |
Hello and welcome to the tutorial getting started with ``for`` loop. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
18 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
19 |
{{{ switch to next slide, outline slide }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
20 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
21 |
In this tutorial we will learn about ``for`` loops in python, and also |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
22 |
learn how to write blocks of code in Python. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
23 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
24 |
.. #[Nishanth]: Instead of saying basics of indenting code, |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
25 |
say How to define code blocks in Python |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
26 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
27 |
{{{ switch to next slide, about whitespaces }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
28 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
29 |
In Python whitespace is significant, and the blocks are visually |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
30 |
separated. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
31 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
32 |
.. #[nishanth]: Simply tell how blocks are defined in python. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
33 |
The details like braces are not used and its |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
34 |
advantages like neat code can be told after completely |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
35 |
explaining the indentation |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
36 |
|
202 | 37 |
.. #[Amit]: Do you want to do that here. May be its better to talk about |
38 |
this after some initiation into the idea of blocks. |
|
39 |
||
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
40 |
The best practice is to indent the code using four spaces. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
41 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
42 |
.. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
43 |
for indentation. Do that while typing so that they can |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
44 |
actually see what is being typed. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
45 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
46 |
Now let us move straight into ``for`` loop. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
47 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
48 |
{{{ switch to next slide, problem statement of exercise 1 }}} |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
49 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
50 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
51 |
Write a for loop which iterates through a list of numbers and find the |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
52 |
square root of each number. |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
53 |
:: |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
54 |
|
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
55 |
numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 |
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
56 |
|
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
57 |
.. #[nishanth]: making new list with square roots induces extra complication |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
58 |
like appending which has no use case here |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
59 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
60 |
.. #[Nishanth]: The problem focuses more on square root and creation |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
61 |
of list. The problem must be simple and focusing on |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
62 |
nothing more but the indentation and for loop. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
63 |
May be change the problem to print squares than to |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
64 |
print square roots. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
65 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
66 |
For the problem, first we need to create a ``list`` of numbers and |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
67 |
then iterate over the list and find the square root of each element in |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
68 |
it. And let us create a script, rather than typing it out in the |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
69 |
interpreter itself. Create a script called list_roots.py and type the |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
70 |
following. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
71 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
72 |
{{{ open the text editor and paste the following code there }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
73 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
74 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
75 |
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
76 |
for each in numbers: |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
77 |
print "Square root of", each, "is", sqrt(each) |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
78 |
print "This is not in for loop!" |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
79 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
80 |
.. numbers = [1, 12, 3, 4, 21, 17] |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
81 |
for each in numbers: |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
82 |
print each, each * each |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
83 |
|
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
84 |
.. #[nishanth]: I don't see a use case to append the sq_root to |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
85 |
square_roots. It is only complicating stuff. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
86 |
Simply iterate and print. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
87 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
88 |
{{{ save the script }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
89 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
90 |
Now save the script, and run it from your IPython interpreter. I |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
91 |
assume that you have started your IPython interpreter using ``-pylab`` |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
92 |
option. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
93 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
94 |
Run the script as, |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
95 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
96 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
97 |
%run -i list_roots.py |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
98 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
99 |
.. #[Nishanth]: you don't have to use the -i option here |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
100 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
101 |
{{{ run the script }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
102 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
103 |
So that was easy! All what we did was iterate over the list element by |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
104 |
element and then use the element for calculation. Note that here we |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
105 |
used two variables. One the variable ``numbers``, which is a list, |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
106 |
another one ``each``, which is the element of list under consideration |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
107 |
in each cycle of the ``for`` loop. The variable names can be chosen by |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
108 |
you. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
109 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
110 |
.. #[Nishanth]: The details like we didn't have to find the length |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
111 |
are relevant for people who have programmed in C or |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
112 |
other languages earlier. But for a newbie it is more |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
113 |
of confusing extra info. That part may be skipped. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
114 |
Simply go ahead and focus on the syntax of for loop. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
115 |
And how the variable name is used inside the for loop. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
116 |
If you modify the question to only print, the extra |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
117 |
variable sq_root can also be avoided. let it be more |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
118 |
about "each", "numbers" and "for". no other new names. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
119 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
120 |
{{{ show the script which was created }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
121 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
122 |
Note that the lines after ``for`` statement, is indented using four |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
123 |
spaces. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
124 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
125 |
{{{ highlight the line after for statement }}} |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
126 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
127 |
It means that line is part of the for loop. And it is a block of code, |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
128 |
although it is only a single statement in the block. And the fourth |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
129 |
line or the immediate line after the ``for`` block is not indented, |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
130 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
131 |
{{{ highlight the fourth line - the line just after for loop }}} |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
132 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
133 |
it means that it is not part of the ``for`` loop and the lines after |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
134 |
that doesn't fall in the scope of the ``for`` loop. Thus each block is |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
135 |
separated by the indentation level. Thus marking the importance of |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
136 |
white-spaces in Python. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
137 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
138 |
{{{ switch to the slide which shows the problem statement of the first |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
139 |
problem to be tried out }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
140 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
141 |
Now a question for you to try, from the given numbers make a list of |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
142 |
perfect squares and a list of those which are not. The numbers are, |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
143 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
144 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
145 |
7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916 |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
146 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
147 |
{{{ switch to next slide, problem statement of second problem in |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
148 |
solved exercise}}} |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
149 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
150 |
Now let us try a simple one, to print the square root of numbers in |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
151 |
the list. And this time let us do it right in the IPython |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
152 |
interpreter. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
153 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
154 |
{{{ switch focus to the IPython interpreter }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
155 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
156 |
So let us start with making a list. Type the following |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
157 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
158 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
159 |
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
160 |
for each in numbers: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
161 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
162 |
and now you will notice that, as soon as you press the return key |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
163 |
after for statement, the prompt changes to four dots and the cursor is |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
164 |
not right after the four dots but there are four spaces from the |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
165 |
dots. Please note that IPython automatically indents the block. The |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
166 |
four dots tell you that you are inside a block. Now type the rest of |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
167 |
the ``for`` loop, |
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
168 |
|
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
169 |
.. #[Nishanth]: Tell that IPython does auto indentation. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
170 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
171 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
172 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
173 |
print "Square root of", each, "is", sqrt(each) |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
174 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
175 |
Now we have finished the statements in the block, and still the |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
176 |
interpreter is showing four dots, which means you are still inside the |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
177 |
block. To exit from the block press return key or the enter key twice |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
178 |
without entering anything else. It printed the square root of each |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
179 |
number in the list, and that is executed in a ``for`` loop. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
180 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
181 |
Now, let us find the cube of all the numbers from one to ten. But this |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
182 |
time let us try it in the vanilla version of Python interpreter. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
183 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
184 |
Start the vanilla version of Python interpreter by issuing the command |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
185 |
``python`` in your terminal. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
186 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
187 |
{{{ open the python interpreter in the terminal using the command |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
188 |
python to start the vanilla Python interpreter }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
189 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
190 |
Start with, |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
191 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
192 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
193 |
for i in range(1,11): |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
194 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
195 |
and press enter once, and we will see that this time it shows four |
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
196 |
dots, but the cursor is close to the dots, so we have to indent the |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
197 |
block. The vanilla version of Python interpreter does not indent the |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
198 |
code automatically. So enter four spaces there and then type the |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
199 |
following |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
200 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
201 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
202 |
print i, "cube is", i**3 |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
203 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
204 |
Now when we hit enter, we still see the four dots, to get out of the |
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
205 |
block, hit enter once again |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
206 |
|
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
207 |
.. #[Nishanth]: Here also the overhead on print can be reduced. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
208 |
Think of a simple print statement. This statement |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
209 |
will be confusing for a newbie. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
210 |
We can focus more on indentation in python. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
211 |
|
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
212 |
.. #[nishanth]: Not sure if you must use range here. You can |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
213 |
define a list of numbers and iterate on it. |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
214 |
Then say this list can also be generated using |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
215 |
the range function and hence introduce range. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
216 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
217 |
Okay! so the main thing here we learned is how to use Python |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
218 |
interpreter and IPython interpreter to specify blocks. But while we |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
219 |
were generating the multiplication table we used something new, |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
220 |
``range()`` function. ``range()`` is an inbuilt function in Python |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
221 |
which can be used to generate a ``list`` of integers from a starting |
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
222 |
number to an ending number. Note that the ending number that you |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
223 |
specify will not be included in the ``list``. |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
224 |
|
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
225 |
.. #[Nishanth]: Show some examples of range without the step argument |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
226 |
May be give an exercise with negative numbers as arguments |
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
227 |
|
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
228 |
Now, let us print all the odd numbers from 1 to 50. Let us do it in |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
229 |
our IPython interpreter for ease of use. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
230 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
231 |
{{{ switch to next slide, problem statement of the next problem in |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
232 |
solved exercises }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
233 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
234 |
{{{ switch focus to ipython interpreter }}} |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
235 |
|
206
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
236 |
The problem can be solved by just using the ``range()`` function. |
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
237 |
|
8835b2c071e6
Made changes suggested by Nishanth and Amit on the script Getting started with for.
Anoop<anoop@fossee.in>
parents:
203
diff
changeset
|
238 |
It can be solved as, |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
239 |
:: |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
240 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
241 |
print range(1,51,2) |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
242 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
243 |
This time we passed three parameters to ``range()`` function unlike |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
244 |
the previous case where we passed only two parameters. The first two |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
245 |
parameters are the same in both the cases. The first parameter is the |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
246 |
starting number of the sequence and the second parameter is the end of |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
247 |
the range. Note that the sequence doesn't include the ending |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
248 |
number. The third parameter is for stepping through the sequence. Here |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
249 |
we gave two which means we are skipping every alternate element. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
250 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
251 |
{{{ switch to next slide, recap slide }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
252 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
253 |
Thus we come to the end of this tutorial. We learned about blocks in |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
254 |
Python, indentation, blocks in IPython, for loop, iterating over a |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
255 |
list and then the ``range()`` function. |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
256 |
|
202 | 257 |
.. #[Amit]: There does seem to too much overhead of details. Should |
258 |
the first example be done using script is it necessary. |
|
259 |
Do add some things in evolutionary manner. Like introducing |
|
260 |
range as a list and doing a very very simple for loop.Like |
|
261 |
iterating over [1,2,3] .Before getting into a problem. |
|
262 |
And club details about problem in one paragraph and syntactic details |
|
263 |
in other. |
|
264 |
||
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
265 |
{{{ switch to next slide, thank you slide }}} |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
266 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
267 |
Thank you! |
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
268 |
|
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
269 |
.. Author: Anoop Jacob Thomas <anoop@fossee.in> |
188
d0b76bc48aef
reviewed getting_started_with_lists
Nishanth <nishanth@fossee.in>
parents:
161
diff
changeset
|
270 |
Reviewer 1: Nishanth |
203 | 271 |
Reviewer 2: Amit Sethi |
161
b7c47307e510
added dictionaries, getting started with for, other_type_of_plots and savefig.
anoop
parents:
diff
changeset
|
272 |
External reviewer: |