Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Operator precedence and evaluation order
📖 Scenario: Imagine you are calculating the total cost of items in a shopping cart with discounts and taxes applied. You need to write a program that carefully follows the order of operations to get the correct final price.
🎯 Goal: Build a Python program that calculates the final price of an item after applying a discount and adding tax, demonstrating how operator precedence and evaluation order affect the result.
📋 What You'll Learn
Create variables for price, discount rate, and tax rate with exact values
Create a variable for the discounted price using subtraction and multiplication
Calculate the final price by adding tax to the discounted price using correct operator precedence
Print the final price with two decimal places
💡 Why This Matters
🌍 Real World
Calculating prices with discounts and taxes is common in shopping apps and billing systems.
💼 Career
Understanding operator precedence is essential for writing correct formulas in software development and data analysis.
Progress0 / 4 steps
1
Set up the initial price and rates
Create a variable called price and set it to 100. Create a variable called discount_rate and set it to 0.2. Create a variable called tax_rate and set it to 0.1.
Python
Hint
Use simple assignment statements to create the variables with the exact values given.
2
Calculate the discounted price
Create a variable called discounted_price that calculates the price after discount by subtracting price * discount_rate from price.
Python
Hint
Remember multiplication happens before subtraction. Write the expression exactly as price - price * discount_rate.
3
Calculate the final price with tax
Create a variable called final_price that adds tax to the discounted_price by calculating discounted_price + discounted_price * tax_rate.
Python
Hint
Use the same pattern as before: multiplication first, then addition. Write the expression exactly as shown.
4
Print the final price
Write a print statement to display the final_price rounded to two decimal places using f-string formatting like print(f"Final price: {final_price:.2f}").
Python
Hint
Use an f-string to format the number with two decimals. The output should exactly match Final price: 88.00.
Practice
(1/5)
1. Which operator has the highest precedence in the expression 3 + 4 * 2?
easy
A. Multiplication (*)
B. Addition (+)
C. Subtraction (-)
D. Division (/)
Solution
Step 1: Recall operator precedence rules
Multiplication (*) has higher precedence than addition (+), subtraction (-), and division (/).
Step 2: Identify highest precedence operator in expression
In 3 + 4 * 2, multiplication (*) runs before addition (+).
Final Answer:
Multiplication (*) -> Option A
Quick Check:
Highest precedence = Multiplication (*) [OK]
Hint: Multiplication and division run before addition and subtraction [OK]
Common Mistakes:
Thinking addition runs before multiplication
Ignoring operator precedence
Assuming left to right always applies
2. Which of the following expressions is syntactically correct in Python?
easy
A. 5 + * 3
B. 4 + (3 * 2)
C. 7 / / 2
D. 8 - -
Solution
Step 1: Check each expression for syntax errors
5 + * 3 has two operators in a row without operand: invalid. 4 + (3 * 2) uses parentheses correctly and valid operators. 7 / / 2 has double division operator which is invalid. 8 - - ends with operator without operand: invalid.
Step 2: Confirm correct syntax
Only 4 + (3 * 2) is syntactically correct: 4 + (3 * 2).
Final Answer:
4 + (3 * 2) -> Option B
Quick Check:
Valid syntax = 4 + (3 * 2) [OK]
Hint: Check for missing operands or extra operators [OK]
Common Mistakes:
Using two operators in a row
Missing parentheses around expressions
Ending expression with an operator
3. What is the output of the following code?
result = 10 - 3 * 2 + 4 // 2
print(result)
medium
A. 5
B. 8
C. 6
D. 4
Solution
Step 1: Apply operator precedence and evaluate multiplication and floor division first
3 * 2 = 6 4 // 2 = 2
Step 2: Evaluate the expression left to right with addition and subtraction
10 - 6 + 2 = 4 + 2 = 6
Final Answer:
6 -> Option C
Quick Check:
10 - 6 + 2 = 6 [OK]
Hint: Multiply and divide before add and subtract [OK]
Common Mistakes:
Adding before multiplying
Using normal division instead of floor division
Ignoring left to right evaluation for same precedence
4. Find the error in this expression:
value = 5 + (3 * 2
medium
A. No error, expression is correct
B. Wrong operator used
C. Extra operator before 2
D. Missing closing parenthesis
Solution
Step 1: Check parentheses balance
Expression has an opening parenthesis '(' but no matching closing parenthesis ')'.
Step 2: Identify syntax error
Missing closing parenthesis causes syntax error in Python.
Final Answer:
Missing closing parenthesis -> Option D
Quick Check:
Parentheses must be balanced [OK]
Hint: Count opening and closing parentheses carefully [OK]
Common Mistakes:
Ignoring missing parentheses
Assuming expression is valid without closing parenthesis
Confusing operator errors with syntax errors
5. Given the expression result = (2 + 3) * (4 - 1) ** 2 // 5, what is the value of result?
hard
A. 9
B. 15
C. 5
D. 25
Solution
Step 1: Evaluate parentheses and exponentiation first
(2 + 3) = 5 (4 - 1) = 3 3 ** 2 = 9
Step 2: Multiply and then floor divide
5 * 9 = 45 45 // 5 = 9
Final Answer:
9 -> Option A
Quick Check:
Parentheses and exponent first, then multiply, then floor divide [OK]
Hint: Do parentheses and powers before multiply/divide [OK]