Python Operators Explained Like Never Before: Master Them in 10 Minutes

Students generally focus on the variables, print statements, and fundamental syntax when they start learning Python. But when you start writing program from thinking to execute in this situation Python operators are very useful.

Python operators are some symbols which are used to perform specific operation on variables and values. Values and variables on which we perform some operations are knowns as operands. Python operators are the foundation of a logic which build in the python programming.

Check this out if you want learn more about python programming. click here

What Are Python Operators?

Python operators are special symbols or keywords that perform operations on variables and values. They tell Python what action to perform.

For example:

  • + tells Python to add
  • > tells Python to compare
  • and tells Python to check multiple conditions

Operators can be equivalent to tools in a toolbox. Every tool serves a certain function, such as cutting, measuring, or joining. In a similar vein, operators assist your software in carrying out certain jobs effectively.

Programs would just store data without operators; they would never process it.

Why Python Operators Are Important

Understanding python operators is crucial because they are used in:

  • Mathematical calculations
  • Decision-making (if-else logic)
  • Loop conditions
  • Data filtering
  • Automation scripts
  • Data science and machine learning formulas

In short, operators make programs dynamic and intelligent.

Types of Python Operators

Different types of Operators used in Python are as follows:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic Operators

Arithmetic operators are used to operate mathematical operations on two operands, like addition, multiplication, division and subtraction. There are different arithmetic operators

  • Exponent (**)
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Floor division (//)
  • Modulus/ Remainder (%)
OperatorMeaningExampleResult
+Addition10 + 515
Subtraction10 – 55
*Multiplication10 * 550
/Division10 / 52.0
//Floor Division10 // 33
%Modulus (Remainder)10 % 31
**Exponent2 ** 38
a = 15
b = 4

print('Addition of two numbers: a + b =', a + b)      
print('Subtraction of two numbers: a - b =', a - b)  
print('Multiplication of two numbers: a * b =', a * b)      
print('Division of two numbers: a / b =', a / b)      
print('Floor division of two numbers: a // b =',a // b)      
print('Reminder of two numbers: a mod b =', a % b)      
print('Exponent of two numbers: a ^ b =',a ** b)

Output:
Addition of two numbers: a + b = 19
Subtraction of two numbers: a - b = 11
Multiplication of two numbers: a * b = 60
Division of two numbers: a / b = 3.75
Floor division of two numbers: a // b = 3
Reminder of two numbers: a mod b = 3
Exponent of two numbers: a ^ b = 50625

2. Comparison Operators

These operators are used to compare the value of two operands and it give in return Boolean value “True” or “False”. There are many comparison operators in Python like ‘==’, ‘!=’, ‘<=’, ‘>=’, ‘<‘, and ‘>’.

OperatorMeaning
==Equal to
!=Not equal to
Greater than
Less than
>=Greater than or equal to
<=Less than or equal to
a = 20    # Initializing the value of a      
b = 16     # Initializing the value of b      
    
print("For a =", a, "and b =", b,"\nCheck the following:")    
    
# printing different results    
print('Two numbers are equal or not:', a == b)      
print('Two numbers are not equal or not:', a != b)      
print('a is less than or equal to b:', a <= b)      
print('a is greater than or equal to b:', a >= b)      
print('a is greater than b:', a > b)      
print('a is less than b:', a < b)

Output:
For a = 20 and b = 16 
Check the following:
Two numbers are equal or not: False
Two numbers are not equal or not: True
a is less than or equal to b: False
a is greater than or equal to b: True
a is greater than b: True
a is less than b: False

3. Assignment Operators

The outcome of the correct expression is assigned to the left operand using the assignment operators. To assign values to a variable, Python provides a variety of assignment operators. ‘=’, ‘+=’, ‘-=’, ‘=’, ‘/=’, ‘%=’, ‘//=’, ‘*=’, ‘&=’, ‘|=’, ‘^=’, ‘>>=’, and ‘<<=’ are some of these assignment operators.

OperatorExampleSame As
=x = 10Assign value
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5

a = 24         # Initialize the value of a      
b = 20         # Initialize the value of b      
    
# printing the different results    
print('a += b:', a + b)      
print('a -= b:', a - b)      
print('a *= b:', a * b)      
print('a /= b:', a / b)      
print('a %= b:', a % b)    
print('a **= b:', a ** b)      
print('a //= b:', a // b)  

Output: 
a += b: 44
a -= b: 4
a *= b: 480
a /= b: 1.2
a %= b: 4
a **= b: 4019988717840603673710821376
a //= b: 1

4. Logical Operators

Logical operators are used to evaluate the expression to make decisions. In python there are three logical operators like “or”, “and”, and “not”. In “and” operation both conditions should be True, in “or” operator one condition should be True and in “not” operator it will revers your output.

OperatorMeaning
andTrue if both conditions are true
orTrue if at least one condition is true
notReverses the condition
a = 7          # initializing the value of a    
# printing different results    
print("For a = 7, checking whether the following conditions are True or False:")    
print('\"a > 5 and a < 7\" =>', a > 5 and a < 7)      
print('\"a > 5 or a < 7\" =>', a > 5 or a < 7)    
print('\"not (a > 5 and a < 7)\" =>', not(a > 5 and a < 7))


Output:
For a = 7, checking whether the following conditions are True or False:
"a > 5 and a < 7" => False
"a > 5 or a < 7" => True
"not (a > 5 and a < 7)" => True

5. Bitwise Operators

Bitwise operators work on binary (0 and 1) values. They are mostly used in low-level programming.

OperatorMeaning
&AND
^XOR
~NOT
<< Left shift
>> Right shift
a = 7          # initializing the value of a      
b = 8          # initializing the value of b      
    
# printing different results    
print('a & b :', a & b)      
print('a | b :', a | b)      
print('a ^ b :', a ^ b)      
print('~a :', ~a)    
print('a << b :', a << b)      
print('a >> b :', a >> b)

Output:
a & b : 0
a | b : 15
a ^ b : 15
~a : -8
a << b : 1792
a >> b : 0

6. Membership Operators

These operators check if a value exists in a sequence like a list or string.

OperatorMeaning
inExists
not inDoes not exist
myList = [12, 13, 23, 24, 54, 57, 78, 89, 134, 36473]    
    
# initializing x and y with some values    
x = 13    
y = 90  
    
# printing the given list    
print("Given List:", myList)    
    
# checking if x is present in the list or not    
if (x not in myList):    
    print("x =", x,"is NOT present in the given list.")    
else:    
    print("x =", x,"is present in the given list.")    
    
# checking if y is present in the list or not    
if (y in myList):    
    print("y =", y,"is present in the given list.")    
else:    
    print("y =", y,"is NOT present in the given list.")

Output:
Given List: [12, 13, 23, 24, 54, 57, 78, 89, 134, 36473]
x = 13 is present in the given list.
y = 90 is not present in the given list.

7. Identity Operators

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

OperatorMeaning
isSame object
is notDifferent object
# initializing two variables a and b    
a = ["Rahul", "Abhay"]      
b = ["Raman", "Aman"]      
    
# initializing a variable c and storing the value of a in c    
c = a      
    
# printing the different results    
print("a is c => ", a is c)    
print("a is not c => ", a is not c)    
print("a is b => ", a is b)    
print("a is not b => ", a is not b)    
print("a == b => ", a == b)    
print("a != b => ", a != b)    

Output:
a is c =>  True
a is not c =>  False
a is b =>  False
a is not b =>  True
a == b =>  False
a != b =>  True

Real-Life Programs Using Python Operators

1. Even or Odd Checker

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")

2. Simple Login System

user = "admin"
pwd = "1234"

if user == "admin" and pwd == "1234":
    print("Login Successful")
else:
    print("Access Denied")

3. Shopping Cart Total

price = 299
quantity = 3
total = price * quantity
print("Total Bill:", total)

Common Mistakes Beginners Make

  1. Using = instead of ==
  2. Confusing / and //
  3. Misusing is for value comparison
  4. Forgetting operator precedence

Avoiding these mistakes in python operators makes your code professional. And if you want to learn about mistakes in Python programming click here

Conclusion

One of the most crucial phases in learning Python programming is becoming proficient with Python operators. Your program’s computation, comparison, and decision-making processes are governed by these little symbols.
Understanding Python operators makes it easier to write logic, speeds up debugging, and makes your programs smarter. Python Operators are tools you will utilize on a daily basis, regardless of your level of coding expertise.

Leave a Comment

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

Scroll to Top