Introduction to Python

Control Flow Structure

Lesson 6: Conditional Statements

What is Control Flow?

By default, Python executes code line by line from top to bottom. Control flow structures allow you to change this order — making your program skip certain lines, repeat others, or choose between different paths based on conditions.

A road with a fork showing two paths — one labeled True leading to one block of code and one labeled False leading to another block — representing how control flow works

Control Flow Structures in Python

StructureKeyword(s)Purpose
Conditionalif, elif, elseExecute code only when a condition is met
Loop (count)forRepeat code a fixed number of times
Loop (condition)whileRepeat code while a condition is true
BreakbreakExit a loop early
ContinuecontinueSkip the current iteration and move to the next

The if Statement

# Basic if statement score = 75 if score >= 50: print("You passed!")

The if-elif-else Statement

temperature = 35 if temperature > 40: print("It is extremely hot!") elif temperature > 30: print("It is hot.") elif temperature > 20: print("It is warm.") else: print("It is cool.")

Flowchart showing the if-elif-else decision structure with temperature checks — four diamonds for each condition with arrows leading to the correct print statement
Python uses indentation (4 spaces) to define code blocks inside control flow structures. Every line inside an if, elif, or else block must be indented consistently.

Comparison Operators

Python Comparison Operators

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than7 > 4True
<Less than3 < 6True
>=Greater than or equal to5 >= 5True
<=Less than or equal to4 <= 7True

Subscribe to our newsletter.

Get updates to news and events.