Skip to content

Beginner's Tutorial: for Loops in Python

Overview

In this tutorial, we will learn how to use for loops in Python. A for loop is a control structure used to iterate over a sequence (such as a list, tuple, string, etc.). By the end of this tutorial, you will have a solid understanding of how to use for loops and some related concepts.

Table of Contents

  1. What is a for Loop?
  2. Basic Syntax
  3. Example Code
  4. Supporting Concepts
  5. Exercises
  6. Summary

What is a for Loop?

A for loop is an iteration structure used to traverse each element in a sequence. It automatically handles the length of the sequence and updates the loop variable during each iteration. for loops are ideal for tasks that require repetitive execution.

Basic Syntax

The basic syntax of a for loop in Python is as follows:

python
for variable in sequence:
    loop body
  • variable: During each iteration, an element from the sequence is assigned to this variable.
  • sequence: This can be a list, tuple, string, dictionary, or any iterable object.
  • loop body: The block of code that is executed during each iteration.

Example Code

Example 1: Iterating Over a List

python
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
    print(fruit)

Output:

python
Apple
Banana
Orange

Example 2: Iterating Over a String

python
message = "Hello, World!"
for char in message:
    print(char)

Output:

python
H
e
l
l
o
,
 
W
o
r
l
d
!

Example 3: Using the range() Function

The range() function generates a sequence of numbers, commonly used in for loops.

python
for i in range(5):
    print(i)

Output:

python
0
1
2
3
4

Supporting Concepts

1. Lists

A list is a data structure in Python used to store multiple elements. The elements in a list can be of any data type and can be mixed.

python
numbers = [1, 2, 3, 4, 5]

2. Strings

A string is a sequence of characters, represented by single or double quotes.

python
greeting = "Hello, World!"

3. The range() Function

The range() function generates a sequence of numbers, often used in for loops. It can take one, two, or three arguments:

  • range(stop): Generates numbers from 0 to stop-1.
  • range(start, stop): Generates numbers from start to stop-1.
  • range(start, stop, step): Generates numbers from start to stop-1 with a step of step.
python
for i in range(1, 10, 2):
    print(i)

Output:

1
3
5
7
9

Exercises

Exercise 1: Summing Elements in a List

Write a for loop to calculate the sum of all elements in the list numbers = [10, 20, 30, 40, 50].

python
numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
    total += number
print("The total sum is:", total)

Exercise 2: Counting Characters in a String

Write a for loop to count the occurrences of the character 'l' in the string message = "Hello, World!".

python
message = "Hello, World!"
count = 0
for char in message:
    if char == 'l':
        count += 1
print("The character 'l' appears", count, "times")

Summary

By completing this tutorial, you have learned how to use for loops in Python to iterate over sequences. for loops are a powerful tool in programming that can help automate repetitive tasks. We hope you found this tutorial helpful and that you will continue to practice and explore more advanced concepts.

If you have any questions or need further assistance, feel free to ask!

This tutorial should provide a comprehensive introduction to for loops in Python, along with relevant examples and exercises to reinforce the concepts.