📖
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
Edit on GitHub

5 - Iteration

PreviousPEP 8 RecommendationsNextThe while statement

Last updated 1 year ago

The following code repeats the same task twice:

word = 'Lilian' 
print(word) 
print(word)

If we wanted to write a script that print the same thing a thousand times, this approach would be impractical. Thankfully we can do the same thing more concisely via iteration.

In programming, iteration refers to the process of repeatedly executing a block of code until a certain condition is met. Iteration is often used in loops, which allow you to iterate over a sequence of values or perform a certain operation a specific number of times.

There are two main types of loops in Python: for loops and while loops.

for loops are used to perform a certain operation a specific number of times or to iterate over a sequence of values, such as a list, tuple, or string. The for loop is also referred to as a count-controlled loops.

To print my name a thousand times can be done concisely with a for statement as shown below.

word = 'Lilian' 
for i in range(1000): 
    print(word) 

This is the simplest use of the for statement; we will see more later.

while loops, on the other hand, are used to iterate until a certain condition is met. They are sometimes referred to as condition-controlled loops.

In both cases, iteration allows us to execute a block of code multiple times, which can be useful for performing repetitive tasks or iterating over a sequence of values.