For Loop in Python | Python for Data Science | Day 5
Your friends at The Data Monk are back with another absorbing topic- For Loop in Python. In vanilla terms, a For Loop in Python is used to execute a set of instructions for “n” no. of times i.e. it is used to iterate over instructions.
Given a list of students, say students=[ “Amyra”, “Baskin”, “Chandler”, “Dave” ]
If you have been asked to print the name of all the students in the list, you would probably do this:
But what if you are asked to print student names from a list of 1000 students. How tedious will it be to write the print statement 1000 times? Phew!
When we’re stuck in a problem like this, for loop rescues!
Let’s get coding.
In the snippet above, “students” is a list and “child” is the iterator.
When we use the for loop above, we are actually telling the compiler to execute the print statement for each child in the list of students.
You may also refer to https://www.w3schools.com/python/python_for_loops.asp for some other interesting examples on for loops.
Not just lists, for loops can be used with tuples, strings and dictionaries. Let’s see how.
- When you use for loop with a string, it will iterate over each character of the string to produce an output like one shown below:
- When we use for loop on a tuple, it will iterate over each element. See below:
- When we use for loop on a dictionary, it iterates over keys of the dictionary.
Another important aspect of for loop is that you can use a for loop inside another for loop. This concept is called nested looping. Try the code snippet given below:
If you have been following this article carefully, you must know how this output is produced. If not, here’s why:
For each element in list1, print each value in list 2. This implies ↴
While we are on A from list1, we print it with 1, followed by 2, 3 and 4. This process is repeated for all values in list1, yielding the output above.
A very noteworthy function used in Python, generally along with for loop is the range() function.
Range function is named aptly, it is used to specify the range for iteration.
Try the code below:
Carefully note that we specified the range as 5 but 0,1,2,3,4 were printed. This is because range always starts from 0 unless explicitly specified. Also, it prints till n-1 where n is the value specified within the brackets. If you want it to start at 1, refer to the code below:
The range() function can also be used while iterating over lists.
range() also has a parameter – step size which lets you fix the number of steps you need to take for an iteration. See below:
Mostly, we use a for loop to iterate a block of code. But in some specific cases, it may be required that the for loop does “nothing”. In code, “do nothing” is implemented by the use of pass. See below:
It does exactly what is meant to do. It produces no output, does nothing!
Now that you know quite a few things about the for loop, let’s test our knowledge by attempting the questions below. We have provided the answers for you, but again, we recommend that you give them a shot without any assistance.
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk