Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: List Iteration

check

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

  1. Create a list of numbers from 1 to 10 named numbers.

  2. Iterate over the list using a for loop and print each number squared.

  3. Use list comprehension to create a new list squares where each element is the square of the corresponding number in numbers. Print squares.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 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
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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()

  1. Initialize a variable N to 100, this represents the number of natural numbers we want to sum up.

  2. Initialize a variable sum to 0, we'll use this to keep track of the running total.

  3. Create a for loop using range(), starting from 1 and ending at N+1 (the end of the range is exclusive).

  4. Inside the loop, add the current number i to sum.

  5. After the loop, print sum to get the sum of the first N natural numbers.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 5050
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
N = 100
sum = 0
for i in range(1, N+1):
    sum += i
print(sum)

Practice: Countdown using while

  1. Initialize a variable count to 10, this represents the countdown start.

  2. Create a while loop that continues as long as count is greater than 0.

  3. Inside the loop, print the current count and then decrement count by 1.

  4. Once the loop completes (when count is no longer greater than 0), print "Liftoff!".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
> Liftoff!
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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.