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
adivided byb. - Task-4: print the remainder of
adivided byb. - Task-5: print
braised to the power 2.
Need a hint?
Arithmetic operators: + and * for sum/product, // for floor division, % for remainder, ** for power.
Show the solution
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.
Need a hint?
Built-ins abs() for absolute value and divmod() which returns the (quotient, remainder) tuple.
Show the solution
Problem-3: Update In Place.
Write a program that:
- Task-1: starts a running
totalat50, adds 25 to it, then subtracts 10 from it, and prints the result. - Task-2: starts a
counterat0, increases it by 1 three times, and prints the result.
Need a hint?
Augmented assignment: += to add in place, -= to subtract in place.
Show the solution
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
stockis greater than 50. - Task-2: print whether
stockequals 72. - Task-3: print whether
stockis less than 40. - Task-4: print whether
stockis greater than or equal to 60.
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
Problem-2: Coupon Applied?
Write a program that, for a coupon whose value is None:
- Task-1: print whether
couponisNone. - Task-2: print whether
couponis notNone.
Need a hint?
Identity test with is None and is not None (not == for None).
Show the solution
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
oappears in the wordorder.
Need a hint?
Boolean operators and / or / not, plus the in membership operator on a string.
Show the solution
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")
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.
Need a hint?
all() is True only if every item is truthy; any() is True if at least one is.
Show the solution
Problem-3: More Checks.
Write a program that prints these boolean checks:
- Task-1: whether the letter
zdoes NOT appear in the wordstore. - Task-2: for a
priceof85, whether it sits between 0 and 100 inclusive, written as one chained comparison. - Task-3: the same
pricecheck written withand. - Task-4: whether
2 + 3equals5(the math runs before the comparison).
Need a hint?
not in for absence; chained comparison 0 <= price <= 100; arithmetic binds before ==.
Show the solution
# 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)
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.
Need a hint?
An if / elif / else ladder of cart_total tiers, largest threshold first.
Show the solution
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.
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
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.
Need a hint?
int(input()) then a single if cart_total >= 50 / else.
Show the solution
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.
Need a hint?
A nested if: the outer tests is_paid, the inner tests is_premium.
Show the solution
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).
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
# 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)
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.
Practice: Strings and fstrings Next: Practice: Collections