For Loop in Python | Python for Data Science | Day 5

Hola amigos! 
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. 

For Loop in Python

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:

print(students[0])

print(students[1])

print(students[2])

print(students[3])

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.

For Loop in Python

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:
For Loop in Python
  • When we use for loop on a tuple, it will iterate over each element. See below:
For Loop in Python
  • 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.

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics Manager, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

Author: TheDataMonk

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)