In Python, for
is a looping keyword that helps iterate through sequences, such as lists, tuples, dictionaries, and strings. By using for
, you can perform various tasks on each item of an iterable. It allows you to execute a block of code repeatedly for each item in an iterable until you’ve looped through all the elements.
The syntax for a for
loop is:
for <variable> in <iterable>: <statement 1> <statement 2> ... <statement n>
Here, the <iterable>
is an object that can return its elements one at a time, and the <variable>
refers to each element of the iterable in turn.
In this tutorial, you’ll learn the various ways to use the for
keyword to loop through iterables in Python.
Using “for” to Loop Through a List
A list is an ordered collection of elements, and it is iterable in Python. You can use a for
loop to cycle through each item of a list and perform operations on them.
Suppose you have a list of languages:
languages = [‘Python’, ‘C++’, ‘Java’, ‘PHP’, ‘JavaScript’]
To loop through this list, you can use the following code:
languages = ['Python', 'C++', 'Java', 'PHP', 'JavaScript'] for language in languages: print(language)
This code will output:
Python C++ Java PHP JavaScript
You can use the range()
function and the len()
function with the for
loop to access indices and elements of a list:
languages = ['Python', 'C++', 'Java', 'PHP', 'JavaScript'] for index in range(len(languages)): print(languages[index], " is at index ", index)
You can also loop through a list of numbers and perform arithmetic operations on them:
numbers = [1, 2, 3, 4, 5] sum = 0 for number in numbers: sum += number print("Sum of numbers in the list is: ", sum)
This will output:
Sum of numbers in the list is: 15
Using “for” to Loop Through a Tuple
A tuple is an immutable collection of elements, and it is iterable. You can use for
to loop through a tuple just as you would for a list:
fruits = ('apple', 'banana', 'orange') for fruit in fruits: print(fruit)
This will output:
apple banana orange
Similarly, you can use the range()
function and the len()
function with the for
loop to access indices and elements of a tuple:
fruits = ('apple', 'banana', 'orange') for index in range(len(fruits)): print(fruits[index], " is at index ", index)
You can’t modify a tuple, but you can define a tuple with just one element by following it with a comma:
t = (5,)
Using “for” to Loop Through a Dictionary
A dictionary is a collection of key-value pairs, and it is iterable in Python. To loop through a dictionary, you can use a for
loop that iterates over its keys, values, or items:
person = {'name': 'John', 'age': 24, 'country': 'USA'} for key in person: print(key, ":", person[key])
This will output:
name : John age : 24 country : USA
You can also use the values()
method to loop through the values of a dictionary:
for value in person.values(): print(value)
This will output:
John 24 USA
Or, you can use the items()
method to loop through both key-value pairs:
for key, value in person.items(): print(key, ":", value)
This will output:
name : John age : 24 country : USA
Using “for” to Loop Through a String
A string is an iterable in Python, and you can use the for
loop to cycle through each character of a string:
greeting = "Hello, World!" for char in greeting: print(char)
This will output:
H e l l o , W o r l d !
Similarly, you can use the range()
function and the len()
function with the for
loop to access indices and characters of a string:
greeting = "Hello, World!" for index in range(len(greeting)): print(greeting[index], " is at index ", index)
This will output:
H is at index 0 e is at index 1 l is at index 2 l is at index 3 o is at index 4 , is at index 5 W is at index 7 o is at index 8 r is at index 9 l is at index 10 d is at index 11 ! is at index 12
Nested “for” Loops
You can use nested for
loops to loop through multiple sequences simultaneously. For example, you can loop through a list of colors and a list of fruits:
colors = ['red', 'green', 'blue'] fruits = ['apple', 'banana', 'orange'] for color in colors: for fruit in fruits: print(color, fruit)
This will output:
red apple red banana red orange green apple green banana green orange blue apple blue banana blue orange
Conclusion
The for
loop is a powerful tool for iterating through sequences in Python. It helps you automate repetitive tasks and perform operations on each element of an iterable. With the for
keyword, you can loop through various types of iterables, such as lists, tuples, dictionaries, and strings. Understanding how to use the for
loop correctly is essential for any Python programmer.