runtric-logo
Python Basic 1 - Getting StartedLogical Operations & Control Flow in Python (4/4)
Chapter 4
Python Basic 1 - Getting Started

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.

Example Code

Interactive Code Editor
python

This 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 == 5True
  • != (not equal): 3 != 1True
  • > (greater than): 7 > 9False
  • >= (greater or equal): 7 >= 7True
  • < (less than): 2 < 5True
  • <= (less or equal): 2 <= 5True

Try it:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Logical Operators – Combine Multiple Tests

  • and → both conditions must be True
  • or → at least one condition is True
  • not → flips True/False

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Quick Summary

  • Comparisons turn numbers or text into Booleans

  • and, or, not mix or flip those Booleans

  • The result guides Python’s control-flow choices

if / elif / else – Python’s Decision Fork

Pattern:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Python runs the first condition that is True, then skips the rest.

Example Code

Interactive Code Editor
python

This 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: raises IndentationError

Nested Conditions – Decisions Inside Decisions

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Keep nesting shallow—too many layers become hard to read.

Hands-On Challenge – Movie Ticket Checker

Practice Challenge

Interactive Code Editor
📝 Your Task:

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.

python

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 runs

  • When 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 minutes

Control-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?

Lesson Complete!
100%
Progress
What You've Learned:
  • 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

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.