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