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, are ignoring to brush up 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 then you are good to go with for loop for better understanding. First of let me tell you why these loop s are important in any programming language.
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 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:
4.8 (2,892 ratings)
Look at the difference between the code. For loop helps us to write code in 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. 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 that 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. 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 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, the time to take a look at how can we abort execution at a certain point with the help of a break statement.
color = ["green", "pink", "red"]
for c in color:
if c == "pink":
In the above program, we gave 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)
for letter in ‘Hello John':
print ('current letter :',letter)
In this example, we have used 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 very must essential skills these days as no. of libraries are out there to work in a lot of data generated by different applications worldwide. Rest you know it is a basic to start with robust python programming.
This has been a guide to For Loop in Python. Here we discuss an introduction to For Loop in Python and Flow Diagram Along with Example. You can also go through our other suggested articles to learn more –