Python offers various ways to loop and iterate over elements, making it highly efficient and readable for traversing through data structures like lists, tuples, dictionaries, and sets. This document explores the different methods and nuances associated with looping and iterating in Python.
In Python, while loops are used for repeated execution as long as an expression is true.
count = 0
while count < 5:
print(count)
count += 1
The for loop in Python is used for iterating over a sequence (like a list, tuple, set, or string) or other iterable objects. The basic syntax is as follows:
for element in sequence:
# Do something with element
myList = [1, 2, 3, 4, 5]
for number in myList:
print(number)
for char in "Hello":
print(char)
Python allows using one loop inside another loop.
myListA = [1, 2, 3, 4, 5]
myListB = [1, 2, 3]
for i in myListA: # Outer loop
for j in myListB: # Inner loop
print(i, end=" ")
print()
Loop control statements change the execution from its normal sequence.
break Statement
Terminates the loop and transfers execution to the statement immediately following the loop.
for number in myList:
if number == 3:
break
print(number)
continue Statement
Causes the loop to skip the remainder of its body and immediately retest its condition.
for number in myList:
if number == 3:
continue
print(number)
pass Statement
The pass statement is a null operation; nothing happens when it executes. It's used as a placeholder.
for number in myList:
if number == 3:
pass
print(number)
In Python, the range()
function returns a range object, which is an iterator that yields a sequence of numbers. This range object does not store all its values in memory; it generates the numbers on demand during iteration. However, this means you cannot directly access an element by its index as you would with a list. To access a specific item by its index, you need to convert the range object into a list.
# Using range
for num in range(5):
print(num) # This works fine for iteration
# Directly accessing an item from range - This will cause an error
# print(range(5)[2])
# Converting to list and accessing an item
numbers_list = list(range(5))
print(numbers_list[2]) # This will print 2
Specifying Start, Stop, and Step
for i in range(2, 10, 2): # Starts at 2, stops before 10, steps by 2
print(i)
Using enumerate()
Sometimes it's useful to have access to the indices of the values when iterating.
for index, value in enumerate(myList):
print(index, value)
Using zip()
zip() is used to iterate over two or more lists in parallel.
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for number, letter in zip(list1, list2):
print(number, letter)