So far we have learned how to make lists and access the individual items/elements in a list. Often it is required for programmers to run through all entries in a list and perform the same task with each element of the list.
For example from our motorbikes list we need to print the names of all the motorbikes we have in the list. Now we could do this by retrieving each name from the list individually, but this approach could cause several problems. For one, it would be repetitive to do this with a long list of names. Also, we’d have to change our code each time the list’s length changed.
These issues can be avoided by Looping. Looping allows you to take the same action, or set of actions, with every item in a list. As a result, you’ll be able to work efficiently with lists of any length, including those with thousands or even millions of items. So if we use a for loop for our example it'll avoid both of the issues we just discussed if we try by retrieving each name from the list individually.
So let’s use a for loop to print out each motorbike in a list of motorbikes :
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
for motorbike in motorbikes:
print(motorbike)
See the screen shot below for the output:
We started with defining our motorbikes list.
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
Then we defined a for loop which tells Python to pull a name from the list motorbikes , and store it in the variable motorbike .
for motorbike in motorbikes:
Next we tell Python to print the name that was just stored in motorbike.
print(motorbike)
Because the list contains more values, Python returns to the first line of the loop for motorbike in motorbikes:
Python retrieves the next name in the list,and stores that value in motorbike . Python then executes the line: print(motorbike) and prints the current value of motorbike again. Python repeats the entire loop till the last value in the list is retrieved and when nothing comes after the for loop, the program simply ends.
It might help to read this code as -
“For every motorbike in the list of motorbikes , print the motorbike’s name.”
The set of steps is repeated once for each item in the list, no matter how many items are in the list. If you have a million items in your list, Python repeats these steps a million times—and usually very quickly.
In the above program we have seen that the program ends once all the items in the list are printed and usually it'll be better if we end our program with some message which will be a more user friendly way. So if I add the following line in my program it will end with a message "These are the bikes I've owned so far!"
print("These are the bikes I've owned so far!")
See the screen shot below for the program and output:
Not that the statement print("These are the bikes I've owned so far!") was written outside the scope of for loop (see the indentation). If it was within the for loop as shown below the the output would be totally different from what we desired.
See the screen shot below for the program and output:
Both print statements are repeated once for each motorbike in the list hence the message we wanted to put only once in the end was printed after each motorbike's name was printed.
When we write code that relies on proper indentation, we’ll need to watch for a few common indentation errors.
Just try this code and see what happens-
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
for motorbike in motorbikes:
print(motorbike)
If we accidentally indent a line that doesn’t need to be indented, Python informs us about the unexpected indent, example:
message = "Indentation in Python"
print (message)
See the screen shot below for the program and output:
We can avoid unexpected indentation errors by indenting only when we have a specific reason to do so. In the programs we’re writing at this point, the only lines you should indent are the actions we want to repeat for each item in a for loop.
Another common mistake is forgetting the colon at the end of a for statement which actually tells Python to interpret the next line as the start of a loop. For example-
for motorbike in motorbikes
print(motorbike)
If we run this we'll get a syntax error because Python doesn’t know what you’re trying to do. Although this is an easy error to fix, it’s not always an easy error to find. See the screen shot below for the program and output:
It is advisable to make some programs using lists and try to access the list items using for loop. Try to print each item with a message, say "Honda is a nice bike". See if you are not making any indentation errors.
In the next post we will continue with list and learn more about their usage. Keep studying and remember that 'Python is easy to learn'.
For example from our motorbikes list we need to print the names of all the motorbikes we have in the list. Now we could do this by retrieving each name from the list individually, but this approach could cause several problems. For one, it would be repetitive to do this with a long list of names. Also, we’d have to change our code each time the list’s length changed.
These issues can be avoided by Looping. Looping allows you to take the same action, or set of actions, with every item in a list. As a result, you’ll be able to work efficiently with lists of any length, including those with thousands or even millions of items. So if we use a for loop for our example it'll avoid both of the issues we just discussed if we try by retrieving each name from the list individually.
So let’s use a for loop to print out each motorbike in a list of motorbikes :
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
for motorbike in motorbikes:
print(motorbike)
See the screen shot below for the output:
We started with defining our motorbikes list.
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
Then we defined a for loop which tells Python to pull a name from the list motorbikes , and store it in the variable motorbike .
for motorbike in motorbikes:
Next we tell Python to print the name that was just stored in motorbike.
print(motorbike)
Because the list contains more values, Python returns to the first line of the loop for motorbike in motorbikes:
Python retrieves the next name in the list,and stores that value in motorbike . Python then executes the line: print(motorbike) and prints the current value of motorbike again. Python repeats the entire loop till the last value in the list is retrieved and when nothing comes after the for loop, the program simply ends.
It might help to read this code as -
“For every motorbike in the list of motorbikes , print the motorbike’s name.”
The set of steps is repeated once for each item in the list, no matter how many items are in the list. If you have a million items in your list, Python repeats these steps a million times—and usually very quickly.
In the above program we have seen that the program ends once all the items in the list are printed and usually it'll be better if we end our program with some message which will be a more user friendly way. So if I add the following line in my program it will end with a message "These are the bikes I've owned so far!"
print("These are the bikes I've owned so far!")
See the screen shot below for the program and output:
Not that the statement print("These are the bikes I've owned so far!") was written outside the scope of for loop (see the indentation). If it was within the for loop as shown below the the output would be totally different from what we desired.
See the screen shot below for the program and output:
Both print statements are repeated once for each motorbike in the list hence the message we wanted to put only once in the end was printed after each motorbike's name was printed.
When we write code that relies on proper indentation, we’ll need to watch for a few common indentation errors.
Just try this code and see what happens-
motorbikes = ['Honda','Yamaha','Suzuki','Royal Enfield', 'Harley Davidson']
for motorbike in motorbikes:
print(motorbike)
If we accidentally indent a line that doesn’t need to be indented, Python informs us about the unexpected indent, example:
message = "Indentation in Python"
print (message)
See the screen shot below for the program and output:
We can avoid unexpected indentation errors by indenting only when we have a specific reason to do so. In the programs we’re writing at this point, the only lines you should indent are the actions we want to repeat for each item in a for loop.
Another common mistake is forgetting the colon at the end of a for statement which actually tells Python to interpret the next line as the start of a loop. For example-
for motorbike in motorbikes
print(motorbike)
If we run this we'll get a syntax error because Python doesn’t know what you’re trying to do. Although this is an easy error to fix, it’s not always an easy error to find. See the screen shot below for the program and output:
It is advisable to make some programs using lists and try to access the list items using for loop. Try to print each item with a message, say "Honda is a nice bike". See if you are not making any indentation errors.
In the next post we will continue with list and learn more about their usage. Keep studying and remember that 'Python is easy to learn'.
0 comments:
Post a Comment