Difference between revisions of "Python-3.4.3/C2/Getting-started-with-for/English"
(Created page with "Python/C2/Getting-started-with-for/English '''Title of script''': Getting started with '''for loops''' '''Author: Trupti, Thirumalesh H S''' '''Keywords:Python, IPython, fo...") |
|||
Line 1: | Line 1: | ||
− | |||
− | |||
'''Title of script''': Getting started with '''for loops''' | '''Title of script''': Getting started with '''for loops''' | ||
Line 18: | Line 16: | ||
Objectives | Objectives | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| At the end of this tutorial, you will be able to, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| At the end of this tutorial, you will be able to, | ||
− | |||
# Use the '''for''' loop. | # Use the '''for''' loop. | ||
# Use '''range()''' function. | # Use '''range()''' function. | ||
− | |||
− | |||
|- | |- | ||
Line 38: | Line 31: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 45: | Line 36: | ||
Pre-requisite | Pre-requisite | ||
− | |||
If not, see the relavant Python | If not, see the relavant Python | ||
tutorials on http://spoken-tutorial.org | tutorials on http://spoken-tutorial.org | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practice this tutorial, you should know how to | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practice this tutorial, you should know how to | ||
use lists. | use lists. | ||
− | |||
If not, see the relevant Python | If not, see the relevant Python | ||
Line 68: | Line 55: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide | ||
− | |||
Syntax: For | Syntax: For | ||
− | |||
<nowiki>for <loop variable> in sequence:</nowiki><nowiki><statement 1></nowiki> | <nowiki>for <loop variable> in sequence:</nowiki><nowiki><statement 1></nowiki> | ||
Line 80: | Line 65: | ||
<nowiki><statement n></nowiki> | <nowiki><statement n></nowiki> | ||
− | |||
'''Highlight according to narration''' | '''Highlight according to narration''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''for''' statement iterates over the members of a sequence in order, executing the block each time. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''for''' statement iterates over the members of a sequence in order, executing the block each time. | ||
− | |||
Here the loop variable takes the value of the item inside the sequence on each iteration. | Here the loop variable takes the value of the item inside the sequence on each iteration. | ||
− | |||
For each '''item,''' the '''loop''' '''body''' is '''executed. ''' | For each '''item,''' the '''loop''' '''body''' is '''executed. ''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 105: | Line 82: | ||
'''ipython3''' | '''ipython3''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. | ||
− | |||
Now, type '''ipython3''' and press '''Enter'''. | Now, type '''ipython3''' and press '''Enter'''. | ||
Line 118: | Line 92: | ||
'''%pylab '''and press '''Enter.''' | '''%pylab '''and press '''Enter.''' | ||
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' package. | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' package. | ||
− | |||
Type '''%pylab '''and press''' Enter.''' | Type '''%pylab '''and press''' Enter.''' | ||
Line 128: | Line 101: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight according to narration | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight according to narration | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us write a '''for''' loop. Type the code as shown here: | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us write a '''for''' loop. Type the code as shown here: | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''<nowiki>numbers = [4, 9, 16, 25, 36]</nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''<nowiki>numbers = [4, 9, 16, 25, 36]</nowiki>''' | ||
− | |||
'''for num in numbers:''' | '''for num in numbers:''' | ||
Line 147: | Line 114: | ||
'''print("This is outside for-loop")''' | '''print("This is outside for-loop")''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here the '''for''' loop iterates over a list of numbers and finds the square root of each number. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here the '''for''' loop iterates over a list of numbers and finds the square root of each number. | ||
− | |||
The numbers are: 4, 9, 16, 25, 36 | The numbers are: 4, 9, 16, 25, 36 | ||
− | |||
Note that here we used two variables, | Note that here we used two variables, | ||
Line 172: | Line 135: | ||
2:22 | 2:22 | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that a '''colon''' after the '''for''' '''statement''' '''indicates''' the starting of '''loop''' '''body'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that a '''colon''' after the '''for''' '''statement''' '''indicates''' the starting of '''loop''' '''body'''. | ||
− | |||
Every '''statement '''in '''loop''' starts''' with 4 spaces''' | Every '''statement '''in '''loop''' starts''' with 4 spaces''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the line after '''for '''statement | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the line after '''for '''statement | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| It means that, the line is a '''block''' of '''code''' in '''for loop'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| It means that, the line is a '''block''' of '''code''' in '''for loop'''. | ||
− | |||
In this example, it is only a single statement in the block. | In this example, it is only a single statement in the block. | ||
Line 190: | Line 148: | ||
'''print("This is outside for-loop")''' | '''print("This is outside for-loop")''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that the line '''print("This is outside for-loop")''' is not indented. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that the line '''print("This is outside for-loop")''' is not indented. | ||
− | |||
It means that it is not a part of the''' for''' loop. | It means that it is not a part of the''' for''' loop. | ||
− | |||
And the lines after that don't fall in the scope of the '''for''' loop. | And the lines after that don't fall in the scope of the '''for''' loop. | ||
Line 204: | Line 158: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight indentation | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight indentation | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Thus each block is separated by the indentation level. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Thus each block is separated by the indentation level. | ||
− | |||
This marks the importance of '''white-spaces''' in '''Python'''. | This marks the importance of '''white-spaces''' in '''Python'''. | ||
Line 211: | Line 164: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Save the file as '''sqrt_num_list.py '''in the''' home '''directory'''.''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Save the file as '''sqrt_num_list.py '''in the''' home '''directory'''.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now switch back to your terminal. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now switch back to your terminal. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 242: | Line 189: | ||
This is outside for-loop | This is outside for-loop | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Run the script using the '''run''' command as, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Run the script using the '''run''' command as, | ||
'''''percent'' run ''minus'' i filename '''and press '''Enter.''' | '''''percent'' run ''minus'' i filename '''and press '''Enter.''' | ||
− | |||
We get the square root of the given numbers executed by the for loop. | We get the square root of the given numbers executed by the for loop. | ||
− | |||
This is the print statement output executed after the for loop. | This is the print statement output executed after the for loop. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 262: | Line 202: | ||
Exercise 1 | Exercise 1 | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| * Use the same example we used in sqrt_num_list.py | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| * Use the same example we used in sqrt_num_list.py | ||
Line 270: | Line 208: | ||
* Skip the line: '''print("This is outside for-loop")''' | * Skip the line: '''print("This is outside for-loop")''' | ||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
'''<nowiki>numbers = [4, 9, 16, 25, 36]</nowiki>''' | '''<nowiki>numbers = [4, 9, 16, 25, 36]</nowiki>''' | ||
− | |||
'''for num in numbers: '''Press '''Enter''' | '''for num in numbers: '''Press '''Enter''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal. | ||
Type, | Type, | ||
− | |||
'''numbers '''''equal to inside square brackets''''' 4, 9, 16, 25, 36''' | '''numbers '''''equal to inside square brackets''''' 4, 9, 16, 25, 36''' | ||
− | |||
'''for num in numbers '''''colon''''' '''Press '''Enter''' | '''for num in numbers '''''colon''''' '''Press '''Enter''' | ||
Line 297: | Line 227: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the dots | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the dots | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| You will notice that, the prompt changes to three dots. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| You will notice that, the prompt changes to three dots. | ||
− | |||
And the cursor is not right after the three dots but, | And the cursor is not right after the three dots but, | ||
there are four spaces from the dots. | there are four spaces from the dots. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
Highlight the three dots | Highlight the three dots | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Please note that '''IPython''' automatically indents the block. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Please note that '''IPython''' automatically indents the block. | ||
− | |||
The three dots tell you that you are inside a block. | The three dots tell you that you are inside a block. | ||
Line 318: | Line 242: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
'''print(“sqrt of”, num, ''' | '''print(“sqrt of”, num, ''' | ||
'''“is”, num**0.5)''' | '''“is”, num**0.5)''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now we have finished the statements in the block. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now we have finished the statements in the block. | ||
− | |||
But still the interpreter is showing three dots, this means that you are still inside the block. | But still the interpreter is showing three dots, this means that you are still inside the block. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
press '''Enter''' twice | press '''Enter''' twice | ||
Line 353: | Line 264: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
Highlight output | Highlight output | ||
Line 366: | Line 276: | ||
'''range()''' function | '''range()''' function | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Range() function generates a '''list''' of '''integers.''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Range() function generates a '''list''' of '''integers.''' | ||
− | |||
The syntax is: '''range '''''inside parentheses inside square brackets''''' start '''''comma close square brackets '''''stop '''''open square brackets comma''''' step '''''close square brackets'' | The syntax is: '''range '''''inside parentheses inside square brackets''''' start '''''comma close square brackets '''''stop '''''open square brackets comma''''' step '''''close square brackets'' | ||
− | |||
For example: | For example: | ||
Line 380: | Line 286: | ||
1 to 19 with step of 2 | 1 to 19 with step of 2 | ||
− | |||
'''range '''''inside parentheses''''' twenty '''– generates integers from 0 to 19 | '''range '''''inside parentheses''''' twenty '''– generates integers from 0 to 19 | ||
Line 386: | Line 291: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
− | |||
Highlight output | Highlight output | ||
Line 395: | Line 299: | ||
Exercise 2 | Exercise 2 | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Find out the cube of all the numbers from one to ten | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Find out the cube of all the numbers from one to ten | ||
− | |||
Execute this in the '''Python''' interpreter. | Execute this in the '''Python''' interpreter. | ||
Line 407: | Line 308: | ||
'''python''' | '''python''' | ||
− | |||
'''for i in range(1, 11):''' | '''for i in range(1, 11):''' | ||
press '''Enter''' | press '''Enter''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us now try to run the '''for''' loop in a Python terminal window. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us now try to run the '''for''' loop in a Python terminal window. | ||
Line 425: | Line 323: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | ||
− | |||
Highlight the cursor | Highlight the cursor | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We will see that this time it shows three dots, but the cursor is close to the dots. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We will see that this time it shows three dots, but the cursor is close to the dots. | ||
− | |||
So we have to indent the block. | So we have to indent the block. | ||
Line 435: | Line 331: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | ||
− | |||
'''print(i, "cube is", i**3)''' | '''print(i, "cube is", i**3)''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The '''Python''' interpreter does not indent the code automatically. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The '''Python''' interpreter does not indent the code automatically. | ||
− | |||
So enter four spaces there and then type the following | So enter four spaces there and then type the following | ||
− | |||
'''print'' '''''inside parentheses''''' ''i '''''comma inside quotes''' ''cube is '''''comma''''' i '''''raised to''''' '''''power of''''' three''' | '''print'' '''''inside parentheses''''' ''i '''''comma inside quotes''' ''cube is '''''comma''''' i '''''raised to''''' '''''power of''''' three''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | ||
− | |||
press '''Enter''' | press '''Enter''' | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now when we hit enter, we still see the three dots. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now when we hit enter, we still see the three dots. | ||
− | |||
To get out of the block, press '''Enter''' once again. | To get out of the block, press '''Enter''' once again. | ||
Line 465: | Line 351: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Python Terminal]</nowiki> | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Okay! so the main thing we learnt here is - | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Okay! so the main thing we learnt here is - | ||
− | |||
* how to use the '''Python''' '''interpreter''' and | * how to use the '''Python''' '''interpreter''' and | ||
* the '''IPython''' interpreter to specify blocks. | * the '''IPython''' interpreter to specify blocks. | ||
− | |||
− | |||
|- | |- | ||
Line 477: | Line 360: | ||
Exercise 3 | Exercise 3 | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Print all the odd numbers from 1 to 50. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Print all the odd numbers from 1 to 50. | ||
Line 484: | Line 365: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us do it in our '''IPython''' interpreter for ease of use. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us do it in our '''IPython''' interpreter for ease of use. | ||
− | |||
The problem can be solved by just using the '''range()''' function. | The problem can be solved by just using the '''range()''' function. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
for i in range(1, 51, 2): | for i in range(1, 51, 2): | ||
Line 504: | Line 378: | ||
press '''Enter '''twice | press '''Enter '''twice | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| for''' i''' in '''range''' ''inside parentheses '''''one''' ''comma'' '''fifty one''' ''comma''''' two''''' colon '' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| for''' i''' in '''range''' ''inside parentheses '''''one''' ''comma'' '''fifty one''' ''comma''''' two''''' colon '' | ||
Line 513: | Line 385: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
Highlight parameters in '''range '''function and highlight output | Highlight parameters in '''range '''function and highlight output | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The first '''parameter''' is the starting number of the sequence. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The first '''parameter''' is the starting number of the sequence. | ||
− | |||
The second '''parameter''' is the end of the '''range'''. | The second '''parameter''' is the end of the '''range'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight third parameter | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight third parameter | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that the '''sequence''' does not include the ending number. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Note that the '''sequence''' does not include the ending number. | ||
− | |||
The third '''parameter''' is for stepping through the '''sequence'''. | The third '''parameter''' is for stepping through the '''sequence'''. | ||
− | |||
Here we gave '''two''' which means we are skipping every alternate element. | Here we gave '''two''' which means we are skipping every alternate element. | ||
Line 538: | Line 403: | ||
Summary | Summary | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This brings us to the end of the tutorial. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This brings us to the end of the tutorial. | ||
In this tutorial, we learnt to, | In this tutorial, we learnt to, | ||
− | |||
# Create blocks in python using''' for''' | # Create blocks in python using''' for''' | ||
Line 550: | Line 412: | ||
# Iterate over a list using '''for''' loop | # Iterate over a list using '''for''' loop | ||
# Use the '''range()''' function | # Use the '''range()''' function | ||
− | |||
− | |||
|- | |- | ||
Line 557: | Line 417: | ||
Assignment | Assignment | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | ||
− | |||
# Indentation is not mandatory in '''Python''' | # Indentation is not mandatory in '''Python''' | ||
Line 569: | Line 426: | ||
2. Write a '''for '''loop to print the product of all natural numbers from 1 to 20. | 2. Write a '''for '''loop to print the product of all natural numbers from 1 to 20. | ||
− | |||
3. What will be the output of: | 3. What will be the output of: | ||
'''range(1, 5)''' | '''range(1, 5)''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 582: | Line 435: | ||
Solution of self assessment questions on slide | Solution of self assessment questions on slide | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| And the answers, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| And the answers, | ||
− | |||
# False, '''Indentation''' is essential in '''python'''. | # False, '''Indentation''' is essential in '''python'''. | ||
Line 601: | Line 451: | ||
3. '''range(1, 5)'''<nowiki> will produce a list of integers from 1 to 4 [1,2,3,4]</nowiki> | 3. '''range(1, 5)'''<nowiki> will produce a list of integers from 1 to 4 [1,2,3,4]</nowiki> | ||
− | |||
− | |||
− | |||
|- | |- |
Revision as of 11:19, 15 November 2017
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 If not, see the relavant Python tutorials on http://spoken-tutorial.org |
To practice this tutorial, you should know how to
use lists. If not, see the relevant Python tutorials on this website. |
First let us see the syntax of for loop. | |
Show slide
Syntax: For for <loop variable> in sequence:<statement 1> <statement 2> Body of for … <statement n> Highlight according to narration |
for statement iterates over the members of a sequence in order, executing the block each time.
Here the loop variable takes the value of the item inside the sequence on each iteration. For each item, the loop body is executed. |
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.
Now, type ipython3 and press Enter. |
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
Type %pylab and press Enter. |
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]
for num in numbers: 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 numbers are: 4, 9, 16, 25, 36 Note that here we used two variables,
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.
Every statement in loop starts with 4 spaces |
Highlight the line after for statement | It means that, the line is a block of code in for loop.
In this example, it is only a single statement in the block. |
Highlight the fifth line -
print("This is outside for-loop") |
Note that the line print("This is outside for-loop") is not indented.
It means that it is not a part of the for loop. And the lines after that don't fall in the scope of the for loop. |
Highlight indentation | Thus each block is separated by the indentation level.
This marks the importance of white-spaces in Python. |
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. We get the square root of the given numbers executed by the for loop. This is the print statement output executed after the for loop. |
Show Slide
Exercise 1 |
* Use the same example we used in sqrt_num_list.py
|
[Ipython Terminal]
numbers = [4, 9, 16, 25, 36] for num in numbers: Press Enter |
Switch to the terminal.
Type, numbers equal to inside square brackets 4, 9, 16, 25, 36 for num in numbers colon Press Enter |
Highlight the dots | You will notice that, the prompt changes to three dots.
And the cursor is not right after the three dots but, there are four spaces from the dots. |
[Ipython Terminal]
Highlight the three dots |
Please note that IPython automatically indents the block.
The three dots tell you that you are inside a block. |
[Ipython Terminal]
print(“sqrt of”, num, “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.
But still the interpreter is showing three dots, this means that you are still inside the block. |
[Ipython Terminal]
press Enter twice |
To exit from the block press the Enter key twice without entering anything else. |
[Ipython Terminal]
Highlight output |
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.
The syntax is: range inside parentheses inside square brackets start comma close square brackets stop open square brackets comma step close square brackets For example: range inside parentheses one comma twenty comma two – generates integers from 1 to 19 with step of 2 range inside parentheses twenty – generates integers from 0 to 19 |
[Ipython Terminal]
Highlight output |
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
Execute this in the Python interpreter. |
[Terminal]
python for i in range(1, 11): 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]
Highlight the cursor |
We will see that this time it shows three dots, but the cursor is close to the dots.
So we have to indent the block. |
[Python Terminal]
print(i, "cube is", i**3) |
The Python interpreter does not indent the code automatically.
So enter four spaces there and then type the following print inside parentheses i comma inside quotes cube is comma i raised to power of three |
[Python Terminal]
press Enter |
Now when we hit enter, we still see the three dots.
To get out of the block, press Enter once again. |
[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.
The problem can be solved by just using the range() function. |
[IPython Terminal]
for i in range(1, 51, 2): 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]
Highlight parameters in range function and highlight output |
The first parameter is the starting number of the sequence.
The second parameter is the end of the range. |
Highlight third parameter | 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
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. 3. What will be the output of: 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. |