📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Syntax and Basic Usage
  • The Range Function and Numeric Iteration
  • Advantages of Python's For Loop
  • Disadvantages of Python's For Loop
  • Conclusion
Edit on GitHub
  1. 5 - Iteration

The for statement

The syntax of a for statement is similar to a function definition. It has a header that ends with a colon and an indented body. The body can contain any number of statements. A for statement is sometimes called a loop because the flow of execution runs through the body and then loops back to the top. In this case, it runs the body of the loop a thousand times.

Syntax and Basic Usage

In Python, the for loop is used to iterate over a sequence, which can be a string, list, tuple, or any other iterable object. The basic syntax of a for loop is as follows:

for item in sequence:
    # Code block to be executed for each item

Here, item represents the variable that takes on each element of the sequence in each iteration. The code block indented under the loop is executed for each item in the sequence.

Let's consider a simple example to illustrate this concept:

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

Output:

apple
banana
cherry

In this example, the for loop iterates over the list of fruits and prints each one.

The Range Function and Numeric Iteration

In addition to iterating over sequences, Python's for loop is often employed for numeric iteration using the range() function. The range() function generates a sequence of numbers within a specified range. Its syntax is as follows:

range(start, stop, step)
  • start: The starting value of the range (inclusive).

  • stop: The ending value of the range (exclusive).

  • step: The increment between values (default is 1).

Here's an example of using range() with a for loop:

for i in range(1, 5):
    print(i)

Output:

1
2
3
4

Advantages of Python's For Loop

  1. Readability: Python's for loop syntax is concise and easy to read, making it an ideal choice for beginners and experienced programmers alike. This readability contributes to code maintainability.

  2. Versatility: The for loop can iterate over various data types, including lists, tuples, dictionaries, and strings, making it highly versatile for different programming tasks.

Disadvantages of Python's For Loop

  1. Performance Overhead: In certain scenarios, especially when dealing with large datasets, using a for loop can introduce performance overhead.

  2. Memory Consumption: When iterating over large sequences, Python's for loop may consume significant memory, as it loads the entire sequence into memory. This can be a concern in memory-constrained environments.

Conclusion

Python's for loop is a versatile and powerful tool for iterating over sequences and performing repetitive tasks. Its readability and flexibility make it an excellent choice for a wide range of programming tasks.

PreviousThe while statementNextbreak and continue statements

Last updated 1 year ago