summaryrefslogtreecommitdiff
path: root/getting_started_with_for/script.rst
diff options
context:
space:
mode:
Diffstat (limited to 'getting_started_with_for/script.rst')
-rw-r--r--getting_started_with_for/script.rst379
1 files changed, 231 insertions, 148 deletions
diff --git a/getting_started_with_for/script.rst b/getting_started_with_for/script.rst
index 69f925b..304cbe5 100644
--- a/getting_started_with_for/script.rst
+++ b/getting_started_with_for/script.rst
@@ -30,279 +30,362 @@
Getting started with for loop
=============================
-{{{ show welcome slide }}}
+.. L1
-Hello and welcome to the tutorial `Getting started with ``for`` loop`.
+{{{ Show the first slide containing title, name of the production
+team along with the logo of MHRD }}}
-{{{ switch to next slide, outline slide }}}
+.. R1
-In this tutorial we will learn about ``for`` loops in python, and also
-learn how to write blocks of code in Python.
+Hello and welcome to the tutorial on `Getting started with ``for`` loop`.
-.. #[Nishanth]: Instead of saying basics of indenting code,
- say How to define code blocks in Python
+.. L2
-{{{ switch to next slide, about whitespaces }}}
+{{{ Show slide with objectives }}}
+
+.. R2
+
+At the end of this tutorial, you will be able to,
+
+ 1. Write blocks of code in python using indentation.
+ #. Use the ``for`` loop.
+ #. Use ``range()`` function.
+ #. Write blocks in python interpreter
+ #. Write blocks in ipython interpreter.
+
+.. L3
+
+{{{ switch to next slide, 'Whitespace in python' }}}
+
+.. R3
In Python whitespace is significant, and the blocks are visually
-separated.
+separated.The best practice is to indent the code using four spaces.
-.. #[nishanth]: Simply tell how blocks are defined in python.
- The details like braces are not used and its
- advantages like neat code can be told after completely
- explaining the indentation
+As you can see in the slide, “Block B” is an inner block, indented by 4 spaces.
+After “Block B” the next statement in ”Block A” starts from the same
+indentation level of other ”Block A” Statements.
-.. #[Amit]: Do you want to do that here. May be its better to talk about
- this after some initiation into the idea of blocks.
+.. R4
-The best practice is to indent the code using four spaces.
+Start the ipython interpreter using ipython -pylab.
-.. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces
- for indentation. Do that while typing so that they can
- actually see what is being typed.
+.. L4
+::
+
+ ipython -pylab
-As you can see in the slide, ``Block B`` is an inner block and it is
-indented using 4 spaces, and after ``Block B`` the next statement in
-``Block A`` starts from the same indentation level of other ``Block
-A`` statements.
+.. R5
Now let us move straight into ``for`` loop.
-{{{ switch to next slide, problem statement of exercise 1 }}}
+.. L5
+.. L6
-Write a for loop which iterates through a list of numbers and find the
-square root of each number.
-::
+{{{ switch to slide showing Question 1 }}}
- numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
+.. R6
-.. #[nishanth]: making new list with square roots induces extra complication
- like appending which has no use case here
+Write a for loop which iterates through a list of numbers and find the
+square root of each number.
+numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
-.. #[Nishanth]: The problem focuses more on square root and creation
- of list. The problem must be simple and focusing on
- nothing more but the indentation and for loop.
- May be change the problem to print squares than to
- print square roots.
+.. R7
For the problem, first we need to create a ``list`` of numbers and
then iterate over the list and find the square root of each element in
it. And let us create a script, rather than typing it out in the
-interpreter itself. Create a script called list_roots.py and type the
-following.
+interpreter itself. Open your text editor and type the following code shown on the slide.
-{{{ open the text editor and paste the following code there }}}
-::
+.. L7
- numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
- for each in numbers:
- print "Square root of", each, "is", sqrt(each)
- print "This is not in for loop!"
+{{{ Switch to the slide Solution 1 }}}
-.. numbers = [1, 12, 3, 4, 21, 17]
- for each in numbers:
- print each, each * each
+.. L8
-.. #[nishanth]: I don't see a use case to append the sq_root to
- square_roots. It is only complicating stuff.
- Simply iterate and print.
+{{{ switch to next slide, save & run script }}}
-{{{ switch to next slide, save and run script }}}
+.. R8
-{{{ save the script }}}
+Now switch to your terminal and run the script as,
-Now save the script, and run it from your IPython interpreter. I
-assume that you have started your IPython interpreter using ``-pylab``
-option.
-
-Run the script as,
+.. L9
::
%run -i list_roots.py
-.. #[Nishanth]: you don't have to use the -i option here
-
-{{{ run the script }}}
+.. R9
So that was easy! All what we did was iterate over the list element by
element and then use the element for calculation. Note that here we
-used two variables. One the variable ``numbers``, which is a list,
-another one ``each``, which is the element of list under consideration
+used two variables,the variable ``numbers``, which is a list,and the
+other variable ``each``, which is the element of list under consideration
in each cycle of the ``for`` loop. The variable names can be chosen by
you.
-.. #[Nishanth]: The details like we didn't have to find the length
- are relevant for people who have programmed in C or
- other languages earlier. But for a newbie it is more
- of confusing extra info. That part may be skipped.
- Simply go ahead and focus on the syntax of for loop.
- And how the variable name is used inside the for loop.
- If you modify the question to only print, the extra
- variable sq_root can also be avoided. let it be more
- about "each", "numbers" and "for". no other new names.
+.. L10
{{{ show the script which was created }}}
+.. R10
+
Note that the lines after ``for`` statement, is indented using four
spaces.
-{{{ highlight the line after for statement }}}
+.. L11
+
+{{{ highlight the line after ``for`` statement }}}
+
+.. R11
-It means that line is part of the for loop. And it is a block of code,
-although it is only a single statement in the block. And the fourth
-line or the immediate line after the ``for`` block is not indented,
+It means that line is a part of the for loop. And it is a block of code,
+although it is only a single statement in the block. Also, the fourth
+line or the immediate line after the ``for`` block is not indented.
-{{{ highlight the fourth line - the line just after for loop }}}
+.. L12
-it means that it is not part of the ``for`` loop and the lines after
-that doesn't fall in the scope of the ``for`` loop. Thus each block is
+{{{ Highlight the fourth line - the line just after for loop }}}
+
+.. R12
+
+It means that it is not a part of the ``for`` loop and the lines after
+that dont fall in the scope of the ``for`` loop. Thus each block is
separated by the indentation level and that marks the importance of
white-spaces in Python.
-{{{ switch to the slide which shows the problem statement of the first
-problem to be tried out }}}
-
-Now a question for you to try, from the given numbers make a list of
-perfect squares and a list of those which are not. The numbers are,
-::
-
- 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916
+.. L13
-Pause here and try to solve the problem before proceeding further.
+{{{ switch to slide showing Question 2 }}}
-{{{ switch to next slide, problem statement of second problem in
-solved exercise}}}
+.. R13
-Now let us try a simple one, to print the square root of numbers in
-the list. And this time let us do it right in the IPython
-interpreter.
+Print the square root of numbers in the list.
+And this time let us do it right in the IPython interpreter.
+So let us create a list.
-{{{ switch to next slide, Indentation in ``ipython`` }}}
+.. L14
{{{ switch focus to the IPython interpreter }}}
-
-So let us start with making a list. Type the following
::
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
for each in numbers:
-and now you will notice that, as soon as you press the return key
+{{{ Hit enter }}}
+
+.. R14
+
+You will notice that, as soon as you press the enter key
after for statement, the prompt changes to four dots and the cursor is
not right after the four dots but there are four spaces from the
-dots. Please note that IPython automatically indents the block. The
-four dots tell you that you are inside a block. Now type the rest of
-the ``for`` loop,
+dots.
+
+.. L15
+
+{{{ Higlight the four dots }}}
+
+.. R15
-{{{ switch to next slide, Indentation in ``ipython`` (cont'd) }}}
+Please note that IPython automatically indents the block. The
+four dots tell you that you are inside a block.
-.. #[Nishanth]: Tell that IPython does auto indentation.
+.. R16
+Now type the rest of the ``for`` loop,
+
+.. L16
::
- print "Square root of", each,
- print "is", sqrt(each)
+ print "Square root of", each,
+ print "is", sqrt(each)
+
+.. R17
Now we have finished the statements in the block, and still the
interpreter is showing four dots, this means that you are still inside the
block. To exit from the block press the return key or the enter key twice
-without entering anything else. It printed the square root of each
-number in the list, and that is executed in a ``for`` loop.
+without entering anything else.
+
+.. L17
+
+{{{ Hit enter twice }}}
+
+.. R18
+
+It printed the square root of each
+number in the list, which was executed in the ``for`` loop.
+
+.. L18
+
+.. L19
+
+{{{ switch to slide Question 3 }}}
+
+.. R19
-{{{ switch to next slide, Indentation in ``python`` interpreter }}}
+Find the cube of all the numbers from one to ten.
+But this time let us try it in the vanilla version of Python interpreter.
-Now, let us find the cube of all the numbers from one to ten. But this
-time let us try it in the vanilla version of Python interpreter.
+.. R20
Start the vanilla version of Python interpreter by issuing the command
``python`` in your terminal.
-{{{ open the python interpreter in the terminal using the command
-python to start the vanilla Python interpreter }}}
+.. L20
-{{{ switch to next slide, Indentation in ``python`` interpreter
-(cont'd) }}}
-
-Start with,
+{{{ Switch to the terminal }}}
::
+
+ python
for i in range(1,11):
-and press enter once, and we will see that this time it shows four
+{{{ Hit enter }}}
+
+.. R21
+
+press enter once, and we will see that this time it shows four
dots, but the cursor is close to the dots, so we have to indent the
-block. The vanilla version of Python interpreter does not indent the
+block.
+
+.. L21
+
+{{{ Highlight the cursor }}}
+
+.. R22
+
+The vanilla version of Python interpreter does not indent the
code automatically. So enter four spaces there and then type the
following
+
+.. L22
::
- print i, "cube is", i**3
+ print i, "cube is", i**3
+
+.. R23
+
+Now when we hit enter, we still see the four dots.To get out of the
+block, hit enter once again.
-Now when we hit enter, we still see the four dots, to get out of the
-block, hit enter once again
+.. L23
-.. #[Nishanth]: Here also the overhead on print can be reduced.
- Think of a simple print statement. This statement
- will be confusing for a newbie.
- We can focus more on indentation in python.
+{{{ Hit enter }}}
-.. #[nishanth]: Not sure if you must use range here. You can
- define a list of numbers and iterate on it.
- Then say this list can also be generated using
- the range function and hence introduce range.
+.. L24
{{{ switch to the next slide, ``range()`` function }}}
-Okay! so the main thing that we learned here is how to use Python
-interpreter and IPython interpreter to specify blocks. But while we
+.. R24
+
+Okay! so the main thing we learnt here is how to use the Python
+interpreter and the IPython interpreter to specify blocks. But while we
were generating the multiplication table we used something new,
``range()`` function. ``range()`` is an inbuilt function in Python
which can be used to generate a ``list`` of integers from a starting
number to an ending number. Note that the ending number that you
specify will not be included in the ``list``.
-.. #[Nishanth]: Show some examples of range without the step argument
- May be give an exercise with negative numbers as arguments
+.. L25
-{{{ switch to next slide, problem statement of the next problem in
-solved exercises }}}
+{{{ switch to next slide Question 4 }}}
-Now, let us print all the odd numbers from 1 to 50. Pause here and try
-to solve the problem yourself.
+.. R25
+Print all the odd numbers from 1 to 50.
Let us do it in our IPython interpreter for ease of use.
+.. L26
+
{{{ switch focus to ipython interpreter }}}
+::
+
+ ipython
+
+.. R26
The problem can be solved by just using the ``range()`` function.
It can be solved as,
+
+.. L27
::
print range(1,51,2)
+.. R27
+
This time we passed three parameters to ``range()`` function unlike
the previous case where we passed only two parameters. The first two
-parameters are the same in both the cases. The first parameter is the
+parameters are same in both the cases. The first parameter is the
starting number of the sequence and the second parameter is the end of
-the range. Note that the sequence doesn't include the ending
+the range. Note that the sequence does not include the ending
number. The third parameter is for stepping through the sequence. Here
we gave two which means we are skipping every alternate element.
-{{{ switch to next slide, summary slide }}}
+.. L28
+
+{{{ switch to Summary slide }}}
+
+.. R28
+
+This brings us to the end of the tutorial.In this tutorial,we learnt to,
+
+ 1. create blocks in python using ``for`` loop
+ #. indent the blocks of code
+ #. iterate over a list using ``for`` loop
+ #. use the ``range()`` function
-Thus we come to the end of this tutorial. We learned about blocks in
-Python, indentation, blocks in IPython, for loop, iterating over a
-list and then the ``range()`` function.
+.. L29
-.. #[Amit]: There does seem to too much overhead of details. Should
- the first example be done using script is it necessary.
- Do add some things in evolutionary manner. Like introducing
- range as a list and doing a very very simple for loop.Like
- iterating over [1,2,3] .Before getting into a problem.
- And club details about problem in one paragraph and syntactic details
- in other.
+{{Show self assessment questions slide}}
-{{{ switch to next slide, thank you slide }}}
+.. R29
+Here are some self assessment questions for you to solve
+
+1. Indentation is not mandatory in Python
+
+ - True
+ - False
+
+2. Write a code using ``for`` loop to print the product of all
+ natural numbers from 1 to 20.
+
+
+3. What will be the output of-
+::
+
+ range(1,5)
+
+.. L30
+
+{{{ solution of self assessment questions on slide }}}
+
+.. R30
+
+And the answers,
+
+1. False.Indentation is essential in python.
+
+2. We use the ``for`` loop in the following manner.
+::
+
+ y = 1
+ for x in range(1,21):
+ y*=x
+ print y
+
+3. ``range(1,5)`` will produce a list of integers from 1 to 4.
+ [1,2,3,4]
+
+.. L31
+
+{{{ switch to Thank you slide }}}
+
+.. R31
+
+Hope you have enjoyed and found it useful.
Thank you!
+