Python-3.4.3/C2/Getting-started-with-for/English
Python/C2/Getting-started-with-for/English
Title of script: Getting started with for loops
Author: Trupti, Thirumalesh H S
Keywords:Python, IPython, for loop, blocks, indent, iterate, loop
|
|
Show Slide | Hello Friends. Welcome to the tutorial on "Getting started with for loops". |
Show Slide
Objectives
|
At the end of this tutorial, you will be able to,
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show Slide:
Pre-requisite
tutorials on http://spoken-tutorial.org
|
To practice this tutorial, you should know how to
use lists.
tutorials on this website. |
First let us see the syntax of for loop. | |
Show slide
<statement 2> Body of for … <statement n>
|
for statement iterates over the members of a sequence in order, executing the block each time.
|
We will see an example of ‘For’ loop and how to execute it. | |
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
|
Open your text editor. | |
Highlight according to narration
|
Let us write a for loop. Type the code as shown here:
|
numbers = [4, 9, 16, 25, 36]
print(“sqrt of”, num, “is”, num**0.5) print("This is outside for-loop")
|
Here the for loop iterates over a list of numbers and finds the square root of each number.
The variable names can be any of your choice. |
Switch back to the editor. | Let us switch back to the text editor. |
Highlight the line after for statement
2:22 |
Note that a colon after the for statement indicates the starting of loop body.
|
Highlight the line after for statement | It means that, the line is a block of code in for loop.
|
Highlight the fifth line -
print("This is outside for-loop")
|
Note that the line print("This is outside for-loop") is not indented.
|
Highlight indentation | Thus each block is separated by the indentation level.
|
Save the file as sqrt_num_list.py in the home directory.
| |
Now switch back to your terminal.
| |
[Ipython Terminal]
Save & run script Highlight the output %run -i sqrt_num_list.py sqrt of 4 is 2.0 sqrt of 9 is 3.0 sqrt of 16 is 4.0 sqrt of 25 is 5.0 sqrt of 36 is 6.0 This is outside for-loop
|
Run the script using the run command as,
percent run minus i filename and press Enter.
|
Show Slide
Exercise 1
|
* Use the same example we used in sqrt_num_list.py
|
[Ipython Terminal]
|
Switch to the terminal.
Type,
|
Highlight the dots | You will notice that, the prompt changes to three dots.
there are four spaces from the dots.
|
[Ipython Terminal]
|
Please note that IPython automatically indents the block.
|
[Ipython Terminal]
“is”, num**0.5)
|
Now type the rest of the for loop,print inside parentheses inside quotes sqrt of comma num comma inside quotes is comma num raised to power of 0.5
|
[Ipython Terminal]
|
Now we have finished the statements in the block.
|
[Ipython Terminal]
|
To exit from the block press the Enter key twice without entering anything else. |
[Ipython Terminal]
|
It printed the square root of each number in the list, which was executed in the for loop. |
Next we will see about range built-in function in Python. | |
Show Slide
range() function
|
Range() function generates a list of integers.
range inside parentheses one comma twenty comma two – generates integers from 1 to 19 with step of 2
|
[Ipython Terminal]
|
Note that the ending number that you specify will not be included in the list. |
Show Slide
Exercise 2
|
Find out the cube of all the numbers from one to ten
|
[Terminal]
python
press Enter
|
Let us now try to run the for loop in a Python terminal window.
Start the Python interpreter by issuing the command python in a new terminal. Type for i in range inside parentheses one comma eleven colon press Enter |
[Python Terminal]
|
We will see that this time it shows three dots, but the cursor is close to the dots.
|
[Python Terminal]
|
The Python interpreter does not indent the code automatically.
|
[Python Terminal]
|
Now when we hit enter, we still see the three dots.
|
[Python Terminal] | Okay! so the main thing we learnt here is -
|
Show Slide
Exercise 3
|
Print all the odd numbers from 1 to 50. |
[IPython Terminal]
|
Let us do it in our IPython interpreter for ease of use.
|
[IPython Terminal]
print(i) press Enter twice
|
for i in range inside parentheses one comma fifty one comma two colon
print inside parentheses i and press Enter twice |
[IPython Terminal]
|
The first parameter is the starting number of the sequence.
|
Highlight third parameter | Note that the sequence does not include the ending number.
|
Show Slide
Summary
|
This brings us to the end of the tutorial.
In this tutorial, we learnt to,
|
Show Slide
Assignment
|
Here are some self assessment questions for you to solve
2. Write a for loop to print the product of all natural numbers from 1 to 20.
range(1, 5)
|
Show Slide
Solution of self assessment questions on slide
|
And the answers,
2. y equal to one for x in range inside parentheses one comma twenty one colon y into equal to x print inside parentheses y 3. range(1, 5) will produce a list of integers from 1 to 4 [1,2,3,4]
|
Show Slide
Forum |
Please post your timed queries in this forum. |
Show Slide
Fossee Forum |
Please post your general queries on Python in this forum. |
Show Slide
Textbook Companion |
FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
Thank you. |