5. Arithmetic Operators

Quiz: 0/8

Arithmetic operators are tools to perform basic math in Python. Think of them as shortcuts for calculations like adding, subtracting, or multiplying numbers.

Here are the main arithmetic operators in Python:

Operator Symbol Example Result
Addition + 3 + 5 8
Subtraction - 10 - 4 6
Multiplication * 2 * 3 6
Division / 7 / 2 3.5
Floor Division // 7 // 2 3
Modulus % 7 % 2 1
Exponentiation ** 2 ** 3 8

The order of operations (PEMDAS)

When multiple operators are in one expression, Python follows the order of operations, or PEMDAS:

  1. Parentheses (): Solve inside parentheses first.
    Example: 2 * (3 + 5)2 * 8 = 16.

  2. Exponents **: Solve powers next.
    Example: 2 ** 3 * 48 * 4 = 32.

  3. Multiplication, Division *, /, //, %: Perform these from left to right.
    Example: 10 / 2 * 55 * 5 = 25.

  4. Addition, Subtraction +, -: Perform these last, from left to right.
    Example: 10 - 3 + 27 + 2 = 9.

Pro Tip:

Use parentheses to make your code clearer!

result = 2 + 3 * 4
# Without parentheses: Output = 14
result = (2 + 3) * 4
# With parentheses: Output = 20

Common mistakes and pitfalls

Mixing division types

Regular division / gives a floating-point number, even if it looks like a whole number.

print(4 / 2)  # Output: 2.0 (not 2)

Floor division // truncates the decimal part:

print(4 // 2)  # Output: 2

Misunderstanding modulus %

The modulus operator gives the remainder, not the quotient.

print(7 % 3)  # Output: 1 (7 divided by 3 has a remainder of 1)

Order of operations confusion

Forgetting the order of operations can lead to unexpected results.

result = 10 + 3 * 2  # Output: 16 (multiplication first)
result = (10 + 3) * 2  # Output: 26 (parentheses first)

Floating-point precision

Floating-point numbers might give surprising results due to how they’re stored:

print(0.1 + 0.2)  # Output: 0.30000000000000004

Real-world use cases

Here are practical examples of arithmetic operators in action:

1. Budget Calculator

income = 3000
rent = 1200
food = 500
savings = income - (rent + food)
print("Savings:", savings)

2. Discount Application

original_price = 50
discount = 20 / 100  # 20%
final_price = original_price - (original_price * discount)
print("Final Price:", final_price)

Summary

  • Arithmetic operators like +, -, *, /, //, %, and ** are essential for calculations in Python.
  • Remember the order of operations (PEMDAS) to avoid mistakes.
  • Use parentheses when in doubt to make your code clear.
  • Test your understanding with real-world examples and practice exercises.

Hands-On Practise

Exercise: Shopping cart calculator

You’re building a simple program to calculate the total cost of items in a shopping cart, including a discount and tax. Here's the breakdown:

  1. A customer adds the following items to their cart:

    • Apples: $2 each, quantity: 3
    • Bread: $1.5 each, quantity: 2
    • Milk: $3 each, quantity: 1
  2. A discount of 10% is applied to the total cost of items.

  3. A sales tax of 5% is added to the discounted price.

  4. Write a program to calculate:

    • The subtotal (cost of items before discounts/tax).
    • The discount amount.
    • The final total after applying the discount and adding tax.

Requirements

  • Use arithmetic operators to calculate the costs.
  • Ensure the order of operations is handled correctly.
  • Print the subtotal, discount amount, and final total with clear labels.

Bonus Challenges

  1. Add user input to allow the customer to specify the quantity of each item.
  2. Ensure the program rounds the final total to two decimal places.
  3. Add error handling to ensure all input is valid (e.g., no negative quantities).
Output:

Quizzes: 0/8

Question 1:

Which operator is used for exponentiation in Python?

  • **
  • ^
  • //
  • *

Question 2:

What is the output of the following code? `python result = 10 + 3 * 2 print(result) `

  • 26
  • 13
  • 16
  • 20

Question 3:

What does the modulus operator % do?

  • It divides two numbers and returns the quotient.
  • It raises a number to the power of another number.
  • It divides two numbers and returns the remainder.
  • It truncates a floating-point number to an integer.

Question 4:

What will be the output of the following code? `python result = 7 // 2 print(result) `

  • 3.5
  • 3
  • 4
  • Error

Question 5:

What is the correct order of operations in Python?

  • Multiplication → Division → Addition → Subtraction → Exponents → Parentheses
  • Parentheses → Exponents → Multiplication/Division → Addition/Subtraction
  • Addition → Subtraction → Multiplication → Division → Exponents → Parentheses
  • Exponents → Multiplication/Division → Addition/Subtraction → Parentheses

Question 6:

What is the output of the following code? `python print(0.1 + 0.2) `

  • 0.3
  • 0.30000000000000004
  • 0.2
  • Error

Question 7:

Which of the following statements is true about regular and floor division?

  • Both return the same result when dividing integers.
  • Regular division `/` returns a float, and floor division `//` truncates the decimal part.
  • Regular division `/` always returns an integer, and floor division `//` returns a float.
  • Floor division `//` always rounds up the result.

Question 8:

What is the output of the following code? `python result = (2 + 3) * 4 print(result) `

  • 14
  • 16
  • 20
  • 18

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonType conversionNext lessonComparison and logical Operators