Python Operators

We are going to be Covering Arithmetic, Comparism, Logical and Assignment

Arithmetic Operators

Arithmetic Operators are used to perform mathematical operations like addition, subtraction , multipliation e.t.c

				
					a = 10
b = 5
				
			

Example 1 Addition

Here we add a + b using th python addition operator

				
					print(a + b)
				
			

Example 2 Subtraction

Here, we subtract b from a using the python subtraction operator

				
					print(a - b) 
				
			

Example 3 Multiplication

Here we multiply a to b using the python multiplication operator

				
					print(a * b) 
				
			

Example 4 Division

Here we divide a by b using the python division operator.

The division operator in Python is “/” (it returns a float

There’s also “//” for floor division (This returns an integer result, rounded down”)

				
					print(a / b) 
				
			

Example 5 Modulus

The modulus “%” prints the remainder when “a” is divided by “b”

				
					print(a % b) 
				
			

Example 6 Exponentiation

This uses the exponentiation operator ** in Python.

				
					print(a ** b)  
				
			

Example 7 Floor Division

Ths uses the floor division operator “//” in Python. 

The floor division gives the largest wgle number less than or equal to the actual division result.

				
					print(a // b) 
				
			

Nex we are going to be looking at the comparism Operators.

Comparism operators are used to compare two values.

Example 8 Equal to

we use the  equality operator “==” to check if two values are equal.

it returns True if a and b have the same value, other wise False

				
					print(a == b)
				
			

Example 9 Not equal to

This line uses the “not equal operator !=” in Python.

!= checks if a and b are not equal.

It returns True if they are different, False if they are the same

				
					print(a != b)
				
			

Example 10 Greater than

Here we use the “greater than operator >” in Python.

“>” checks if a is greater than b.

it returns True if a is greater than b , otherwise False.

				
					print(a > b)
				
			

Example 11 Less than

This uses the “less than operator <” in Python.

“<” checks if a is less than b and returns  True if yes, otherwise False

				
					print(a < b)
				
			

Example 12 Greater than or equal to

This uses the greater than or equal to operator “>=” in Python.

“>=” checks if a is greater than or equal to b

It returns True if a is either greater or exact;y equal to b

				
					print(a >= b)
				
			

Example 13 Less than or equal to

Here we use the less than or equal to operator “<=” in Python.

“<=” checks if a is less than or equal to b. 

It returns True if a is either less than or equal to b

				
					print(a <= b)
				
			

Example 14 Chained Comparison

This uses chained comparism 

it checks if a is  between 5 and 15.

 

				
					print(5 < a < 15) 
				
			

Next we are going to look at Logical operators.

Logical operators are used to combine conditional statements.

				
					a = True
b = False
				
			

Example 15 AND

This uses the logical AND operator in Python.

The and returns the true if all are truthy and false if one or all are falsey.

				
					print(a and b)
				
			

Example 16 OR

This uses the logical OR operator in Python

it returns True for the first truthy value it encounters.

if a is falsy and b is truthy, it returs True.

In summary it returns True if either one is True. provided there is ine Truthy value present.

				
					print(a or b)
				
			

Example 17 Not

				
					print(not a)
				
			

Example 18 combined logical operators

				
					a = 10
b = 5
c = 15
				
			
				
					print((a < b) or (b < c))  
				
			

This checks if either a < b or b < c.

if at least one condition is true, the whole expression returns Trye

Assignment Operators

				
					a = 10
				
			

Example 19 Addition assignment

This adds 5 to a its same as a=a +5

				
					a += 5
print(a)
				
			

Example 20 Subtraction assignment

This substracts  3 from a same as a= a -3

				
					a -= 3
print(a)
				
			

Example 21 Multiplication assignment

This multiplies a by 2, same as a = a *2

				
					a *= 2
print(a)
				
			

Example 22 Division assignment

Thus Divides a by 4, same as a = a/4

It returns a float

				
					a /= 4
print(a)
				
			

Example 23 Modulus assignment

This sets a to the remainder of a /3. same as a  = a % 3

				
					a %= 3
print(a)
				
			

Example 24 Exponentiation assignment

This rasies a to the power of 2, same as a = a **2

				
					a **= 2
print(a)
				
			

Example 25 Floor division assignment

This performs floor division on a by 2, same as a = a //2

				
					a //= 2
print(a)
				
			

Assignment Operators with Multiple Variables

				
					a, b, c = 1, 2, 3
				
			

Here we update multiple variables at once using tuple unpacking.

 

				
					a, b, c = a + 1, b + 2, c + 3
print(a, b, c)  # Output: 2 4 6

				
			

Assignment Operators with Multiple Variables

This swaps the value of a and b.

				
					a, b = b, a
print(a, b)  # Output: 4 2
				
			

Identity Operators Identity operators are used to compare the memory locations of two objects.

Here a and b points to the same list object in memory.

c is a new list that just happens to have the same values

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

# is operator
print(a is b)  # Output: True
print(a is c)  # Output: False

# is not operator
print(a is not b)  # Output: False
print(a is not c)  # Output: True

				
			

Membership Operators Membership operators are used to test if a sequence is present in an object.

This checks if 3 is in the list a,

it checks if 6 is not in the list a,

 

it also checks if 3 is not in list a,

and it checks if 6 is not in a

				
					a = [1, 2, 3, 4, 5]

# in operator
print(3 in a)  # Output: True
print(6 in a)  # Output: False

# not in operator
print(3 not in a)  # Output: False
print(6 not in a)  # Output: True
				
			

Bitwise operators are used to perform bit-level operations.

print(a & b) # Bitwise AND → keeps only bits that are 1 in both a and b
print(a | b) # Bitwise OR → sets bits to 1 if they’re 1 in a or b
print(a ^ b) # Bitwise XOR → sets bits to 1 if different in a and b
print(~a) # Bitwise NOT → flips all bits of a (returns -a – 1)
print(a << 2) # Left shift → shifts bits left by 2 (adds 2 zeros on the right)
print(a >> 2) # Right shift → shifts bits right by 2 (removes 2 bits from right)

				
					a = 10  # 1010 in binary
b = 4   # 0100 in binary

# Bitwise AND
print(a & b)  # Output: 0 (0000 in binary)

# Bitwise OR
print(a | b)  # Output: 14 (1110 in binary)

# Bitwise XOR
print(a ^ b)  # Output: 14 (1110 in binary)

# Bitwise NOT
print(~a)  # Output: -11 (inverts all the bits)

# Bitwise left shift
print(a << 2)  # Output: 40 (101000 in binary)

# Bitwise right shift
print(a >> 2)  # Output: 2 (10 in binary)
				
			

Biwise AND with the mask keeps only the bits where the mask has 1. So 10101010 & 11110000 = 10100000, lowers 4 bits become 0

				
					#Bitwise operators are often used for masking operations.


mask = 0b11110000
value = 0b10101010

# Applying mask using bitwise AND
result = value & mask
print(bin(result))  # Output: 0b
				
			
				
					#You can define how operators behave for custom classes.


class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

v1 = Vector(2, 3)
v2 = Vector(4, 5)
print(v1 + v2)  # Output: Vector(6, 8)
				
			
				
					# Ternary Operator
# Python supports a ternary conditional operator, also known as a conditional expression.

a = 10
b = 20

# Ternary conditional operator
min_value = a if a < b else b
print(min_value)  # Output: 10
				
			

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *