Getting Started

Control Flow

Control flow statements like conditionals and loops determine the order in which code executes, enabling dynamic decision-making and repetition.

Conditional Statements

Conditional statements let your program decide which actions to take based on specific conditions. They are fundamental for making decisions in code.

If Statements

Use an if statement to run a block of code only when a condition is true. It's the simplest form of branching logic.


If...Else

When the condition is false, the else block runs instead. This gives your program a fallback action.


If...Elif...Else

Use elif (short for "else if") when you need to check multiple conditions in sequence. Only the first condition that evaluates to true will run its block.


Ternary Operator

This is a more concise way to write a simple if...else in a single line. It’s useful for quick assignments or expressions based on a condition.

Match Statements

Introduced in Python 3.10, match is similar to switch statements in other languages. It lets you compare a value against several patterns.

  • Each case handles a specific pattern.

  • The special pattern case _ acts like a default case.

  • You don’t need a break statement—only the first match runs.

Loops in Python

Loops help you repeat blocks of code, either for a specific number of times or while a condition remains true.


For Loops

Use a for loop to iterate over items in a sequence, like a list, tuple, or range. This is great when you know in advance how many times to repeat.


While Loops

A while loop runs as long as a certain condition stays true. It’s useful for more dynamic or open-ended repetition.

count = 1

while count <= 5:
    print("Count is:", count)
    count += 1  # This increases the value of count by 1 each time


Loop Control Statements

These statements give you more control over how loops behave.

  • Break: Stops the loop entirely and exits.

  • Continue: Skips the current iteration and moves to the next one.

number = 0

while number < 10:
    number += 1

    if number == 3:
        continue  # Skip the rest of the loop when number is 3

    if number == 8:
        break  # Exit the loop when number reaches 8

    print("Number is:", number)

Conclusion

Conditional statements and loops form the backbone of control flow in Python. They allow your program to make decisions and repeat actions. Up next, we’ll dive into functions—one of Python’s most powerful tools for organizing and reusing code.

Conditional Statements

Conditional statements let your program decide which actions to take based on specific conditions. They are fundamental for making decisions in code.

If Statements

Use an if statement to run a block of code only when a condition is true. It's the simplest form of branching logic.


If...Else

When the condition is false, the else block runs instead. This gives your program a fallback action.


If...Elif...Else

Use elif (short for "else if") when you need to check multiple conditions in sequence. Only the first condition that evaluates to true will run its block.


Ternary Operator

This is a more concise way to write a simple if...else in a single line. It’s useful for quick assignments or expressions based on a condition.

Match Statements

Introduced in Python 3.10, match is similar to switch statements in other languages. It lets you compare a value against several patterns.

  • Each case handles a specific pattern.

  • The special pattern case _ acts like a default case.

  • You don’t need a break statement—only the first match runs.

Loops in Python

Loops help you repeat blocks of code, either for a specific number of times or while a condition remains true.


For Loops

Use a for loop to iterate over items in a sequence, like a list, tuple, or range. This is great when you know in advance how many times to repeat.


While Loops

A while loop runs as long as a certain condition stays true. It’s useful for more dynamic or open-ended repetition.

count = 1

while count <= 5:
    print("Count is:", count)
    count += 1  # This increases the value of count by 1 each time


Loop Control Statements

These statements give you more control over how loops behave.

  • Break: Stops the loop entirely and exits.

  • Continue: Skips the current iteration and moves to the next one.

number = 0

while number < 10:
    number += 1

    if number == 3:
        continue  # Skip the rest of the loop when number is 3

    if number == 8:
        break  # Exit the loop when number reaches 8

    print("Number is:", number)

Conclusion

Conditional statements and loops form the backbone of control flow in Python. They allow your program to make decisions and repeat actions. Up next, we’ll dive into functions—one of Python’s most powerful tools for organizing and reusing code.

Conditional Statements

Conditional statements let your program decide which actions to take based on specific conditions. They are fundamental for making decisions in code.

If Statements

Use an if statement to run a block of code only when a condition is true. It's the simplest form of branching logic.


If...Else

When the condition is false, the else block runs instead. This gives your program a fallback action.


If...Elif...Else

Use elif (short for "else if") when you need to check multiple conditions in sequence. Only the first condition that evaluates to true will run its block.


Ternary Operator

This is a more concise way to write a simple if...else in a single line. It’s useful for quick assignments or expressions based on a condition.

Match Statements

Introduced in Python 3.10, match is similar to switch statements in other languages. It lets you compare a value against several patterns.

  • Each case handles a specific pattern.

  • The special pattern case _ acts like a default case.

  • You don’t need a break statement—only the first match runs.

Loops in Python

Loops help you repeat blocks of code, either for a specific number of times or while a condition remains true.


For Loops

Use a for loop to iterate over items in a sequence, like a list, tuple, or range. This is great when you know in advance how many times to repeat.


While Loops

A while loop runs as long as a certain condition stays true. It’s useful for more dynamic or open-ended repetition.

count = 1

while count <= 5:
    print("Count is:", count)
    count += 1  # This increases the value of count by 1 each time


Loop Control Statements

These statements give you more control over how loops behave.

  • Break: Stops the loop entirely and exits.

  • Continue: Skips the current iteration and moves to the next one.

number = 0

while number < 10:
    number += 1

    if number == 3:
        continue  # Skip the rest of the loop when number is 3

    if number == 8:
        break  # Exit the loop when number reaches 8

    print("Number is:", number)

Conclusion

Conditional statements and loops form the backbone of control flow in Python. They allow your program to make decisions and repeat actions. Up next, we’ll dive into functions—one of Python’s most powerful tools for organizing and reusing code.

© Layer Docs

Made by Alejandro · Powered by Framer

Create a free website with Framer, the website builder loved by startups, designers and agencies.