JAVA | C TUTORIAL | C++ | KOTLIN | SWIFT | R TUTORIAL
TUTORIALS EXAMPLES BUILT-IN FUNCTIONS GET APP
Contents
PYTHON TUTORIAL Python for Loop What is for loop in
Python?
Python Introduction In this article, you'll learn to iterate over a sequence of elements using the Syntax of for Loop
di erent variations of for loop.
Python Flow Control Flowchart of for loop
-- Python if...else Example: Python for
Loop
-- Python for Loop
What is for loop in Python? The range() function
-- Python while Loop
The for loop in Python is used to iterate over a sequence (list, tuple, string) or for loop with else
-- Python break and continue
other iterable objects. Iterating over a sequence is called traversal.
-- Python Pass
-- Take Quiz
Syntax of for Loop
for val in sequence:
Python Functions Body of for
Python Datatypes
Python Files
Here, val is the variable that takes the value of the item inside the sequence
Python Object & Class
on each iteration.
Python Advanced Topics
Python Date and time Loop continues until we reach the last item in the sequence. The body of for
loop is separated from the rest of the code using indentation.
Flowchart of for Loop
Receive the latest tutorial
to improve your
programming skills.
Enter Email Address* Join
Example: Python for Loop
script.py IPython Shell
1 # Program to find the sum of all numbers stored in a list
2
3 # List of numbers
4 numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
5
6 # variable to store the sum
7 sum = 0
8
9 # iterate over the list
10 for val in numbers:
11 sum = sum+val
12
13 # Output: The sum is 48
14 print("The sum is", sum)
Run
RECOMMENDED READINGS
Powered by DataCamp
Python Program to Print the when you run the program, the output will be:
Fibonacci sequence
Python while Loop The sum is 48
Python Program to Print all Prime
Numbers in an Interval
Python Program to Display Fibonacci The range() function
Sequence Using Recursion
We can generate a sequence of numbers using range() function. range(10) will
Python List
generate numbers from 0 to 9 (10 numbers).
We can also de ne the start, stop and step size as range(start,stop,step size) .
step size defaults to 1 if not provided.
This function does not store all the values in memory, it would be ine cient. So
it remembers the start, stop, step size and generates the next number on the
go.
To force this function to output all the items, we can use the function list() .
The following example will clarify this.
# Output: range(0, 10)
print(range(10))
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(10)))
# Output: [2, 3, 4, 5, 6, 7]
print(list(range(2, 8)))
# Output: [2, 5, 8, 11, 14, 17]
print(list(range(2, 20, 3)))
We can use the range() function in for loops to iterate through a sequence of
numbers. It can be combined with the len() function to iterate though a
sequence using indexing. Here is an example.
script.py IPython Shell
1 # Program to iterate through a list using indexing
2
3 genre = ['pop', 'rock', 'jazz']
4
5 # iterate over the list using index
6 for i in range(len(genre)):
7 print("I like", genre[i])
Run
Powered by DataCamp
When you run the program, the output will be:
I like pop
I like rock
I like jazz
for loop with else
A for loop can have an optional else block as well. The else part is executed if
the items in the sequence used in for loop exhausts.
break statement can be used to stop a for loop. In such case, the else part is
ignored.
Hence, a for loop's else part runs if no break occurs.
Here is an example to illustrate this.
script.py IPython Shell
1 digits = [0, 1, 5]
2
3 for i in digits:
4 print(i)
5 else:
6 print("No items left.")
Run
Powered by DataCamp
When you run the program, the output will be:
0
1
5
No items left.
Here, the for loop prints items of the list until the loop exhausts. When the for
loop exhausts, it executes the block of code in the else and prints
No items left.
« »
PREVIOUS NEXT
PYTHON IF...ELIF...ELSE AND PYTHON WHILE LOOP
NESTED IF
TUTORIALS EXAMPLES COMPANY LEGAL
Python Tutorials Python Examples About Privacy Policy
C Tutorials C Examples Advertising Terms And Conditions
Java Tutorials Java Examples Contact App's Privacy Policy
Kotlin Tutorials Kotlin Examples App's Terms And Conditions
C++ Tutorials C++ Examples
Swift Tutorials R Examples
R Tutorials
Algorithms Tutorials
Copyright © Parewa Labs Pvt. Ltd. All rights reserved.