Python Control Flow and Loops

Learning PathSkills: Python, Control Flow, Conditional Statements, Booleans, for Loops, while Loops, enumerate, Nested Loops, break, continue, pass, Membership Testing

Python "while" Loops (Indefinite Iteration)

In this learning path, you’ll learn about Python’s control flow tools. Starting with conditional statements and Boolean operators, you’ll move on to for and while loops, enumerate(), nested loops, and loop control keywords like break, continue, and pass.

Python Control Flow and Loops

Learning Path ⋅ 15 Resources

Conditional Statements and Boolean Logic

Every decision a Python program makes comes down to a condition that is either true or false. You’ll write if, elif, and else statements to branch on those conditions, then look at Boolean values and the and, or, and not operators that build them. Start here, because the loops in the rest of the path decide when to keep running using exactly the same conditions.

Title image for Conditional Statements in Python (if/elif/else) (Conditional Statements in Python)

Course

Conditional Statements in Python (if/elif/else)

Learn how to work with conditional ("if") statements in Python. Master if-statements step-by-step and see how to write complex decision making code in your programs.

Title image for Python Booleans: Leveraging the Values of Truth (Python Booleans: Optimize Your Code With Truth Values)

Course

Python Booleans: Leveraging the Values of Truth

Learn about the built-in Python Boolean data type, which is used to represent the truth value of an expression. You'll see how to use Booleans to compare values, check for identity and membership, and control the flow of your programs with conditionals.

Title image for Using the Python and Operator (Using the "and" Boolean Operator in Python)

Course

Using the Python and Operator

Learn how Python's "and" operator works and how to use it in your code. You'll get to know its special features and see what kind of programming problems you can solve by using "and" in Python.

Title image for Using the Python or Operator (How to Use the Python or Operator)

Course

Using the Python or Operator

Learn about how the Python or operator works and how to use it. You'll get to know its special features and see what kind of programming problems you can solve by using or in Python.

Title image for Using the Python not Operator (Using the "not" Boolean Operator in Python)

Course

Using the Python not Operator

Learn how Python's "not" operator works and how to use it in your code. You'll get to know its features and see what kind of programming problems you can solve by using "not" in Python.

for Loops

You can now make a decision once. Next you’ll make Python repeat work: iterating over a sequence with for loops, keeping a running count with enumerate(), and nesting one loop inside another to walk rows and columns. A for loop runs once per item, so you always know how many times it runs, which makes it the simplest loop to start with.

Title image for For Loops in Python (Definite Iteration) (Python "for" Loops (Definite Iteration))

Course

For Loops in Python (Definite Iteration)

Learn all about how to perform definite iteration with Python "for" loops. You’ll see how other programming languages implement definite iteration, learn about iterables and iterators, and tie it all together to learn about Python’s for loop.

Title image for Looping With Python enumerate() (Python enumerate(): Simplify Looping With Counters)

Course

Looping With Python enumerate()

Once you learn about for loops in Python, you know that using an index to access items in a sequence isn't very Pythonic. So what do you do when you need that index value? In this course, you'll learn all about Python's built-in enumerate(), where it's used, and how you can emulate its behavior.

Title image for Python's enumerate() (Python enumerate(): Simplify Looping With Counters)

Interactive Quiz

Python's enumerate()

Title image for Nested Loops in Python (Nested Loops in Python)

Tutorial

Nested Loops in Python

Learn how to use nested loops in Python to iterate over multiple sequences and perform repeated actions efficiently in your programs.

Title image for Nested Loops in Python (Nested Loops in Python)

Interactive Quiz

Nested Loops in Python

while Loops

A for loop runs once per item in a sequence. A while loop keeps going until its condition turns false, which is what you need when you don’t know the number of repetitions in advance. You’ll write while loops for that kind of repetition and see how to emulate the do-while loop that Python doesn’t have.

Title image for Mastering While Loops (Python "while" Loops (Indefinite Iteration))

Course

Mastering While Loops

Master indefinite iteration using the Python "while" loop. You’ll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.

Title image for How Can You Emulate Do-While Loops in Python? (How Can You Emulate Do-While Loops in Python?)

Tutorial

How Can You Emulate Do-While Loops in Python?

Learn how to emulate do-while loops in Python. The most common technique to do this is to create an infinite while loop with a conditional statement that controls the loop and jumps out of it using a break statement.

Loop Control and Membership

So far your loops have run start to finish. This section is about interrupting them: break to leave a loop early, continue to skip the rest of one iteration, and the in and not in operators to test for membership without writing a loop at all. You’ll finish with the pass statement, Python’s way of writing a block that does nothing.

Title image for Break Out of Loops With Python's break Keyword (How to Exit Loops Early With the Python Break Keyword)

Course

Break Out of Loops With Python's break Keyword

Learn how Python’s break lets you exit for and while loops early, with practical demos from simple games to everyday data tasks.

Title image for Skip Ahead in Loops With Python's continue Keyword (Skip Ahead in Loops With Python's Continue Keyword)

Tutorial

Skip Ahead in Loops With Python's continue Keyword

Learn how Python's continue statement works, when to use it, common mistakes to avoid, and what happens under the hood in CPython byte code.

Title image for Checking for Membership Using Python's "in" and "not in" Operators (Python's "in" and "not in" Operators: Check for Membership)

Course

Checking for Membership Using Python's "in" and "not in" Operators

Learn how to check if a given value is present or absent in a collection of values using Python's in and not in operators. This type of check is known as a membership test in Python.

Title image for The pass Statement: How to Do Nothing in Python (The pass Statement: How to Do Nothing in Python)

Tutorial

The pass Statement: How to Do Nothing in Python

Learn about the Python pass statement, which tells the interpreter to do nothing. Even though pass has no effect on program execution, it can be useful. You'll see several use cases for pass as well as some alternative ways to do nothing in your code.

Test Your Knowledge

You’ve made it through the entire path! In the wrap-up quiz below, you’ll revisit the most important ideas about conditional statements, Boolean operators, for and while loops, enumerate(), nested loops, and the break and continue keywords. Use it to spot any topics that still feel rusty before moving on.

Title image for Python Control Flow and Loops (Python "while" Loops (Indefinite Iteration))

Interactive Quiz

Python Control Flow and Loops

Test your understanding of Python control flow: conditional statements, Boolean operators, for and while loops, break, continue, and nested loops.

Congratulations on completing this learning path! You’ve learned how to steer a Python program with conditional statements and Boolean operators, repeat work with for and while loops, and control that repetition using break, continue, pass, and membership tests with in and not in.

Continue your Python basics journey with the next learning path:

Learning Path

Python Data Structures

24 Resources ⋅ Skills: Python, Strings, Lists, Tuples, Dictionaries, Sets, List Comprehensions, range(), Bytes, Sorting, Copying

You might also be interested in these related learning paths:

Got feedback on this learning path?

Looking for real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

« Browse All Learning Paths