kimberly_young
kimberly_young Feb 13, 2026 β€’ 0 views

Sample Code for a 'For' Loop in Python (Grade 5 Level)

Hey there! πŸ‘‹ Learning about 'for' loops can seem tricky at first, but trust me, it's super useful! Imagine you have a list of your favorite fruits, and you want to print each one out. A 'for' loop helps you do that without writing a ton of code. Let's explore how it works with some fun examples! 🍎
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
lori_burnett Dec 28, 2025

πŸ“š What is a 'For' Loop?

A 'for' loop in Python is like a helpful robot that repeats a set of instructions for each item in a list or a range. Think of it as telling the robot, "For each thing in this box, do this!" It's a super handy way to avoid writing the same code over and over again. It makes your code shorter and easier to read.

πŸ“œ A Little History

The idea of looping has been around since the early days of computers! It's a fundamental concept. 'For' loops, specifically, became popular as programming languages evolved to make working with lists and collections easier.

πŸ”‘ Key Principles of 'For' Loops

  • πŸ”‘ Iteration: The process of going through each item in a sequence.
  • πŸ”’ Sequence: A list, string, or range of numbers that the loop goes through.
  • πŸ€– Loop Body: The code that is executed for each item in the sequence.

🍎 Example 1: Printing Fruits

Let's print out a list of fruits:


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

This code will print:


apple
banana
cherry
  • 🍎 fruits: This is our list of fruits.
  • πŸ”„ for fruit in fruits:: This line starts the loop. It says, "For each fruit in the fruits list..."
  • πŸ–¨οΈ print(fruit): This line prints the current fruit in the loop.

πŸ”’ Example 2: Counting to Five

Let's count from 1 to 5 using a 'for' loop and the range() function:


for number in range(1, 6):
    print(number)

This code will print:


1
2
3
4
5
  • πŸ”’ range(1, 6): This creates a sequence of numbers from 1 up to (but not including) 6.
  • πŸ” for number in range(1, 6):: This starts the loop, assigning each number in the range to the variable 'number'.
  • πŸ–¨οΈ print(number): This line prints the current number.

βž• Example 3: Summing Numbers

Let's add up all the numbers in a list:


numbers = [1, 2, 3, 4, 5]
total = 0
for number in numbers:
    total = total + number
print(total)

This code will print:


15
  • βž• total = 0: We start with a total of 0.
  • βž• total = total + number: This adds the current number to the total in each iteration.

🌍 Real-World Applications

  • 🌍 Games: Making characters move or repeat actions.
  • πŸ“Š Data Analysis: Processing lists of numbers to find averages or patterns.
  • 🌐 Websites: Displaying lists of products or articles.

πŸ’‘ Conclusion

The 'for' loop is a super useful tool in Python that lets you repeat tasks easily. With a little practice, you'll be using them all the time! Keep exploring, and you'll discover even more cool things you can do with loops. πŸ˜„

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€