Logical Operations & Control Flow in Python
7 minutes read
Beginner friendly
Includes practice
Programs often need to choose between different actions—just like you check the weather before grabbing a jacket.
With logical operations and control-flow statements, we give Python that same decision-making power.
Boolean Values – The Simplest Answers
Python stores “yes / no” results in the special type bool.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
There are only two Boolean objects: True and False.
Comparison Operators – Ask a Question, Get True/False
==(is equal):5 == 5→True!=(not equal):3 != 1→True>(greater than):7 > 9→False>=(greater or equal):7 >= 7→True<(less than):2 < 5→True<=(less or equal):2 <= 5→True
Try it:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Logical Operators – Combine Multiple Tests
and→ both conditions must beTrueor→ at least one condition isTruenot→ flipsTrue/False
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Summary
Comparisons turn numbers or text into Booleans
and,or,notmix or flip those BooleansThe result guides Python’s control-flow choices
if / elif / else – Python’s Decision Fork
Pattern:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Python runs the first condition that is True, then skips the rest.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Indentation RULES! 🚧
- Every block must be indented the same number of spaces (4 is standard)
- Mixing tabs & spaces causes errors—stick to spaces
- Forgetting the indent after
if:raisesIndentationError
Nested Conditions – Decisions Inside Decisions
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Keep nesting shallow—too many layers become hard to read.
Hands-On Challenge – Movie Ticket Checker
Practice Challenge
Write a program that checks the value of age. If age is less than 13, set answer to 'Child price'. If it is between 13 and 17 (inclusive), set it to 'Teen price'. Otherwise, set it to 'Adult price' . Finally, print the result.
Use if, elif, and else to handle the three age groups.
Runtic's Learning Tips
Trace code with prints: add
print()inside each branch to see which path runsWhen stuck, explain the logic out loud—teaching yourself reveals gaps
Build tiny “decision apps” (weather advice, quiz grader) to cement control-flow thinking
Quick Practice Quiz
Quick Practice
4 minutesControl-Flow Check-Up
What does `(3 > 2) and (2 == 5)` evaluate to? (This 'and' is logical AND)
Which keyword adds an extra condition after an 'if' block?
- Turned comparisons into Booleans and combined them with and, or, not
- Mastered if, elif, else, indentation, and simple nesting for decision making
- Practiced retrieval with quizzes and edited live code to cement your new skills
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.