Table of Contents
Beginner’s Guide to Python Flow Control
Welcome to your practical guide to Python flow control! In this tutorial, you’ll learn how to control the order in which your Python code runs, make decisions, repeat actions, and handle common programming tasks. We’ll cover if/else statements, for loops, while loops, break/continue, pass, and logical operators—all with clear examples and tips for beginners.
Learning Goals
- Understand and use Python’s conditional statements (if, elif, else)
- Write and control loops (for, while) for repetitive tasks
- Apply break, continue, and pass statements to manage loop flow
- Combine conditions with logical operators (and, or, not)
- Recognize and avoid common mistakes in flow control
1. Conditional Statements: if, elif, else
Conditional statements let your program make decisions. You can run code only if certain conditions are true.
Why It Matters
Almost every program needs to make choices—like checking user input, validating data, or responding to events. Conditional statements are the foundation of decision-making in Python.
Syntax Breakdown
| Statement | Syntax | Purpose |
|---|---|---|
| if | if condition: | Runs code if condition is True |
| elif | elif condition: | Checks another condition if previous was False |
| else | else: | Runs code if all previous conditions are False |
Example: Basic if Statement
number = int(input('Enter a number: '))
if number > 0:
print(f'{number} is a positive number.')
print('This statement always runs.')
Explanation: If the user enters a positive number, the message is printed. If not, only the last statement runs.
Example: if…else Statement
number = int(input('Enter a number: '))
if number > 0:
print('Positive number')
else:
print('Not a positive number')
print('This statement always runs.')
Explanation: The else block runs if the condition is False.
Example: if…elif…else Statement
number = -5
if number > 0:
print('Positive number')
elif number < 0:
print('Negative number')
else:
print('Zero')
Explanation: Only one block runs, depending on the value of number.
Example: Nested if Statements
number = 5
if number >= 0:
if number == 0:
print('Number is 0')
else:
print('Number is positive')
else:
print('Number is negative')
Explanation: You can put an if inside another if for more complex decisions.
Shorthand and Ternary If
grade = 40
result = 'pass' if grade >= 50 else 'fail'
print(result)
Explanation: This assigns 'pass' or 'fail' based on the condition. Use shorthand for simple cases.
2. Logical Operators: and, or, not
Logical operators let you combine multiple conditions in your if statements.
| Operator | Meaning | Example |
|---|---|---|
| and | True if both conditions are True | if age >= 18 and is_verified: |
| or | True if at least one condition is True | if age < 18 or is_student: |
| not | True if condition is False | if not is_verified: |
Example: Combining Conditions
age = 35
salary = 6000
if age >= 30 and salary >= 5000:
print('Eligible for premium membership.')
else:
print('Not eligible')
Tip:
if (a > b and c > a) or is_admin:3. Loops: for and while
Loops let you repeat actions, such as processing items in a list or asking for user input until a condition is met.
For Loop
The for loop is used to iterate over sequences (lists, strings, ranges).
| Loop Type | Syntax | When to Use |
|---|---|---|
| for | for item in sequence: | When you know how many times to loop |
| while | while condition: | When you don't know how many times to loop |
Example: Looping Through a List
languages = ['Swift', 'Python', 'Go']
for lang in languages:
print(lang)
Explanation: Prints each language in the list.
Example: Looping Through a String
language = 'Python'
for char in language:
print(char)
Explanation: Prints each character in the string.
Example: Using range()
for i in range(0, 4):
print(i)
Explanation: Prints numbers 0 to 3.
While Loop
The while loop repeats as long as a condition is True.
number = 1
while number <= 3:
print(number)
number = number + 1
Explanation: Prints numbers 1 to 3.
Example: User Input with while Loop
number = int(input('Enter a number: '))
while number != 0:
print(f'You entered {number}.')
number = int(input('Enter a number: '))
print('The end.')
Explanation: Keeps asking for input until the user enters 0.
4. break, continue, and pass Statements
These statements help you control the flow inside loops and conditional blocks.
| Statement | Purpose | Example |
|---|---|---|
| break | Exit the loop immediately | if x == 5: break |
| continue | Skip the rest of the loop for this iteration | if x % 2 == 0: continue |
| pass | Do nothing (placeholder) | if x > 10: pass |
Example: break Statement
for i in range(5):
if i == 3:
break
print(i)
Explanation: Stops the loop when i is 3.
Example: continue Statement
for i in range(5):
if i == 3:
continue
print(i)
Explanation: Skips printing 3.
Example: pass Statement
score = 85
if score > 90:
pass # Placeholder for future code
print('Score processed')
Explanation: pass lets you write empty blocks without errors.
5. Practical Tips and Common Mistakes
- Always use consistent indentation (usually 4 spaces) for blocks.
- Update variables inside while loops to avoid infinite loops.
- Use
breakto exit loops early,continueto skip iterations, andpassas a placeholder. - Use logical operators to combine conditions, but add parentheses for clarity.
- Shorthand if/else is great for simple assignments, but use full blocks for complex logic.
| Common Mistake | Correct Usage |
|---|---|
| Missing indentation after if/else | Indent all statements inside the block |
| Infinite while loop (no variable update) | Update the variable inside the loop |
| Using a comment instead of pass in empty blocks | Use pass to avoid syntax errors |
Summary & Next Steps
You've learned how to control the flow of your Python programs using conditional statements, loops, and special statements like break, continue, and pass. These tools are essential for writing interactive, efficient, and error-free code.
- Practice writing if/else, for, and while loops with your own examples.
- Try combining conditions with logical operators.
- Experiment with break, continue, and pass in your loops.
- Explore more advanced topics like functions, data structures, and error handling.
Keep practicing and building projects—your skillplayground journey starts here!
Tutorial created by skillplayground. All code examples are for educational purposes.
