Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn For Loops | Loops
Introduction to Python

bookFor Loops

In this chapter, we'll dive into how loops serve as a key tool for automating repetitive tasks and are essential for efficiently handling lists and other iterable data types.

Join Alex as he demonstrates the use of for loops to simplify operations in our grocery store:

The usage of for loops in Python allows you to execute a block of code repeatedly for each item in a collection (iterable object). Here's what you need to know about for loops:

Syntax


for variable in iterable:
    # Code to execute for each item

Use a for loop when you want to go through items one by one in a sequence like a list.

You start with the for keyword, then choose a variable name such as item. This variable temporarily stores each element from the sequence. After that, write the in keyword and the iterable object, for example groceryItems.

The code inside the loop runs once for every element in the sequence. On each iteration, item takes the next value from groceryItems, and the print() function outputs it. This continues until all items in the list have been processed.

1234
groceryItems = ["milk", "eggs", "cheese", "butter"] for item in groceryItems: # Code to be executed print(item)
copy

Iteration refers to the process of executing a block of code repeatedly. Python supports two main types of iteration:

Definite Iteration

Where the number of iterations is known in advance. for loops are a classic example, automatically stopping when they reach the end of the sequence.

Indefinite Iteration

In which execution continues until a specific condition is met, typically handled by while loops, which will be explored in detail in the upcoming chapter.

Example Applications

Counting how many items are in the grocery list:

1234567
groceryItems = ["milk", "eggs", "cheese", "bread"] count = 0 for item in groceryItems: count = count + 1 print(count)
copy

This loop goes through each item in groceryItems. Each time the loop runs, count increases by 1. When the loop finishes, count stores the total number of items in the grocery list.

Calculating the total cost of groceries:

1234567
prices = [2.50, 3.00, 4.75, 1.80] totalCost = 0 for price in prices: totalCost = totalCost + price print("Total cost of Groceries:", totalCost)
copy

Here, the list contains grocery prices instead of item names. The loop goes through each price and adds it to totalCost. After the loop ends, totalCost is the sum of all grocery prices.

Task

Swipe to start coding

Sum Product Prices

Calculate the total cost of products using a for loop to iterate through a list of prices.

  1. Create a variable total and set it to 0.
  2. Use a for loop to iterate through each price in the prices list.
  3. Add each price to the total variable inside the loop.
  4. Print the final total after the loop completes.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookFor Loops

Swipe to show menu

In this chapter, we'll dive into how loops serve as a key tool for automating repetitive tasks and are essential for efficiently handling lists and other iterable data types.

Join Alex as he demonstrates the use of for loops to simplify operations in our grocery store:

The usage of for loops in Python allows you to execute a block of code repeatedly for each item in a collection (iterable object). Here's what you need to know about for loops:

Syntax


for variable in iterable:
    # Code to execute for each item

Use a for loop when you want to go through items one by one in a sequence like a list.

You start with the for keyword, then choose a variable name such as item. This variable temporarily stores each element from the sequence. After that, write the in keyword and the iterable object, for example groceryItems.

The code inside the loop runs once for every element in the sequence. On each iteration, item takes the next value from groceryItems, and the print() function outputs it. This continues until all items in the list have been processed.

1234
groceryItems = ["milk", "eggs", "cheese", "butter"] for item in groceryItems: # Code to be executed print(item)
copy

Iteration refers to the process of executing a block of code repeatedly. Python supports two main types of iteration:

Definite Iteration

Where the number of iterations is known in advance. for loops are a classic example, automatically stopping when they reach the end of the sequence.

Indefinite Iteration

In which execution continues until a specific condition is met, typically handled by while loops, which will be explored in detail in the upcoming chapter.

Example Applications

Counting how many items are in the grocery list:

1234567
groceryItems = ["milk", "eggs", "cheese", "bread"] count = 0 for item in groceryItems: count = count + 1 print(count)
copy

This loop goes through each item in groceryItems. Each time the loop runs, count increases by 1. When the loop finishes, count stores the total number of items in the grocery list.

Calculating the total cost of groceries:

1234567
prices = [2.50, 3.00, 4.75, 1.80] totalCost = 0 for price in prices: totalCost = totalCost + price print("Total cost of Groceries:", totalCost)
copy

Here, the list contains grocery prices instead of item names. The loop goes through each price and adds it to totalCost. After the loop ends, totalCost is the sum of all grocery prices.

Task

Swipe to start coding

Sum Product Prices

Calculate the total cost of products using a for loop to iterate through a list of prices.

  1. Create a variable total and set it to 0.
  2. Use a for loop to iterate through each price in the prices list.
  3. Add each price to the total variable inside the loop.
  4. Print the final total after the loop completes.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 1
single

single

some-alt