Python-3.4.3/C3/Getting-started-with-for/English-timed
|
|
00:01 | Hello Friends. Welcome to the tutorial on "Getting started with for loops". |
00:08 | At the end of this tutorial, you will be able to,
Use the for loop. Use range() function. |
00:17 | To record this tutorial, I am using,
Ubuntu Linux 14.04 operating system |
00:25 | Python 3.4.3 and IPython 5.1.0 |
00:32 | To practice this tutorial, you should know how to use lists. |
00:37 | If not, see the relevant Python tutorials on this website. |
00:42 | First let us see the syntax of for loop. |
00:46 | for statement iterates over the members of a sequence in order, executing the block each time. |
00:54 | Here the loop variable takes the value of the item inside the sequence on each iteration. |
01:01 | For each item, the loop body is executed. |
01:05 | We will see an example of for loop and how to execute it. |
01:10 | Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. |
01:17 | Now, type ipython3 and press Enter. |
01:22 | Let us initialise the pylab package.
Type %pylab and press Enter. |
01:30 | Open your text editor. |
01:33 | Let us write a for loop. Type the code as shown here: |
01:39 | Here the loop variable iterates over a list of numbers and finds the square root of each number. |
01:46 | The numbers are: 4, 9, 16, 25, and 36. |
01:52 | Note that here we used two variables, numbers which is a list of numbers |
02:00 | 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. |
02:11 | Note that a colon after the for statement indicates the starting of loop body. |
02:18 | Every statement in loop starts with 4 spaces. |
02:24 | It means that, the line is a block of code in for loop. |
02:29 | In this example, it is only a single statement in the block. |
02:35 | 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. |
02:46 | And the lines after that don't fall in the scope of the for loop. |
02:51 | Thus each block is separated by the indentation level.
This marks the importance of white-spaces in Python. |
03:01 | Save the file as sqrt_num_list.py in the home directory. |
03:13 | Now switch back to your terminal.
Let us clear the terminal. |
03:19 | Run the script using the run command as percent run minus i filename and press Enter. |
03:31 | We get the square root of the given numbers executed by the for loop. |
03:37 | This is the print statement output executed after the for loop. |
03:42 | Use the same example we used in sqrt_num_list.py |
03:48 | Type each line of the code in the IPython interpreter prompt. |
03:53 | Skip the line: print("This is outside for-loop") |
03:58 | Switch to the terminal. |
04:01 | Type,numbers equal to inside square brackets 4, 9, 16, 25, 36 and press Enter |
04:20 | for num in numbers colon Press Enter |
04:27 | You will notice that, the prompt changes to three dots.
And the cursor is not after the three dots but, there are four spaces from the three dots. |
04:40 | Please note that IPython automatically indents the block.
The three dots tell you that you are inside a block. |
04:50 | 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 |
05:19 | Now we have finished the statements in the block.
But still the interpreter is showing three dots. |
05:26 | This means that you are still inside the block. |
05:30 | To exit from the block, press Enter key twice without entering anything else. |
05:37 | It printed the square root of each number in the list, which was executed in the for loop. |
05:44 | Next we will see about range built-in function in Python. |
05:48 | range() function generates a list of integers. |
05:52 | The syntax is: range start comma stop comma step |
05:59 | For example: range inside parentheses one comma twenty comma two generates integers from 1 to 19 with step of 2 |
06:11 | range inside parentheses twenty generates integers from 0 to 19 |
06:18 | Note that the ending number that you specify will not be included in the list. |
06:25 | Find out the cube of all the numbers from one to ten.
Execute this in the Python interpreter. |
06:34 | Let us now try to run the for loop in a Python terminal window. |
06:40 | Open a new terminal by pressing Ctrl+Alt+T keys simultaneously. |
06:46 | Start the Python interpreter by issuing the command python in the new terminal and press Enter. |
06:54 | Type, for i in range inside parentheses one comma eleven colon and press Enter |
07:06 | 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. |
07:17 | The Python interpreter does not indent the code automatically.
So enter four spaces and then type the following. |
07:27 | print inside parentheses i comma inside quotes cube is comma i raised to power of three press Enter. |
07:44 | Now when we hit Enter, we still see the three dots.
To get out of the block, press Enter once again. |
07:53 | Okay! So the main thing we learnt here is - how to use the Python interpreter and
the IPython interpreter to specify blocks. |
08:03 | Print all the odd numbers from 1 to 50. |
08:07 | Let us do it in our IPython interpreter for ease of use. |
08:12 | The problem can be solved by just using the range() function. |
08:17 | Let us clear the terminal. |
08:20 | for i in range inside parentheses one comma fifty comma two colon
press Enter print inside parentheses i press Enter twice. |
08:40 | The first parameter is the starting number of the sequence. |
08:45 | The second parameter is the end of the range. |
08:49 | Note that the sequence does not include the ending number.
The third parameter is for stepping through the sequence. |
08:59 | Here we gave two which means we are skipping every alternate element. |
09:05 | This brings us to the end of the tutorial.
In this tutorial, we learnt to, Create blocks in python using for |
09:15 | Indent the blocks of code |
09:17 | Iterate over a list using for loop and Use the range() function |
09:23 | Here are some self assessment questions for you to solve
Indentation is not mandatory in Python True or False |
09:32 | Write a for loop to print the product of all natural numbers from 1 to 20.
What will be the output of: range(1, 5) |
09:43 | And the answers,
1. False, Indentation is essential in python. |
09:51 | 2. y equal to one
'for x in range one comma twenty one |
09:58 | y into equal to x
print y |
10:03 | 3. range(1, 5) will produce a list of integers from 1 to 4 i.e. [1,2,3,4] |
10:14 | Please post your timed queries in this forum. |
10:18 | Please post your general queries on Python in this forum. |
10:23 | FOSSEE team coordinates the TBC project. |
10:27 | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
10:37 | This is Trupti Kini from IIT Bombay signing off.
Thank you. |