Theory of Intelligence
PYTHON PROGRAMMING•Data Types And Operators•Operators And Expressions•2.1 Arithmetic & Assignment

2.1.1 Arithmetic Operators

Subtopics: Arithmetic Operators, Assignment Operators and Expressions.

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.

PYTHON
# 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?

lightbulb

A 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.

PYTHON
# 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.

PYTHON
# 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.

PYTHON
# 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, 3+43 + 4 is an expression that results in 77. More complex expressions can use parentheses ( ) to control the order of operations. For example:

PYTHON
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.

BMI=weightheight2BMI = \frac{\text{weight}}{\text{height}^{2}}

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. (3+2)∗4(3 + 2) * 4 evaluates to 2020.
  • Exponentiation: Performs power operations next. 232^{3} evaluates to 88.
  • Multiplication '*', Division '/', and Modulus '%': These operations come next. 4∗34*3 evaluates to 1212, 10/210/2 evaluates to 5.05.0 and 11%311\%3 evaluates 22.
  • Addition '+' and Subtraction '-': These have lower precedence compared to multiplication and division. 5+25 + 2 evaluates to 77, and 10−410-4 evaluates to 66.

Class Activity

Compute the following expressions and compare results with your class fellows and class teacher.

  1. 10+3∗2∗∗2−5/510 + 3*2**2-5/5
  2. (10+3)∗(2∗∗(2−1))/5(10 + 3)*(2**(2 - 1))/5

DO YOU KNOW?

lightbulb

Using parentheses can help clarify complex expressions and ensure the operations are performed in the desired order.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!