Skip to content

Practice: Operators, Booleans, and Conditionals

View practice code on GitHub Get the Video Course

From the lesson

Practice problems for Section 04 Concepts. Build each yourself first, then expand the solution to check.

Four practice problems, one per concept step; the numbers line up. Build each yourself, then check python-practice/solutions. Some end with a short input() "Write a Program" exercise.

Step-01: Arithmetic

Concepts: + - * /, //, %, **, augmented assignment (+=).

Problem-1: Two-Number Math.

Write a program that, for two numbers a = 23 and b = 4:

  • Task-1: print their sum.
  • Task-2: print their product.
  • Task-3: print the whole-number part of a divided by b.
  • Task-4: print the remainder of a divided by b.
  • Task-5: print b raised to the power 2.
Expected output
27
92
5
3
16
Need a hint?

Arithmetic operators: + and * for sum/product, // for floor division, % for remainder, ** for power.

Show the solution
s04_practice01_sol_arithmetic.py
a = 23
b = 4

# Task-1: print their sum
print(a + b)
# Task-2: print their product
print(a * b)

# Task-3: print whole-number part of a // b
print(a // b)
# Task-4: print remainder of a divided by b
print(a % b)

# Task-5: print b raised to power 2
print(b ** 2)

View the full solution file on GitHub

Problem-2: Number Helpers.

Write a program that prints the absolute value of -9, then prints the quotient and remainder of 23 divided by 4 together as a pair.

Expected output
9
(5, 3)
Need a hint?

Built-ins abs() for absolute value and divmod() which returns the (quotient, remainder) tuple.

Show the solution
s04_practice01_sol_arithmetic.py
print(abs(-9))
print(divmod(23, 4))

View the full solution file on GitHub

Problem-3: Update In Place.

Write a program that:

  • Task-1: starts a running total at 50, adds 25 to it, then subtracts 10 from it, and prints the result.
  • Task-2: starts a counter at 0, increases it by 1 three times, and prints the result.
Expected output
65
3
Need a hint?

Augmented assignment: += to add in place, -= to subtract in place.

Show the solution
s04_practice01_sol_arithmetic.py
# Task-1: running total, add 25 then subtract 10
total = 50
total += 25
total -= 10
print(total)

# Task-2: counter from 0, add 1 three times
counter = 0
counter += 1
counter += 1
counter += 1
print(counter)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-02: Comparison

Concepts: comparison operators, is / is None.

Problem-1: Stock Check.

Write a program that, for a stock of 72:

  • Task-1: print whether stock is greater than 50.
  • Task-2: print whether stock equals 72.
  • Task-3: print whether stock is less than 40.
  • Task-4: print whether stock is greater than or equal to 60.
Expected output
True
True
False
True

True and False are capitalized

A comparison answers with the boolean True or False, always with a capital first letter. Lowercase true / false are not valid in Python and raise a NameError.

Need a hint?

Comparison operators >, ==, <, >= each evaluate to a bool.

Show the solution
s04_practice02_sol_comparison.py
stock = 72
# Task-1: print whether stock is greater than 50
print(stock > 50)
# Task-2: print whether stock equals 72
print(stock == 72)
# Task-3: print whether stock is less than 40
print(stock < 40)
# Task-4: print whether stock is at least 60
print(stock >= 60)

View the full solution file on GitHub

Problem-2: Coupon Applied?

Write a program that, for a coupon whose value is None:

  • Task-1: print whether coupon is None.
  • Task-2: print whether coupon is not None.
Expected output
True
False
Need a hint?

Identity test with is None and is not None (not == for None).

Show the solution
s04_practice02_sol_comparison.py
coupon = None
# Task-1: print whether coupon is None
print(coupon is None)
# Task-2: print whether coupon is not None
print(coupon is not None)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-03: Boolean Logic

Concepts: and, or, not, in / not in, chained comparison, precedence.

Problem-1: Order Check.

Write a program that, for an order total of 30 and in_stock set to True:

  • Task-1: print whether the total is above 25 and it is in stock.
  • Task-2: print whether the total is above 40 or it is in stock.
  • Task-3: print the opposite of in_stock.
  • Task-4: print whether the letter o appears in the word order.
Expected output
True
True
False
True
Need a hint?

Boolean operators and / or / not, plus the in membership operator on a string.

Show the solution
s04_practice03_sol_boolean_logic.py
total = 30
in_stock = True
# Task-1: total above 25 and in stock
print(total > 25 and in_stock)
# Task-2: total above 40 or in stock
print(total > 40 or in_stock)
# Task-3: print the opposite of in_stock
print(not in_stock)

# Task-4: whether "o" appears in "order"
print("o" in "order")

View the full solution file on GitHub

Problem-2: All In Stock?

Write a program that, for a list in_stock_flags = [True, False, True] (square brackets hold several values at once; lists get a section of their own next):

  • Task-1: print whether every item is true.
  • Task-2: print whether at least one item is true.
Expected output
False
True
Need a hint?

all() is True only if every item is truthy; any() is True if at least one is.

Show the solution
s04_practice03_sol_boolean_logic.py
in_stock_flags = [True, False, True]
# Task-1: whether every item is true
print(all(in_stock_flags))
# Task-2: whether at least one item is true
print(any(in_stock_flags))

View the full solution file on GitHub

Problem-3: More Checks.

Write a program that prints these boolean checks:

  • Task-1: whether the letter z does NOT appear in the word store.
  • Task-2: for a price of 85, whether it sits between 0 and 100 inclusive, written as one chained comparison.
  • Task-3: the same price check written with and.
  • Task-4: whether 2 + 3 equals 5 (the math runs before the comparison).
Expected output
True
True
True
True
Need a hint?

not in for absence; chained comparison 0 <= price <= 100; arithmetic binds before ==.

Show the solution
s04_practice03_sol_boolean_logic.py
# Task-1: whether "z" is NOT in "store"
print("z" not in "store")
price = 85
# Task-2: price between 0 and 100 as chained comparison
print(0 <= price <= 100)
# Task-3: same price check written with and
print(price >= 0 and price <= 100)
# Task-4: whether 2 + 3 equals 5
print(2 + 3 == 5)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-04: Making Decisions

Concepts: if / elif / else, the ternary (one-line if / else), nested if, %, input().

Problem-1: Shipping by Cart Total.

Write a program that, for a cart_total of 70, prints the shipping cost using if / elif / else: 0 when the total is 100 or more, 5 when it is 50 or more, 10 when it is 20 or more, and 15 otherwise.

Expected output
5
Need a hint?

An if / elif / else ladder of cart_total tiers, largest threshold first.

Show the solution
s04_practice04_sol_if_elif_else.py
# Problem-1 Task-1: shipping cost by cart total with if / elif / else
cart_total = 70
if cart_total >= 100:
    shipping = 0
elif cart_total >= 50:
    shipping = 5
elif cart_total >= 20:
    shipping = 10
else:
    shipping = 15
print(shipping)

View the full solution file on GitHub

Problem-2 (Write a Program): Pack in Pairs.

Write a program that asks for a quantity with the prompt Enter a quantity: and prints <n> packs in pairs when the number divides evenly by 2 and <n> has a leftover otherwise.

Example run
Enter a quantity: 7
7 has a leftover

int() needs a whole number

int(input()) converts the typed text to a whole number. If you type something that is not a whole number (like 7.5 or hello), it raises a ValueError and the program stops.

Need a hint?

int(input()) then number % 2 == 0 to branch even vs odd; format the result with an f-string.

Show the solution
s04_practice04_sol_if_elif_else.py
# Problem-2 Task-1: pack in pairs, flag a leftover with modulo
quantity = int(input("Enter a quantity: "))
if quantity % 2 == 0:
    print(f"{quantity} packs in pairs")
else:
    print(f"{quantity} has a leftover")

View the full solution file on GitHub

Problem-3 (Write a Program): Free Shipping?

Write a program that asks for a cart total with the prompt Enter your cart total: and prints free shipping when it is 50 or more and add more otherwise.

Example run
Enter your cart total: 20
add more
Need a hint?

int(input()) then a single if cart_total >= 50 / else.

Show the solution
s04_practice04_sol_if_elif_else.py
# Problem-3 Task-1: free shipping when cart total is 50 or more
cart_total_in = int(input("Enter your cart total: "))
if cart_total_in >= 50:
    print("free shipping")
else:
    print("add more")

View the full solution file on GitHub

Problem-4: Order Status.

Write a program that, for is_paid set to True and is_premium set to False, prints Paid when the order is paid and then, still inside that branch, prints Premium delivery when the customer is premium and Standard delivery when not; when not paid, print Awaiting payment instead.

Expected output
Paid
Standard delivery
Need a hint?

A nested if: the outer tests is_paid, the inner tests is_premium.

Show the solution
s04_practice04_sol_if_elif_else.py
# Problem-4 Task-1: nested order status by paid then premium
is_paid = True
is_premium = False
if is_paid:
    print("Paid")
    if is_premium:
        print("Premium delivery")
    else:
        print("Standard delivery")
else:
    print("Awaiting payment")

View the full solution file on GitHub

Problem-5: Stock Status.

Write a program that, for a units count of 0, sets status to in stock when units is above 0 and sold out otherwise, then prints status. Do it two ways: first with a regular if / else, then with the one-line if / else (the ternary).

Expected output
sold out
sold out
Need a hint?

Do it both ways: a regular if / else first, then the one-line ternary status = A if condition else B (the value before if when True, otherwise the value after else) - both set the same status.

Show the solution
s04_practice04_sol_if_elif_else.py
# Problem-5 Task-1: stock status two ways, plain if / else then ternary
# Using a regular if / else
units = 0
if units > 0:
    status = "in stock"
else:
    status = "sold out"
print(status)

# Using the ternary (one-line if / else)
units = 0
status = "in stock" if units > 0 else "sold out"
print(status)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Finished this section?

Check your work against the solutions above, then head back to the Section 04 lesson, or try the Section 04 workshop for more reps.

Prefer to learn by watching?

The full video course teaches every concept on screen and builds the projects with you, step by step.

Get the Video Course

Practice: Strings and fstrings Next: Practice: Collections

🎉 New Course
Python for Absolute Beginners
$15.99 $84.99 81% OFF
Get the Video Course