
Syntax: List Iteration
Repeating operations on all values in a list with iteration.
In Python, you can use loops to iterate over all the elements in a list. This is very handy when you want to perform an operation on all items in a list.
Iterating through Lists
The for loop in Python allows us to iterate over every element of a list.
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
> 1
> 2
> 3
> 4
> 5
List Comprehension
Python also provides a powerful feature called list comprehension which can be used to create new lists based on existing lists.
numbers = [1, 2, 3, 4, 5]
squares = [number ** 2 for number in numbers]
print(squares)
> [1, 4, 9, 16, 25]
Looping with for and range()
In Python, range() function generates a sequence of numbers, which is often used in combination with a for loop to repeat an operation a certain number of times.
for i in range(5):
print(i)
> 0
> 1
> 2
> 3
> 4
In the above code, the for loop iterates over the sequence of numbers generated by range(5), which are 0 to 4.
Looping with while
while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
i = 0
while i < 5:
print(i)
i += 1
> 0
> 1
> 2
> 3
> 4
In the above code, the while loop prints numbers from 0 to 4. When i becomes 5, the condition i < 5 becomes false, so the loop terminates.
Practice: Iterating through a List of Numbers
-
Create a list of numbers from 1 to 10 named
numbers. -
Iterate over the list using a
forloop and print each number squared. -
Use list comprehension to create a new list
squareswhere each element is the square of the corresponding number innumbers. Printsquares.
HINT*: Click here for a hint
> 1
> 4
> 9
> 16
> 25
> 36
> 49
> 64
> 81
> 100
> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
SOLUTION*: Click here for an example solution
numbers = list(range(1, 11))
for number in numbers:
print(number ** 2)
squares = [number ** 2 for number in numbers]
print(squares)
Practice: Summing Numbers using for and range()
-
Initialize a variable
Nto 100, this represents the number of natural numbers we want to sum up. -
Initialize a variable
sumto 0, we'll use this to keep track of the running total. -
Create a
forloop usingrange(), starting from 1 and ending atN+1(the end of the range is exclusive). -
Inside the loop, add the current number
itosum. -
After the loop, print
sumto get the sum of the first N natural numbers.
HINT*: Click here for a hint
> 5050
SOLUTION*: Click here for an example solution
N = 100
sum = 0
for i in range(1, N+1):
sum += i
print(sum)
Practice: Countdown using while
-
Initialize a variable
countto 10, this represents the countdown start. -
Create a
whileloop that continues as long ascountis greater than 0. -
Inside the loop, print the current
countand then decrementcountby 1. -
Once the loop completes (when
countis no longer greater than 0), print "Liftoff!".
HINT*: Click here for a hint
> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
> Liftoff!
SOLUTION*: Click here for an example solution
count = 10
while count > 0:
print(count)
count -= 1
print("Liftoff!")
Congratulations! This lesson gave you a comprehensive tour of Python loops, empowering you with tools to tackle repetitive tasks and complex data manipulations.