Chapter 10 of 11

Python Operators

Operators are symbols or keywords that tell Python to perform an operation on values.

You've already used some operators without probably realizing it:

total = 10 + 5

Here, + is an operator used for addition.

Python provides different types of operators for calculations, comparisons, logical operations, and more.

Types of Operators in Python

The main types are:

Type

Purpose

Arithmetic

Perform mathematical calculations

Assignment

Assign values to variables

Comparison

Compare values

Logical

Combine conditions

Identity

Check whether two objects are the same

Membership

Check whether a value exists in a collection

Bitwise

Perform operations on binary values

Let's look at each one.

1. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

Operator

Meaning

Example

+

Addition

10 + 5

-

Subtraction

10 - 5

*

Multiplication

10 * 5

/

Division

10 / 5

//

Floor Division

10 // 3

%

Modulus

10 % 3

**

Exponentiation

2 ** 3

Example:

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

Output:

13
7
30
3.3333333333333335
3
1
1000

2. Assignment Operators

Assignment operators are used to assign or update values in variables.

The basic assignment operator is:

x = 10

Python also provides shortcuts:

Operator

Example

Equivalent To

=

x = 5

x = 5

+=

x += 2

x = x + 2

-=

x -= 2

x = x - 2

*=

x *= 2

x = x * 2

/=

x /= 2

x = x / 2

//=

x //= 2

x = x // 2

%=

x %= 2

x = x % 2

**=

x **= 2

x = x ** 2

For example:

score = 10
score += 5

print(score)

Output:

15

3. Comparison Operators

Comparison operators are used to compare two values. The result is always True or False.

Operator

Meaning

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

Example:

age = 20

print(age == 20)
print(age > 18)
print(age < 18)

Output:

True
True
False

Don't confuse = with ==.

x = 10    # Assignment
x == 10   # Comparison

4. Logical Operators

Logical operators are used to combine multiple conditions.

Python has three logical operators:

Operator

Meaning

and

Both conditions must be true

or

At least one condition must be true

not

Reverses the result

and

age = 20

print(age >= 18 and age <= 30)

Both conditions are true, so the result is:

True

or

age = 20

print(age < 18 or age > 18)

At least one condition is true, so the result is:

True

not

is_logged_in = True

print(not is_logged_in)

Output:

False

5. Identity Operators

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

There are two:

is
is not

Example:

a = [1, 2, 3]
b = a

print(a is b)

Output:

True

b refers to the same list object as a.

For everyday value comparisons, use ==. The is operator is mainly used for object identity, especially with values such as None.

6. Membership Operators

Membership operators check whether a value exists inside a sequence or collection.

There are two:

in
not in

Example:

fruits = ["Apple", "Banana", "Mango"]

print("Apple" in fruits)
print("Orange" not in fruits)

Output:

True
True

This is very useful when checking whether a particular item exists in a list, string, or other collection.

7. Bitwise Operators

Bitwise operators work with numbers at the binary level.

Some common bitwise operators are:

Operator

Meaning

&

AND

`

`

^

XOR

~

NOT

<<

Left shift

>>

Right shift

For example:

a = 5
b = 3

print(a & b)

Output:

1

You won't need bitwise operators often when starting with Python, but they are useful in areas such as low-level programming, networking, and performance-oriented code.

Operator Precedence

When an expression contains multiple operators, Python follows a certain order.

For example:

result = 10 + 5 * 2

print(result)

Output:

20

Multiplication happens before addition:

5 * 2 = 10
10 + 10 = 20

You can use parentheses when you want to make the order explicit:

result = (10 + 5) * 2

print(result)

Output:

30

A good habit is to use parentheses when they make your intention clearer.

A Practical Example

Let's use operators to check whether a student passed:

marks = 75

passed = marks >= 40
print("Passed:", passed)

Output:

Passed: True

We can make the condition a little more interesting:

marks = 75
attendance = 85

if marks >= 40 and attendance >= 75:
    print("John passed the exam")

Here, the comparison operators and the and logical operator work together to make the decision.

Operators may look like small symbols, but they are fundamental building blocks of Python programs. You'll use them in calculations, conditions, loops, and almost every program you write.