Practice by topic / Python & code / Python

Python Loops & Conditions Exercises

Python is the most popular language for automating work: cleaning data, renaming files, crunching numbers. Each exercise runs your real code in the browser and checks it. These 5 exercises build up step by step, starting with loop through a list, pass or fail, grade bands.

3 of 5 free · no sign-up needed · instant feedback

1

Loop Through a List

Free

Print each fruit in <code>fruits</code> on its own line.

Show the solution

for fruit in fruits: print(fruit)

Right — a for loop runs its indented block once for every item.

Try it in the live editor →
2

Pass or Fail

Free

Create <code>result</code>: <code>"Pass"</code> if <code>score</code> is 50 or more, otherwise <code>"Fail"</code>.

Show the solution

if score >= 50: result = "Pass" else: result = "Fail"

Good — if/else is Python's IF(). Watch the boundary: >= 50 includes 50 itself.

Try it in the live editor →
3

Grade Bands

Free

Create <code>grade</code> from <code>score</code>: <code>"A"</code> for 80+, <code>"B"</code> for 65–79, <code>"C"</code> for 50–64, otherwise <code>"F"</code>.

Show the solution

if score >= 80: grade = "A", then elif score >= 65: grade = "B", elif score >= 50: grade = "C", else: grade = "F"

Exactly — if/elif/else checks top-down and stops at the first match, like nested IFs (or IFS).

Try it in the live editor →