Python/C3/Getting-started-with-for/English

From Script | Spoken-Tutorial
Revision as of 12:22, 2 December 2012 by Pravin1389 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello and welcome to the tutorial on Getting started with ``for` loop`.
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Write blocks of code in python using indentation.
  2. Use the for loop.
  3. Use range() function.
  4. Write blocks in python interpreter
  5. Write blocks in ipython interpreter.


Show Slide 3

Pre-requisite

Switch to next slide, 'Whitespace in python'

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".

In Python whitespace is significant, and the blocks are visually separated. The best practice is to indent the code using four spaces.

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.

ipython -pylab Start the ipython interpreter using ipython -pylab.
Now let us move straight into for loop.
Show Slide 4

Assignment 1

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
Show Slide 5

Solution 1

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. Open your text editor and type the following code shown on the slide.
Show Slide 6

Save & run script

Now switch to your terminal and run the script as,
 %run -i list_roots.py 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,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.
Show the script which was created Note that the lines after for statement, is indented using four spaces.
Highlight the line after for statement 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 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.
Show Slide 7

Assignment 2

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 focus to the IPython interpreter
numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
for each in numbers:

Hit enter

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.
Highlight the four dots Please note that IPython automatically indents the block. The four dots tell you that you are inside a block.
print "Square root of", each,
print "is", sqrt(each)
Now type the rest of the for loop,
Hit enter twice 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, which was executed in the for loop.
Show Slide 8

Assignment 3

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.
Switch to the terminal
python

for i in range(1,11):

Hit enter

Start the vanilla version of Python interpreter by issuing the command python in your terminal.
Highlight the cursor 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.
print i, "cube is", i**3 The vanilla version of Python interpreter does not indent the code automatically. So enter four spaces there and then type the following
Hit enter Now when we hit enter, we still see the four dots. To get out of the block, hit enter once again.
Show Slide 9

range() function

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.
Show Slide 10

Assignment 4

Print all the odd numbers from 1 to 50. Let us do it in our IPython interpreter for ease of use.
Switch focus to ipython interpreter
ipython
The problem can be solved by just using the range() function.

It can be solved as,

print range(1,51,2) This time we passed three parameters to range() function unlike the previous case where we passed only two parameters. The first two 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 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.
Show Slide 11

Summary

This brings us to the end of the tutorial. In this tutorial,we learnt to,
  1. create blocks in python using for loop
  2. indent the blocks of code
  3. iterate over a list using for loop
  4. use the range() function


Show Slide 12

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Indentation is not mandatory in Python
    • True
    • False
  1. 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)
Show Slide 13

Solution of self assessment questions on slide

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
  1. range(1,5) will produce a list of integers from 1 to 4. [1,2,3,4]


Show Slide 14

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Pravin1389