Updated March 30, 2023
Definition of Python 3 For Loop
Python 3 For loop statement is a little different than what we would be used to from C or Pascal. Rather than iterating over an arithmetic progression of numbers or allowing the user to set both the iteration step and the halting condition (as in C), Python’s for statement iterates over the items of any sequence (a list or a string) in the order they occur in the sequence.
What is Python 3 For Loop?
- A For loop is usually used with an iterable object, such as a list or a range. The For statement in Python iterates across the members of a sequence, executing the block each time.
- Compare the For statement to the “while” loop, which is used when a condition needs to be verified each iteration or when a piece of code needs to be repeated indefinitely.
How to Use Python 3 For Loop?
When we have a block of code that we want to repeat a certain number of times, we use a For loop.
- When it comes to iterating through a sequence, a For loop is employed. This behaves more like an iterator function found in other object-oriented programming languages than the keyword in other programming languages.
- The For loop allows us to run a sequence of statements once for each item in a list, tuple, set, and so on.
- A For loop can basically be used with any object that has an iterable method.
- Even strings, despite the lack of an iterable technique but we won’t go into that right now. Having an iterable method simply means that the data may be displayed in a list format, with numerous values arranged in a logical sequence.
Below is the syntax of Python 3 For loop as follows.
Syntax:
for val (variable (in sequence))
Loop body
Val is the variable that, on each iteration, takes the value of the item in the sequence.
The loop is repeated until the last item in the sequence is reached. Indentation is used to distinguish the body of the For loop from the remainder of the code.
The below steps shows how to use python 3 For loop are as follows.
In this step, we use python 3 to print the list of strings. In the below example, we are printing the list of names.
Code:
name = ["ABC", "PQR", "XYZ"]
for x in name:
print (x)
Output:
In this step, we use python 3 to print the single string. In the below example, we are printing the single names.
Code:
for x in "ABCDEF":
print (x)
Output:
In this step returning an iterator that progresses integers from 0 to n-1. It is typecast to list to get a list object of the sequence. The for statement can now be used to iterate this list.
Code:
for num in list (range (5)):
print (num)
Output:
The below step shows a list of values that was iterated through the range of values as follows.
Code:
for p in range(0,10):
print(p)
Output:
Python 3 For loop Data Types
Below is the python 3 For loop data types as follows.
1) Python 3 For loop using range data types
The range function is used in loops to control the number of times the loop is run. We can supply up to three integer arguments to the range when working with it.
The below example shows the use of python 3 For loop data types as follows.
Code:
for p in range(0,10):
print(p)
Output:
2) For loop using sequential data types
- Iteration parameters For loops can be used with lists and other data sequence kinds. We can define a list and iterate through that instead of iterating through a range.
- We’ll create a list and iterate through it. We’re publishing each item in the list in this example. We could have used any legal variable name instead of shark and had the same result.
The below example shows For loop using sequential data types as follows.
Code:
name = ['ABC', 'PQR', 'XYZ', 'CBZ', 'PQR']
for name in name:
print(name)
Output:
3) Nested For loops data types
- A nested loop is a loop that is contained within another loop, comparable to nested if statements in structure.
- The outer loop is encountered first, and the program executes its first iteration. The inner, nested loop is triggered by the first iteration, and it runs to completion.
Code:
num = [11, 12, 13]
alpha = ['abc', 'pqr', 'xyz']
for number in num:
print(number)
for letter in alpha:
print(letter)
Output:
Python 3 For loop Statement
- A Python For loop can traverse over a list of items and perform a series of actions on each one. On each iteration of a For loop, a temporary value is assigned to a variable.
- Remember to properly indent each action while writing a For loop, or else we will get an IndentationError.
Below is an example of For loop statement as follows.
Code:
num = 4
for p in range(0, num):
print (p)
Output:
It is also possible to iterate using the sequence’s index of elements. The fundamental concept is to calculate the list’s length first, then loop through the sequence within that range.
The below example shows the range functions which start with 0 as follows.
Code:
for y in range(10, 16):
print (y)
Output:
Conclusion
The range function is used in loops to control the number of times the loop is run. We can supply up to three integer arguments to the range when working with it. Python 3 For loop statement is a little different than what we would be used to from C.
Recommended Articles
This is a guide to Python 3 For Loop. Here we discuss the definition, What is python 3 For loop, How to use python 3 For loop? Examples with code implementation. You may also have a look at the following articles to learn more –