Skip to content

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.
Expected output
567
46.25
46
3
144
Need a hint?

Same operator family: +, /, //, %, ** on distance and fuel.

Show the solution
s04_workshop01_sol_arithmetic.py
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)

View the full solution file on GitHub

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.

Expected output
50
(4, 15)
Need a hint?

abs() for the price-drop size and divmod() for the boxes-of-20 pair.

Show the solution
s04_workshop01_sol_arithmetic.py
print(abs(-50))
print(divmod(95, 20))

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 (>, ==, <=).

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.
Expected output
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 >, ==, <= returning a bool.

Show the solution
s04_workshop02_sol_comparison.py
speed = 80
limit = 60
# Task-1: print whether the speed is above the limit
print(speed > limit)
# Task-2: print whether the speed equals the limit
print(speed == limit)
# Task-3: print whether the speed is 100 or below
print(speed <= 100)

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, 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.
Expected output
True
True
False
Need a hint?

and / or / not combining the fuel and engine checks.

Show the solution
s04_workshop03_sol_boolean_logic.py
fuel = 20
engine_ok = True
# Task-1: fuel left and engine is ok
print(fuel > 0 and engine_ok)
# Task-2: fuel above 50 or engine is ok
print(fuel > 50 or engine_ok)
# Task-3: print the opposite of engine_ok
print(not engine_ok)

View the full solution file on GitHub

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.
Expected output
False
True
Need a hint?

all() and any() over the features list.

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

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.

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.

Expected output
Service soon
Need a hint?

An if / elif / elif / else ladder on km, thresholds ascending.

Show the solution
s04_workshop04_sol_if_elif_else.py
km = 18000
if km < 10000:
    print("No service yet")
elif km < 20000:
    print("Service soon")
elif km < 30000:
    print("Service due")
else:
    print("Service overdue")

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 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.

Get the Video Course

Workshop: Strings and fstrings Next: Workshop: Collections

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