Python If...Else

Python · Control Flow

Theory

An if statement runs a block of code only when its condition is truthy. Python evaluates the condition after if; if it is true, the indented block runs and the rest of the chain is skipped. Otherwise it checks each elif condition in order, and if none is true, the else block runs. At most one branch of an if/elif/else chain ever runs, and both elif and else are optional. With salary = 85000, salary > 80000 is True, so level becomes 'Senior'.

Because conditions are checked top to bottom and the first match wins, order matters: put the most specific condition first. A common mistake is a boundary like > versus >=: with salary = 80000, salary > 80000 is False, so the person is 'Junior'. Any value can be a condition thanks to truthiness, so if items: means 'if the list is not empty'. For simple either/or values, the conditional expression level = 'Senior' if salary > 80000 else 'Junior' fits on one line.

Why we use it

Programs must react differently to different data: grade a score, apply a discount, flag an outlier. if/elif/else is the basic tool that turns a condition into a decision.

When to use it

Use if/elif/else for decisions with a few branches. Use the one-line conditional expression for choosing between two values. For mapping many fixed values to results, a dictionary lookup or a match statement is often cleaner.

How it works

  1. Reach the if statement
  2. Is salary > 80000 true? Yes: Run the if block: level = 'Senior'. No: Check the next branch.
  3. Any elif condition true? Yes: Run the first matching elif block. No: Run the else block: level = 'Junior'.
  4. Skip all remaining branches
  5. Continue after the chain: print(level)

Syntax

if condition:
    ...
elif other:
    ...
else:
    ...

Example

if salary > 80000:
    level = "Senior"
else:
    level = "Junior"

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic