Difference between revisions of "Python-3.4.3/C2/Getting-started-with-for/English"
(15 intermediate revisions by 3 users not shown) | |||
Line 19: | Line 19: | ||
| 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''' | + | # Use the '''for loop'''. |
− | # Use '''range()''' | + | # Use '''range() function'''. |
|- | |- | ||
Line 39: | Line 39: | ||
If not, see the relavant Python tutorials on http://spoken-tutorial.org | If not, see the relavant Python 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 use lists. | + | | 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'''. |
− | If not, see the relevant Python tutorials on this website. | + | If not, see the relevant '''Python''' tutorials on this website. |
|- | |- | ||
| 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;"| First let us see the syntax of '''for''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| First let us see the syntax of '''for loop'''. |
|- | |- | ||
Line 64: | Line 64: | ||
| 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;"| | ||
− | '''for''' | + | '''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. | ||
|- | |- | ||
| 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;"| We will see an example of ''' | + | | 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 an example of '''for loop''' and how to execute it. |
|- | |- | ||
Line 80: | Line 82: | ||
| 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 87: | Line 90: | ||
'''%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''' | + | | 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 93: | Line 96: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open your '''text''' '''editor'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open your '''text''' '''editor'''. | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open your '''text | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open your '''text editor'''. |
|- | |- | ||
| 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''' | + | | 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: |
|- | |- | ||
Line 111: | Line 114: | ||
| 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 loop variable 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 loop variable iterates over a list of numbers and finds the square root of each number. | ||
− | The numbers are: 4, 9, 16, 25, and 36 | + | |
+ | The numbers are: 4, 9, 16, 25, and 36. | ||
+ | |||
Note that here we used two variables, | Note that here we used two variables, | ||
− | * '''numbers -''' which is a list of numbers | + | * '''numbers -''' which is a '''list''' of numbers |
− | * '''num''' - which is the element of list under consideration in each cycle of the '''for loop'''. | + | * '''num''' - which is the element of '''list''' under consideration, in each cycle of the '''for loop'''. |
The variable names can be any of your choice. | The variable names can be any of your choice. | ||
|- | |- | ||
− | | 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 | + | | 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 '''print(“sqrt of”, num, “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;"| Note that a '''colon''' after the '''for | + | | 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 | + | |
+ | 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 | + | | 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 '''print(“sqrt of”, num, “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;"| 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 140: | Line 147: | ||
| 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. | ||
− | |||
− | And the lines after that don't fall in the scope of the '''for''' | + | 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'''. | ||
|- | |- | ||
| 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 152: | Line 162: | ||
|- | |- | ||
| 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 | | 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 | ||
− | | 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 | + | | 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;"| 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;"| switch back to your terminal. Clear 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;"| 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'''. |
+ | |||
+ | |||
+ | Let us clear the '''terminal'''. | ||
|- | |- | ||
Line 179: | Line 192: | ||
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. | + | |- |
+ | | 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 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 | ||
+ | |||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| 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 193: | Line 224: | ||
| 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;"| | ||
− | * Use the same example we used in sqrt_num_list.py | + | * Use the same example we used in '''sqrt_num_list.py''' |
− | * Type each line of the code in the '''IPython | + | * Type each line of the code in the '''IPython interpreter prompt. ''' |
* Skip the line: '''print("This is outside for-loop")''' | * Skip the line: '''print("This is outside for-loop")''' | ||
Line 201: | Line 232: | ||
|- | |- | ||
| 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> | ||
+ | Type | ||
'''<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'''. | ||
− | |||
Type, | Type, | ||
− | '''numbers | + | '''numbers ''equal to inside square brackets'' 4, 9, 16, 25, 36''' and press '''Enter''' |
− | '''for num in numbers '''''colon | + | |
+ | '''for num in numbers '''''colon'' 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;"| 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 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. | + | | 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 | + | |
+ | And the cursor is not after the three dots but, there are four spaces from the three dots. | ||
|- | |- | ||
Line 224: | Line 261: | ||
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'''. | ||
|- | |- | ||
− | | 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> Type |
'''print(“sqrt of”, num, ''''''“is”, num**0.5)''' | '''print(“sqrt of”, num, ''''''“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''' | + | | 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 ''asterick asterick which is raised to power of'' 0.5''' and 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;"| <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'''. | |
|- | |- | ||
Line 246: | Line 290: | ||
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;"| To exit from the block press | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To exit from the '''block''', press '''Enter''' key twice without entering anything else. |
|- | |- | ||
| 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 the output |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| It printed the square root of each number in the list, which was executed in the '''for loop.''' | + | |
+ | %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 | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| It printed the square root of each number in the '''list''', which was executed in the '''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;"| | | 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;"| Next we will see about '''range | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Next we will see about '''range built-in function''' in '''Python'''. |
|- | |- | ||
Line 265: | 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;"| '''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 | + | The syntax is: '''range start ''comma'' stop ''comma'' step''' |
− | + | |- | |
+ | | 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 | ||
− | '''range ''' | + | '''range()''' function |
− | 1 to 19 with step of 2 | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| |
+ | |||
+ | For example: | ||
+ | |||
+ | '''range ''inside parentheses'' one ''comma'' twenty ''comma'' two '''– generates integers from 1 to 19 with step of 2 | ||
− | '''range | + | '''range ''inside parentheses'' twenty '''– generates integers from 0 to 19 |
|- | |- | ||
Line 279: | Line 342: | ||
Highlight output | 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;"| Note that the ending number that you specify will not be included in the list. | + | | 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 ending number that you specify will not be included in the '''list'''. |
|- | |- | ||
Line 286: | Line 349: | ||
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''' | + | Execute this in the '''Python interpreter'''. |
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Open a new terminal. | ||
+ | | 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. | ||
+ | |||
+ | Open a new '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously. | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[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>[Terminal]</nowiki> | ||
+ | |||
+ | Type | ||
'''python''' | '''python''' | ||
Line 299: | Line 370: | ||
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;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| |
− | + | Start the '''Python interpreter''' by issuing the '''command python '''in the new '''terminal''' and press '''Enter'''. | |
− | Start the '''Python | + | |
Type, | Type, | ||
− | '''for i '''in range '' | + | '''for i '''in range '''''inside parentheses'' one ''comma ''eleven ''colon'' and '''press '''Enter''' |
|- | |- | ||
Line 313: | Line 383: | ||
| 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'''. |
|- | |- | ||
| 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> | ||
+ | Type | ||
'''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''' | + | | 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 | + | So enter four '''spaces''' and then type the following. |
− | '''print | + | '''print ''inside parentheses'' i ''comma inside quotes ''cube is ''comma'' i ''raised to power of'' three''' press '''Enter'''. |
|- | |- | ||
Line 330: | Line 402: | ||
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 | + | | 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. |
|- | |- | ||
| 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! | + | | 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 | + | * how to use the '''Python interpreter''' and |
− | * the '''IPython''' | + | * the '''IPython interpreter''' to specify '''blocks'''. |
|- | |- | ||
Line 352: | Line 424: | ||
| 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''' | + | | 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()''' | + | 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> Type |
− | for i in range(1, | + | for i in range(1, 50, 2): |
print(i) | print(i) | ||
Line 365: | Line 437: | ||
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;"| | + | | 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 clear the '''terminal'''. | ||
+ | |||
+ | for''' i ''in range inside parentheses'' one ''comma'' fifty ''comma'' two ''colon ''' | ||
+ | |||
+ | press '''Enter ''' | ||
+ | |||
− | + | '''print'' inside parentheses'' i''' 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;"| <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 | + | Highlight 1 and 50 in '''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;"| 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. | ||
Line 378: | Line 457: | ||
|- | |- | ||
− | | 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 i.e. 2 |
| 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 394: | Line 473: | ||
In this tutorial, we learnt to, | In this tutorial, we learnt to, | ||
− | # Create blocks in python using''' for''' | + | # Create '''blocks''' in '''python''' using''' for''' |
− | # Indent the blocks of code | + | # Indent the '''blocks''' of code |
− | # Iterate over a list using '''for''' | + | # Iterate over a list using '''for loop''' |
− | # Use the '''range()''' | + | # Use the '''range() function''' |
|- | |- | ||
Line 411: | Line 490: | ||
* False | * False | ||
− | 2. Write a '''for ''' | + | 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: | ||
Line 426: | Line 505: | ||
1. False, '''Indentation''' is essential in '''python'''. | 1. False, '''Indentation''' is essential in '''python'''. | ||
− | 2. | + | 2. '''y '''''equal to''''' one''' |
− | + | ||
− | '''y '''''equal to''''' one''' | + | |
'''for x in range '''''inside parentheses''''' one '''''comma''''' twenty one''''' colon''''' ''' | '''for x in range '''''inside parentheses''''' one '''''comma''''' twenty one''''' colon''''' ''' | ||
Line 436: | Line 513: | ||
'''print '''''inside parentheses '''''y''' | '''print '''''inside parentheses '''''y''' | ||
− | 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 i.e. [1,2,3,4]</nowiki> |
|- | |- | ||
Line 468: | Line 545: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Previous slide | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Previous slide | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This is | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This is Trupti Kini from IIT Bombay signing off. |
Thank you. | Thank you. | ||
|} | |} |
Latest revision as of 19:40, 8 January 2018
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> ... <statement n> Highlight according to narration |
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.
Type %pylab and press Enter. |
Open your text editor. | 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 loop variable iterates over a list of numbers and finds the square root of each number.
The variable names can be any of your choice. |
Highlight the line print(“sqrt of”, num, “is”, num**0.5) | Note that a colon after the for statement indicates the starting of loop body.
|
Highlight the line print(“sqrt of”, num, “is”, num**0.5) | 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 | Save the file as sqrt_num_list.py in the home directory. |
switch back to your terminal. Clear the terminal | 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. |
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 |
We get the square root of the given numbers executed by the for loop.
|
Show Slide
Exercise 1 |
|
[Ipython Terminal]
Type numbers = [4, 9, 16, 25, 36] for num in numbers: Press Enter |
Switch to the terminal.
numbers equal to inside square brackets 4, 9, 16, 25, 36 and press Enter
|
Highlight the three dots | You will notice that, the prompt changes to three dots.
|
[Ipython Terminal]
Highlight the three dots |
Please note that IPython automatically indents the block.
|
[Ipython Terminal] Type
'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 asterick asterick which is raised to power of 0.5 and press Enter |
[Ipython Terminal] | Now we have finished the statements in the block.
|
[Ipython Terminal]
press Enter twice |
To exit from the block, press Enter key twice without entering anything else. |
[Ipython Terminal]
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 |
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 start comma stop comma step |
Show Slide
range() function |
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. |
Open a new terminal. | Let us now try to run the for loop in a Python terminal window.
Open a new terminal by pressing Ctrl+Alt+T keys simultaneously. |
[Terminal]
Type python for i in range(1, 11): press Enter |
Start the Python interpreter by issuing the command python in the new terminal and press Enter. Type, for i in range inside parentheses one comma eleven colon and 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]
Type print(i, "cube is", i**3) |
The Python interpreter does not indent the code automatically.
print inside parentheses i comma inside quotes cube is comma i raised to power of three press Enter. |
[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] Type
for i in range(1, 50, 2): print(i) press Enter twice |
Let us clear the terminal. for i in range inside parentheses one comma fifty comma two colon press Enter
|
[IPython Terminal]
Highlight 1 and 50 in range function |
The first parameter is the starting number of the sequence.
The second parameter is the end of the range. |
Highlight third parameter i.e. 2 | 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
1. Indentation is not mandatory in Python
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,
1. False, Indentation is essential in python. 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 i.e. [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 Trupti Kini from IIT Bombay signing off.
Thank you. |