You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

5.2 For loop

2 min readjune 24, 2024

loops are powerful tools Python, allowing you to iterate through sequences like lists, strings, and ranges. They simplify repetitive tasks by executing a block of code for each item in a , making your programs more efficient and readable.

The function works hand-in-hand with for loops, generating number sequences for . For loops are versatile, working with various data types and performing tasks like calculations, finding values, and modifying elements based on conditions.

Iteration with for loops

Implement a for loop to iterate through a sequence of elements

Top images from around the web for Implement a for loop to iterate through a sequence of elements
Top images from around the web for Implement a for loop to iterate through a sequence of elements
  • For loops enable iterating over a sequence of elements (lists, tuples, strings, or other objects)
  • syntax:
    for variable in sequence:
    • variable
      (also known as the ) takes on each value in the
      sequence
      one at a time
    • Code block under the for loop () executes for each value of
      variable
  • Iterating over a example:
    fruits = ['apple', 'banana', 'cherry']
    for fruit in fruits:
        print(fruit)
    
    Output:
    apple
    banana
    cherry
    

Range function for sequences

  • range()
    function generates a sequence of numbers
    • Syntax:
      range(start, stop, step)
      • start
        : Starting number of the sequence (inclusive, default 0)
      • stop
        : Ending number of the sequence (exclusive)
      • step
        : Increment between each number (default 1)
  • range()
    examples:
    • range(5)
      : Generates numbers 0 to 4 (0, 1, 2, 3, 4)
    • range(1, 6)
      : Generates numbers 1 to 5 (1, 2, 3, 4, 5)
    • range(1, 10, 2)
      : Generates odd numbers 1 to 9 (1, 3, 5, 7, 9)
  • Using
    range()
    with for loops:
    for i in range(5):
        print(i)
    
    Output:
    0
    1
    2
    3
    4
    

For loops across data types

  • For loops work with various data types:
    • Lists:
      numbers = [1, 2, 3, 4, 5]
      for num in numbers:
          print(num ** 2)
      
      Output: 1, 4, 9, 16, 25
    • Strings:
      name = "John"
      for char in name:
          print(char)
      
      Output: J, o, h, n
    • Dictionaries (iterating over keys):
      person = {"name": "Alice", "age": 25, "city": "New York"}
      for key in person:
          print(key)
      
      Output: name, age, city
  • For loops perform repetitive tasks:
    1. Calculating sum or product of a list of numbers
    2. Finding maximum or minimum value in a list
    3. Counting occurrences of specific elements in a list
    4. Modifying elements in a list based on conditions

Loop Control Flow and Structure

  • determines how the program executes the loop
  • The is the object that produces the next item in the sequence
  • Proper is crucial for defining the loop body and maintaining correct execution
  • The sequence is the iterable object being looped over (e.g., list, string, or range)
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary