Day - 5 Python Loops


Using the for loop with Python Lists
Using a for
loop with Python lists allows you to iterate through
each element in the list, one at a time.
This is a fundamental way to work with lists in Python,
making it easier to process or manipulate the elements.
1. Iterating Over List Items
You can use a for
loop to access and perform operations on each item in the list.
Example:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
2. Using the range()
Function
Sometimes, you want to iterate using the index of the items. You can use the range()
function along with the len()
function to achieve this.
Example:
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
print(f"Index {i}: {fruits[i]}")
Output:
Index 0: apple
Index 1: banana
Index 2: cherry
3. Performing Operations on List Items
We can perform calculations, modifications, or any other operations on the elements.
Example:
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for num in numbers:
squared_numbers.append(num ** 2)
print(squared_numbers)
Output:
[1, 4, 9, 16, 25]
4. Nested for
Loops with Lists
When working with nested lists (lists within lists), use nested for
loops.
Example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
for item in row:
print(item, end=' ')
Output:
1 2 3 4 5 6 7 8 9
Overview
Use a for
loop to iterate over each element in a list.
Access elements by index using range()
.
max()
Function in Python
The max()
function in Python is used to find the maximum value in a sequence or among multiple values. It works with various data types, including numbers, strings, and more.
Return Value
The function returns:
The largest item in the iterable.
The largest of the provided arguments.
Examples
1. Maximum in a List
numbers = [10, 20, 30, 40, 50]
print(max(numbers))
Output
50
2. Maximum Among Multiple Values
print(max(3, 7, 2, 9))
Output
9
for loops and the range ( ) function in Python
For Loops in Python
A for
loop in Python is used to iterate over a sequence
(like a list, tuple, string, or range) and execute a block of code
for each item in the sequence.
variable
: Takes the value of each item in the sequence, one by one.sequence
: The data structure or iterable you want to loop over.
Example:
Looping Through a List
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
The range()
Function
The range()
function generates a sequence of numbers, commonly used with for
loops. It doesn't create a list but provides a range object that is iterable.
start
: The starting number (inclusive). The default is 0.stop
: The ending number (exclusive). This is required.step
: The increment (default is 1).
Examples of range()
Simple Range
for i in range(5): print(i)
Output:
0 1 2 3 4
Range with Start and Stop
for i in range(2, 6): print(i)
Output:
2 3 4 5
Range with Step
for i in range(0, 10, 2): print(i)
Output:
0 2 4 6 8
Range in Reverse
for i in range(10, 0, -2): print(i)
Output:
10 8 6 4 2
Key Points
For Loop: Iterates through each item in a sequence.
Range Function: Generates a sequence of numbers, which can be
customized using
start
,stop
, andstep
values.Combination: Use
range()
withfor
loops for precise control over iteration.What’s the next milestone?
On Day 6 of the 100 Days of Python, I will learn about Python functions and Karel.
Subscribe to my newsletter
Read articles from Muniba Amin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
