Skip to content
Guest
Learn Python · Course Syllabus Show lessons
Python Basics · 8 min read · Beginner

Conditionals & Loops

Branch logic cleanly with if/elif/else and iterate over sequences and numeric ranges using for loops.

What you'll learn

  • Write indented if, elif, and else blocks
  • Loop through lists and ranges with for and range()
  • Get both index and value using enumerate()

In Python, indentation (4 spaces) defines code blocks. That keeps every Python file clean and uniform.

if / elif / else

score = 88
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "Keep practicing"

Fill-in-the-Blank Exercise

Conditionals & Range Loop

Use elif for the second branch and range(1, 4) to loop over 1, 2, 3.

if score >= 90:
  print("A")
 score >= 80:
  print("B")

for n in (1, 4):
  print(n)

Try It Yourself

PythonLooping with enumerate() and Conditionals
Python Output (WebAssembly / Pyodide)

Click Run Python to execute real Python 3 right in your browser.

Check Your Understanding

Score: 0 / 1

1. Which numbers does range(1, 5) produce in Python?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind