🔄 For Loops¶
In Python, for loop is used to iterate over a sequence (like list, tuple, string) or other iterable objects.
Here's a basic example:
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
apple banana cherry
1. Enumerate¶
enumerate is a built-in function of Python that allows you to loop over a list and have an automatic counter. It returns an enumerate object which contains the index and value of all the items of list as a tuple.
Here's how you use it:
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(f"The fruit at index {i} is {fruit}.")
The fruit at index 0 is apple. The fruit at index 1 is banana. The fruit at index 2 is cherry.
2. Range¶
range is a built-in function that generates a sequence of numbers over time. You can specify the start and end values, as well as the step size.
Here's how you can use range in a for loop:
for i in range(5): # generates numbers from 0 to 4
print(i)
0 1 2 3 4
You can also specify the start, stop, and step size with range:
for i in range(2, 10, 2): # generates numbers from 2 to 10 with a step size of 2
print(i)
2 4 6 8
3. Zip¶
zip is a built-in function that combines two or more sequences into tuples. This can be useful when you need to iterate over two lists in parallel.
Here's an example:
fruits = ["apple", "banana", "cherry"]
colors = ["red", "yellow", "red"]
for fruit, color in zip(fruits, colors):
print(f"The {fruit} is {color}.")
The apple is red. The banana is yellow. The cherry is red.
In this example, zip combines the fruits and colors lists into tuples. Each tuple contains one fruit and one color, and the for loop iterates over these tuples.
4. List Comprehensions¶
List comprehensions provide a concise way to create lists based on existing lists (or other iterable objects). The resulting list comprehension consists of an output expression followed by one or more for statements and zero or more if statements.
Here's an example of a list comprehension:
numbers = [1, 2, 3, 4, 5]
squares = [n**2 for n in numbers]
print(squares) # prints: [1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
In this example, n**2 is the output expression, for n in numbers is the for statement. The list comprehension [n**2 for n in numbers] creates a new list where each element is the square of the corresponding element in numbers.
You can also include if statements in a list comprehension to create a new list based on a condition:
numbers = [1, 2, 3, 4, 5]
even_squares = [n**2 for n in numbers if n % 2 == 0]
print(even_squares) # prints: [4, 16]
[4, 16]
In this example, the list comprehension creates a new list where each element is the square of the corresponding element in numbers, but only if that element is even.
5. Dictionary Comprehensions¶
Like list comprehensions, dictionary comprehensions provide a concise way to create dictionaries. The resulting dictionary comprehension consists of an output expression in the form key: value, followed by one or more for statements and zero or more if statements.
Here's an example of a dictionary comprehension:
numbers = [1, 2, 3, 4, 5]
squares = {n: n**2 for n in numbers}
print(squares) # prints: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
In this example, n: n**2 is the output expression, for n in numbers is the for statement. The dictionary comprehension {n: n**2 for n in numbers} creates a new dictionary where each key is a number from numbers, and its corresponding value is the square of that number.
You can also include if statements in a dictionary comprehension to create a new dictionary based on a condition:
numbers = [1, 2, 3, 4, 5]
even_squares = {n: n**2 for n in numbers if n % 2 == 0}
print(even_squares) # prints: {2: 4, 4: 16}
{2: 4, 4: 16}
In this example, the dictionary comprehension creates a new dictionary where each key is a number from numbers and its corresponding value is the square of that number, but only if that number is even.