Python13 code examples
⚡ +100 XP

Control Flow (if/else, loops)

1

What is Control Flow?

Control flow determines the order in which statements are executed in a program. Without control flow, statements execute sequentially from top to bottom. Control flow statements allow you to make decisions (conditionals) and repeat actions (loops), making your programs dynamic and powerful.

💡

Control flow is what makes programming useful - it allows programs to respond to different inputs and situations.

2

If Statements - Making Decisions

The if statement is the most basic conditional statement. It executes a block of code only if a condition is True.

💡

Python uses indentation (4 spaces is standard) instead of curly braces to define code blocks. The colon (:) is required at the end of the if/elif/else line.

3

Comparison and Logical Operators

Conditions use comparison operators and can be combined with logical operators.

💡

Comparison operators return boolean values (True/False). Use parentheses to make complex conditions more readable.

4

Nested If Statements

You can put if statements inside other if statements for more complex decision making.

💡

While nested if statements are useful, too many levels can make code hard to read. Consider using elif when possible.

5

Ternary Operator - Conditional Expressions

The ternary operator provides a concise way to write conditional expressions in a single line. It's useful for simple assignments.

💡

Ternary operators are concise but should be used for simple conditions only. For complex logic, use traditional if-elif-else statements for better readability.

6

For Loops - Iterating Over Sequences

For loops are used to iterate over sequences like lists, strings, dictionaries, and ranges.

💡

range() creates a sequence of numbers. In Python, range(5) generates 0,1,2,3,4. Use enumerate() when you need both the index and value in a loop.

7

While Loops - Repeating Until a Condition is Met

While loops continue executing as long as a condition remains True. They're useful when you don't know in advance how many times you need to iterate.

💡

Be careful with while loops! Always ensure the condition will eventually become False to avoid infinite loops. Use break to exit early when needed.

8

Loop Control Statements

Python provides special statements to control loop execution: break, continue, and else with loops.

💡

The else clause with loops is unique to Python. It's useful for search algorithms where you want to know if a search succeeded or not.

9

Common Loop Patterns

Here are some common patterns and use cases for loops in programming.

💡

List comprehensions are a concise way to create lists based on existing sequences. They're more readable and faster than traditional loops for simple operations.

10

Match Case Statement (Python 3.10+)

The match-case statement is Python's version of switch/case statements found in other languages. It provides a cleaner way to handle multiple conditions based on patterns.

💡

Match-case was introduced in Python 3.10. While powerful, traditional if-elif-else statements are often more readable for simple conditions. Use match-case when dealing with multiple patterns or complex data structures.

11

Exception Handling with Control Flow

Control flow can be combined with exception handling to create robust programs that gracefully handle errors.

💡

Exception handling allows you to control program flow when errors occur. Use try/except blocks to gracefully handle errors and keep your program running.

12

Generator Functions and Yield

Generator functions use 'yield' instead of 'return' to produce a sequence of values lazily, which is memory-efficient for large datasets.

💡

Generators are memory-efficient because they generate values on-the-fly rather than storing them in memory. Use yield when working with large datasets or infinite sequences.

13

Conditional Logic in Loops - Advanced Patterns

Combining conditionals with loops enables powerful data processing patterns and algorithms.

💡

These patterns demonstrate how to use conditionals within loops to create intelligent data processing logic. Breaking out of nested loops requires careful flag management.

14

Real-World Example: User Menu System

Here's a complete example of using control flow to create an interactive menu system.

💡

This example combines multiple control flow concepts: while loop for the menu, if/elif for option selection, for loops for displaying contacts, and break/else for search operations.

Finished reading? Mark it complete to earn your XP.