Python For Loop With Example Part-1 of 3
Python for loop is one the basic concept or building block of Python. Learning any language is not a challenge when you have enough details with an example to understand the concept. Python is no different. In fact, Python is one of the easiest languages with the efficiency of powerful language. In this tutorial, I will try to explain the concept of Python for loop. This will not only help your language but also save you time when you have to do the repetitive task in the program.
Loops in any traditional programming language (Python, in our case) is used when you need a specific set of code lines to be executed for a specific number of times.
There are two kinds of loops in Python
- For Loop – When the number of iterations is known
- While loop – When the iteration is decided at condition base.
Python is not going to be any different in this case, so it does the same, iterates over the block of code within the for loop and executes it by the number of times as specified. On the contrary, you would want to check your understanding as against the while loop, which also does the same thing, but does it based on a condition to be satisfied or executes it until infinity.
What is a Python for Loop?
A for loop is used to repeat a piece of code n number of times. The for loop is usually used with a list of things. The basic syntax for the for loop looks like this:
for item in list: things to do
Translated into common English, this would be: “For each item that is present in the list, do the thing”.
There are a few things to note here:
- Every for loop must reference a list or a range.
- Every for loop must close with a colon.
Code to be executed as part of the for loop must be indented by four spaces (or one press of the Tab key) or single while space. You can also read the official Python Style Guide to know more about python styling.
To use individual items in the list or range, we have to create a variable (item in the syntax above). There’s no need to declare this variable before creating the for loop. You can also reuse the same variable for other for loops.
You’ve used for loops extensively in your own life. As an example, suppose you have to go grocery shopping. You walk into the grocery store, grab a cart, and pull out your shopping list. As you walk through the aisles, you pull out each item on the shopping list and place it into the cart. In a way, you are using a for loop – for every item present in your shopping list, you pull it out and keep it in your cart.
Python for Loop Example – 01
Print each fruit in a fruit list:
fruits = ["apple", "banana", "litchi"] for x in fruits: print(x)
Looping Through a String
String is the most common data type used but one of the most complex data types in any programming language. But, Thanks to Python which made this complex object also very simple. You can consider string as group (array) of characters and they contain a sequence of characters:
Python for Loop Example – 02
Loop through the letters in the word “banana”:
for x in "litchi": print(x)
Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the loop is finished:
Python for Loop Example – 03
Print all numbers from 0 to 16, and print a message when the loop has ended:
for x in range(17): print(x) else: print("it’s over")