Skip to main content

✍️ Assignment operators

NoteLearning Objectives Checklist

✍️ Practice

True or False, the following programs contains a SyntaxError:

Program 1

value = 2
value = value + 3

False

Program 2

value = 4
value2 = value+=2

True, cannot use two assignment operators (= and +=) in the same expression. This should be done on two separate lines.

Program 3

value = 10
print(value-=1)

True, cannot print and assignment expression. This should be done on two separate lines.

Program 4

value1 = 34
value2 = 44
value1 += value2

False

Trace the following programs and the data types of variables

Program 5

What’s the ouput of the program and the data type of num1, num2, and num3:

num1 = 23
num2 = 45
num3 = "234"
num1 += 1
num2 -= num1
num3 += str(num1)
num3 += str(num2)
num2 = num2 * 2
print(num1, num2, num3)
24 42 "2342421"

int, int, str

Program 6

What’s the ouput of the program and the data type of a, b, and s:

a = 15
b = 15
s = "111"

a = b//a
b = b % 4

s += "1"
a = b / a

print(a)
print(b)
print(s)
3.0
3
"111"

float int str

🧩 Problem Solving

1. Food Truck Delivery 🚚

Imagine you’re running a busy food truck that starts its day with 20 pending orders and 0 km traveled. As you make deliveries along your route, you’ll stop twice to distribute orders and cover additional distance. Your Task:

  1. Initialize Your Variables: orders = 24 distance = 0 (in kilometres)
  2. Perform the Following Updates (without using loops):
  • Stop 1:
    • Decrease the number of orders by 8
    • Increase the distance by a fixed amount ( add 10 km).
  • Stop 2:
    • Decrease the number of orders by 16
    • Increase the distance by another fixed amount ( add 15 km).

Write a Python Script: Update the variables sequentially and then print the final total distance travelled and the remaining number of orders.

orders = 24
distance = 0

# stop 1
orders -= 8
distance += 10


# stop 2
orders -= 16
distance += 15

print(f"Remaining orders: {orders}, Distance: {distance} Km")
Remaining orders: 0, Distance: 25 Km