2.1.1 Arithmetic Operators
Operators are symbols that perform operations on variables and values. An expression is a combination of variables, operators, and values that produces a result.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, modulus, exponentiation, and floor division as shown in the following code.
# Define variables
a = 10
b = 3
# Perform all arithmetic operations
print(a, "+", b, "=", a + b) # Output: 10 + 3 = 13
print(a, "*", b, "=", a * b) # Output: 10 * 3 = 30
print(a, "/", b, "=", a / b) # Output: 10 / 3 = 3.333333333333335
print(a, "%", b, "=", a % b) # Output: 10 % 3 = 1
print(a, "**", b, "=", a ** b) # Output: 10 ** 3 = 1000
DO YOU KNOW?
lightbulbA tutorial on Python is available at https://docs.python.org/3/tutorial/
Comparison Operators
Comparison operators are used to compare two values or expressions. They determine the relational logic between them, such as equality, inequality, greater than, less than, and so on. These operators return a boolean value (True or False) based on the comparison result.
# Define variables
x = 10
y = 5
# Greater than
print(x, ">", y, "=", x > y) # Output: 10 > 5 = True
# Less than
print(x, "<", y, "=", x < y) # Output: 10 < 5 = False
# Equal to
print(x, "==", y, "=", x == y) # Output: 10 == 5 = False
# Not Equal to
print(x, "!=", y, "!=", x != y) # Output: 10 != 5 = True
# Greater than or equal to
print(x, ">=", y, ">=", x >= y) # Output: 10 >= 5 = True
# Less than or equal to
print(x, "<=", y, "<=", x <= y) # Output: 10 <= 5 = False
Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left. There are also compound assignment operators like +=, -=, *=, and /=, which combine arithmetic operations with assignment.
# Define initial values
a = 10
b = 5
# Assignment
assignment = a; print("a = ", assignment) # Output: a = 10
# Addition assignment
a += b; print("a after addition = ", a) # Output: a = 15
# Subtraction assignment
a -= b; print("a after subtraction = ", a) # Output: a = 5
# Multiplication assignment
a *= b; print("a after multiplication = ", a) # Output: a = 50
# Division assignment
a /= b; print("a after division = ", a) # Output: a = 2.0
# Modulus assignment
a %= b; print("a after modulus division = ", a) # Output: a = 2
# Exponentiation assignment
a **= b; print("a after Exponentiation = ", a) # Output: a = 100000
Logical Operators
Logical operators are used to combine multiple conditions or expressions in a program. The most common logical operators are and, or and not. They are used to perform logical operations and return boolean values based on the evaluation of the expressions involved.
# Define variables
x = True
y = False
# Logical AND
logical_and = x and y
print(x, "and", y, "=", logical_and) # Output: True and False = False
# Logical OR
logical_or = x or y
print(x, "or", y, "=", logical_or) # Output: True or False = True
# Logical NOT
logical_not_x = not x
print("not", x, "=", logical_not_x) # Output: not True = False
Expressions
An expression is a combination of variables, operators, and values that produces a result. For example, is an expression that results in . More complex expressions can use parentheses ( ) to control the order of operations. For example:
result = (3 + 4) * 2 # result is 14
Class Activity
Write a program to calculate Body Mass Index (BMI). Ask the user for their weight and height, then compute and display their BMI and classification. The Body Mass Index (BMI) is calculated using the formula given below.
where:
- weight is in kilograms (kg)
- height is in meters (m)
Operator Precedence in Python
Operator precedence determines the order in which operations are performed in an expression. In Python as well as in Mathematics, certain operators have higher precedence and are evaluated before others.
- Parentheses '()': Highest precedence. Operations inside parentheses are performed first. evaluates to .
- Exponentiation: Performs power operations next. evaluates to .
- Multiplication '*', Division '/', and Modulus '%': These operations come next. evaluates to , evaluates to and evaluates .
- Addition '+' and Subtraction '-': These have lower precedence compared to multiplication and division. evaluates to , and evaluates to .
Class Activity
Compute the following expressions and compare results with your class fellows and class teacher.
DO YOU KNOW?
lightbulbUsing parentheses can help clarify complex expressions and ensure the operations are performed in the desired order.