Python Shorthand If · Exercises
Beginner

Python Shorthand If Exercises

4 exercises that build on the Python Shorthand If (Ternary) lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: Shorthand if gives you two compact forms: a one-line if statement for a single action, and the conditional expression a if condition else b, which picks one of two values. By the end you can write one-line if statements, choose between two values with a conditional expression, chain two of them safely, use them inside f-strings and function calls, and tell when a full if block is the better choice. You will practise: Hot or mild in one line; Predict the one-liners; Fix the plural helper; Cart summary line.

0 / 4 completed

Python Shorthand If practice exercises

1. Hot or mild in one line

Write code Easy

Using a conditional expression on a single line, set weather to "hot" when temperature is above 30 and to "mild" otherwise. Then print weather.

Try a few other temperatures, then set it back to 31 before pressing Check.

Your code
temperature = 31

# one line: weather is "hot" if temperature > 30, otherwise "mild"

2. Predict the one-liners

Predict the output Medium

Read the code and type exactly what it prints, one line per print. Take care with the third print: think about which part of the line the if applies to. Then press Check.

Read this code
n = 4
parity = "even" if n % 2 == 0 else "odd"
size = "big" if n > 10 else "medium" if n > 3 else "small"
print(parity, size)
print(f"{n} item{'s' if n != 1 else ''}")
print("A" + "B" if n > 100 else "C")
if n > 3: print("done")

3. Fix the plural helper

Fix the bug Medium

describe(count, word) should give "3 apples" for 3 and "1 apple" for 1 (and "0 apples" for 0). It works for 3 but drops the number when count is 1. Fix it.

The code is wrapped in a function so the checker can try many inputs. You'll learn functions properly later; for now just edit the return line.

Your code
def describe(count, word):
    return str(count) + " " + word + "s" if count != 1 else word


print(describe(3, "apple"))   # should print 3 apples
print(describe(1, "apple"))   # should print 1 apple

4. Cart summary line

Write code Hard

Complete cart_summary(items, total) so it returns text like "3 items: shipping 5".

  • plural is "s" unless items is exactly 1, when it is empty text
  • shipping is "free shipping" when total is 100 or more, "shipping 5" when it is 50 or more, and "shipping 10" otherwise

Use a conditional expression for plural and a chain of two for shipping, each on one line.

The code is wrapped in a function so the checker can try many carts; keep the def line and the return line.

Your code
def cart_summary(items, total):
    plural = ""      # should be "s" unless items is exactly 1
    shipping = ""    # "free shipping", "shipping 5" or "shipping 10"
    return f"{items} item{plural}: {shipping}"


print(cart_summary(1, 20))   # should print 1 item: shipping 10
print(cart_summary(3, 75))   # should print 3 items: shipping 5

Next lessonPython Truthy and Falsy Values

More Python Shorthand If (Ternary) examples · All Python exercises