Gulberg Green, Islamabad

Programming Fundamentals · Lesson 04

Expressions and operators

Combine values into calculations and control the order of operations.

An expression is a combination of values, names, and operators that produces a value. Addition uses +, subtraction uses -, multiplication uses *, and division uses /.

Python follows an order of operations. Multiplication and division happen before addition and subtraction. Parentheses let you group the part of a calculation that should happen first.

Suppose a workshop has a fixed delivery charge and a price per kit. Trace the expression by substituting the values for their names before doing the arithmetic.

Read the example

Python 3
kits = 3
price = 40
delivery = 10
print(kits * price + delivery)
print(kits * (price + delivery))
Expected output
130
150

The first calculation adds one delivery charge to the price of three kits. The second adds delivery to each kit before multiplying. The parentheses change the meaning of the calculation.

Try it yourself

A group buys four kits at 50 each and uses a discount of 20 on the whole order. Write an expression for the total and check it by hand.

Show a worked solution
kits = 4
price = 50
discount = 20
print(kits * price - discount)  # 180

Check your understanding

Your lesson quiz

Answer all five questions. Score at least 4 out of 5 (80%) to pass. You can retry as often as you like.

1. What is 3 * 40 + 10?
2. What is 3 * (40 + 10)?
3. Which operator multiplies in Python?
4. Which expression subtracts one discount of 20 from four kits at 50 each?
5. Why add parentheses to an expression?

Quiz progress is saved in this browser. View certificates →

Learn together

Was this lesson helpful?

Comments and reactions are not open yet.

WhatsApp