Operators in Python 3

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 variables a and b the values 7.3 and 12 respectively.
As we explained earlier, Python automatically assigns the type "float" to a and the type "integer" to b.

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 Operators Beyond Basic Arithmetic

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.

✅ Basic Syntax:

base ** exponent

✅ Examples:

print(2 ** 3)   # 8  → 2 × 2 × 2
print(5 ** 2)   # 25 → 5 × 5
print(10 ** 0)  # 1

Negative and Fractional Powers

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.

Operator Precedence

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:

PParentheses () have the highest priority, allowing you to control the order of execution.

(2 - 5) ** (1 + 1) # = 4 (1 - 3) * 2 # = -4

EExponentiation (**) comes next, before any other operation.

** 1 * 3 # = 30 (not 59049) 1 + 1 ** 2 # = 2 (not 4)

M & DMultiplication (*) 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
  • Multiplication (*) happens first, so 100 * 59 = 5900.
  • Then, floor division (//) is applied: 60 // 5900, resulting in 0.
  • However, if floor division (//) were performed first (60 // 100), the result would be 0 (not 59).
  • Remember, // performs integer division, which means no decimal values remain.

Additionally, Python includes:

Logical operators

Logical operators are used to combine conditional expressions and return Boolean results.

Operators:

  • and
  • or
  • not

Behavior:

  • and → True if both conditions are True
  • or → True if at least one condition is True
  • notreverses the Boolean value

Example:

x = 10
y = 5
print(x > 5 and y < 10)   # True
print(x < 5 or y < 10)    # True
print(not(x > 5))         # False

Important (Python-specific behavior):

These operators return actual values, not just True/False:

print(0 or 5)     # 5
print(5 and 10)   # 10

String concatenation operators

Concatenation means joining strings together.

Main Operator:

  • + → joins strings

Example:

first = "Hello"
second = "World"result = first + " " + second
print(result)   # Hello World

Repetition Operator (related):

  • * → repeats a string
print("Hi " * 3)   # Hi Hi Hi

Important Rules:

  • Both operands must be strings:
# ❌ Error
"Age: " + 25# ✅ Correct
"Age: " + str(25)

Identity and membership operators

Identity Operators

Identity operators check whether two variables refer to the same object in memory.

Operators:

  • is
  • is not

Example:

a = [1, 2, 3]
b = a
c = [1, 2, 3]print(a is b)   # True  (same object)
print(a is c)   # False (different objects)

Key Concept:

  • is → checks memory identity
  • == → checks value equality
print(a == c)   # True (values same)

Membership Operators

Membership operators check whether a value exists in a sequence.

Operators:

  • in
  • not in

Example:

text = "Python"print("P" in text)       # True
print("z" not in text)   # True

Works with:

  • Strings
  • Lists
  • Tuples
  • Sets
  • Dictionaries (checks keys)
data = [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

Leave a Reply