Workshop: Operators, Booleans, and Conditionals (Optional, Self-Study)¶
View workshop code on GitHub Get the Video Course
From the lesson
Workshop problems for Section 04 Concepts. Build each yourself first, then expand the solution to check.
Four optional workshop problems, one per concept step, in a car scenario. The numbers line up with the steps. Self-study: the stubs say WHAT to do; check python-workshop/solutions.
Step-01: Arithmetic¶
Concepts: +, -, *, /, //, %, **.
Problem-1: Car Mileage.
Write a program that, for a distance of 555 and fuel of 12:
- Task-1: print the distance plus the fuel.
- Task-2: print the distance divided by the fuel.
- Task-3: print the whole-number part of that division.
- Task-4: print the remainder of that division.
- Task-5: print the fuel raised to the power 2.
Need a hint?
Same operator family: +, /, //, %, ** on distance and fuel.
Show the solution
distance = 555
fuel = 12
# Task-1: print the distance plus the fuel
print(distance + fuel)
# Task-2: print the distance divided by the fuel
print(distance / fuel)
# Task-3: print the whole-number part of that division
print(distance // fuel)
# Task-4: print the remainder of that division
print(distance % fuel)
# Task-5: print the fuel raised to power 2
print(fuel ** 2)
Problem-2: Price Helpers.
Write a program that prints the absolute value of -50, then prints the quotient and remainder of 95 divided by 20 together as a pair.
Need a hint?
abs() for the price-drop size and divmod() for the boxes-of-20 pair.
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 (>, ==, <=).
Write a program that, for a speed of 80 and a limit of 60:
- Task-1: print whether the speed is above the limit.
- Task-2: print whether the speed equals the limit.
- Task-3: print whether the speed is 100 or below.
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 >, ==, <= returning a bool.
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, all(), any().
Problem-1: Can Drive?
Write a program that, for a fuel of 20 and engine_ok set to True:
- Task-1: print whether there is fuel left and the engine is ok.
- Task-2: print whether the fuel is above 50 or the engine is ok.
- Task-3: print the opposite of
engine_ok.
Need a hint?
and / or / not combining the fuel and engine checks.
Show the solution
Problem-2: Car Features.
Write a program that, for a list features = [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() and any() over the features list.
Show the solution
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.
Write a program that, for a km reading of 18000, prints the service status using if / elif / else: No service yet when under 10000, Service soon when under 20000, Service due when under 30000, and Service overdue otherwise.
Need a hint?
An if / elif / elif / else ladder on km, thresholds ascending.
Show the solution
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 practice 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.
Workshop: Strings and fstrings Next: Workshop: Collections