Skip to main content
All tracks
Python Basics

Conditional Statements

Use if, elif and else to run code depending on a condition. Python uses indentation to define blocks.

marks = 72

if marks >= 90:
grade = "A"
elif marks >= 60:
grade = "B"
else:
grade = "C"

print(grade) # B

Conditional expression​

status = "adult" if age >= 18 else "minor"

Truthy and falsy values​

0, "", [], {}, None and False are falsy. Everything else is truthy.

items = []
if not items:
print("empty")

match statement (Python 3.10+)​

command = "stop"
match command:
case "go":
print("Going")
case "stop":
print("Stopping")
case _:
print("Unknown")
Finished reading?

Mark it complete to track your progress across the docs.

Questions and discussion

Stuck on something here, or found a clearer way to explain it? Start a thread.

All discussions
  • Ask a question or share what helped
  • React to useful answers
  • Sign in with GitHub to post

Comments are stored in GitHub Discussions and shown here by giscus. PrepVerse never sees your GitHub password.