Introduction to For Loop in Python
In Article For Loop in Python, Any Programming language starts with the same set of rules. The syntax is may be different. Python also has conditional statements and loops. Today we are going to concentrate on loops in python. To be perfect in any language, you must start with the basic concepts. For loop is one of them. This is very important to understand for programmers. Most of the time, we ignore to brush up on these small-small important parts of the language. While learning any programming language, we must start practicing it side by side. The main thing applies while learning for a loop. For loop is confusing to many programmers. It is good practice if you try out code at the time of learning it. Get your hands dirty with the code, so the probability of understanding the concept is faster. So, open your Python IDE (integrated development environment) and start doing it. If you know the basics of python-like keywords, data types, etc., you will go with for loop for better understanding. First of let me tell you why these loops are essential in any programming language.
For Loop in Python Program
Suppose we have to print each letter in the sentence, and for doing this, please look at the below program.
greeting = ‘Hello Python’
#if I wanted to print each letter in greeting sentence without using for loop then we may be #doing something below code
If you look at the above program, it seems like more repetitive we have. It is bulky. We have only 11 characters to shoe for now. Suppose if we have to print letters for the whole paragraph or the article, which may have thousands of words. We have to write a print statement with no limit. As a human being, it is a very tedious task to deal with this.
So, the solution is to iterate over it. Now, we will concentrate on reducing code with the required output. Now it is time to improve our code. Let’s look at below code snippet for the same code which we wrote for the greeting.
greeting = ‘Hello Python’
# here we are taking for loop
for letter in greeting:
Look at the difference between the code. For loop helps us to write code in a simpler way with less code we can iterate over. This minimizes the confusion, reduces lines of code, better understanding and runs faster. Now, you got the idea about why the for loop is necessary for coding and how it reduces our headache if we have to iterate over bulky data. Then, it is time to look at the syntax of for loop in Python.
If you have observed already, you may see that for loop in java or other language has a different syntax than the above written one. Take the same above example, and we will find out how exactly this works.
As you see, we have one variable greeting and That variable we stored on a string ‘Hello Python’. For is a keyword in python. After writing for, we have a variable name letter. The letter is not defined before anywhere here in operator in python, which takes value in Greeting one at a time. And stores it in a letter.
Example of the letter in greeting
Flow Diagram of For Loop in Python
The flow chart below states how to think while working with for loop in python. The flow chart shows the logic of the program. It is recommended to try out the flow chart before coding the actual program.
How For the loop works in python?
For loops in python are designed to loop over any sequence like list, tuple, dictionary, set and string. We have seen already how for loop works in python.
Now is the time to look at how we can abort execution at a certain point with a break statement.
color = ["green", "pink", "red"]
for c in color:
if c == "pink":
In the above program, we gave a condition in the loop if we get the value pink, then stop iterating over. In this way, we can come to a particular outcome. There are many scenarios where we can use this logic.
Examples (proper coding with output)
The examples of for loop in python are as below:
for letter in ‘Hello John':
print ('current letter :',letter)
In this example, we have used the range function in python. This is one of the most used functions in python while working with for loop. This function has owing syntax.
range(starting value, ending value, increment by)
for r in range(1,5,1):
cars =['toyota','tata','honda'] for car in range(len(cars)):
print ("current car:",cars [car])
Nested for a loop. We can nest for a loop as follow. We are iterating over two arrays here.
no = [1, 2] color = ["red", "blue"] for x in no:
for y in color:
Python for loops is very easy to code and to understand. Looping in python while playing with the text is a very must essential skills these days as no. of the libraries are out there to work in a lot of data generated by different applications worldwide. Rest you know it is basic to start with robust python programming.
This has been a guide to For Loop in Python. Here we discuss the basic concept and Flow Diagram Along with the Example. You can also go through our other suggested articles to learn more –