Operators
Seven operator families and precedence
Unit 2 · Operators
Give Python instructions to calculate and compare
An operator is a symbol or word that tells Python what to do with values. Choose a family, change the marks and bonus below, and run real Python code to see each operation.
| Operator | Meaning |
|---|---|
| + | Add |
| - | Subtract |
| * | Multiply |
| / | Divide |
| // | Floor divide |
| % | Remainder |
| ** | Power |
marks = 6
bonus = 3
print("Total:", marks + bonus)
print("Double:", marks * 2)
print("Remainder:", marks % bonus)Output
Your program's output will appear here.
Common Python mistakes
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
Interactive expression
Which operation happens first?
Python follows precedence rules: powers before multiplication or division, then addition or subtraction. Operators of the same level usually run left to right; powers run right to left.
6 + 3 * 2 = 12
- 1First calculate
3 * 2= 6. - 2Then calculate
6 + 6= 12.
Now you try
Add a discount calculation: subtract a discount from a book price, then check whether the final price is under 500.
# Task: calculate the sale price and check if it is below 500. price = 640 discount = 175 # Write your calculation and comparison below
Output
Your program's output will appear here.
Common Python mistakes
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
Common mistakes
Compare values with ==, assign them with =
NameError: name 'mark' is not defined
A misspelled variable name means Python cannot find that value. Check that both names match.
A name was used before Python could find a variable or function with that name.
For object identity, use is for special values such as None; use == to compare ordinary values and contents.
1. What is evaluated first in 6 + 3 * 2?
2. Which operator tests whether two values are equal?
Next, use conditions and loops to make a program choose and repeat.