Operators in python are special symbols used to represent basic operations. For example, arithmetic operations such as addition, subtraction, multiplication, and division.
You can manipulate variable values using operators to form expressions. For example:
a, b = 7.3, 12 y = 3 * a + b / 5
In this example, we first assign the variablesaandbthe values 7.3 and 12 respectively.
As we explained earlier, Python automatically assigns the type "float" toaand the type "integer" tob.
The second line in our example assigns the variable y the result of the expression containing the operators *, +, and / along with the values a, b, 3, and 5.
Python evaluates every expression given to it, even if complex, and the result always has a value.
This value is automatically assigned a type, depending on the components of the expression.
In the example above, y will be a float, because the evaluated expression contains at least one floating-point number.
Python provides more than just the four basic arithmetic operators. In python 3 floor division operator (//) was introduced to force integer division.
In Python 3:
59 / 60 0.983333...
In Python 2:
59 / 60
To force integer division in Python 3:
59 // 60
There is also the exponentiation operator (**). The exponentiation operator ** is used to raise a number to the power of another number.
base ** exponent
print(2 ** 3) # 8 → 2 × 2 × 2 print(5 ** 2) # 25 → 5 × 5 print(10 ** 0) # 1
print(2 ** -2) # 0.25 → 1 / (2²) print(9 ** 0.5) # 3.0 → square root of 9
Another useful operator is the modulo operator (%), which returns the remainder of a division operation.
Try the following:
10 % 3 # Notice what happens! 10 % 5
This operator is useful for checking whether a number a is divisible by b.
Simply using a % b will return zero (0) if a is divisible by b.
When multiple operators appear in an expression, we must understand the order of operations, which follows basic mathematical precedence rules.
In Python, the rules are the same as in mathematics, and you can remember them using the PEMDAS mnemonic:
P → Parentheses () have the highest priority, allowing you to control the order of execution.
(2 - 5) ** (1 + 1) # = 4 (1 - 3) * 2 # = -4
E → Exponentiation (**) comes next, before any other operation.
** 1 * 3 # = 30 (not 59049) 1 + 1 ** 2 # = 2 (not 4)
M & D → Multiplication (*) and Division (/) come before Addition (+) and Subtraction (-).
1 - 2 / 3 # ≈ 0.3333 (not -1.0) 1 - 3 * 2 # = -5 (not 4)
If multiple operators have the same precedence, evaluation proceeds from right to left.
For example, in the expression:
60 // 100 * 59
*) happens first, so 100 * 59 = 5900.//) is applied: 60 // 5900, resulting in 0.//) were performed first (60 // 100), the result would be 0 (not 59).// performs integer division, which means no decimal values remain.Additionally, Python includes:
Logical operators are used to combine conditional expressions and return Boolean results.
andornotand → True if both conditions are Trueor → True if at least one condition is Truenot → reverses the Boolean valuex = 10 y = 5 print(x > 5 and y < 10) # True print(x < 5 or y < 10) # True print(not(x > 5)) # False
These operators return actual values, not just True/False:
print(0 or 5) # 5 print(5 and 10) # 10
Concatenation means joining strings together.
+ → joins stringsfirst = "Hello" second = "World"result = first + " " + second print(result) # Hello World
* → repeats a stringprint("Hi " * 3) # Hi Hi Hi# ❌ Error "Age: " + 25# ✅ Correct "Age: " + str(25)
Identity operators check whether two variables refer to the same object in memory.
isis nota = [1, 2, 3] b = a c = [1, 2, 3]print(a is b) # True (same object) print(a is c) # False (different objects)
is → checks memory identity== → checks value equalityprint(a == c) # True (values same)
Membership operators check whether a value exists in a sequence.
innot intext = "Python"print("P" in text) # True
print("z" not in text) # Truedata = [1, 2, 3]print(2 in data) # True print(5 in data) # False
In the next article we will learn about Control Structures in Python.
No Comments