Python Operators - RecentBlogger




Operators perform Mathematical, String, and Logical operations on values. Operands are expressions on which operations are performed. 

Python operators, in general, are used to performing operations on values and variables. 
Consider a simple expression 6 + 9 = 15. Here, 6 and 9 are called operands, and '+' is called an operator.

In this article, we will look into different types of Python operators with their syntax and we shall see them with examples.

Types of Operator in Python

  • Arithmetic Operators
  • Comparison / Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Identity Operators
  • Membership Operators
Now, let us have a look at all the operators mentioned above one by one with examples.

Python Arithmetic Operators

Arithmetic Operators can be used to perform operations like Addition, Subtraction, Multiplication, Division, and more



Now, let's see a couple of examples of the Arithmetic Operators below.

#Arithmatic Operator - Python
x = 9
y = 6

#Addition
print("Addition of x and y is: ",x + y)
#Subtraction
print("Subtraction of x and y is: ",x - y)
#Multiplication
print("Multiplication of x and y is: ",x * y)
#Division
print("Division of x and y is: ",x / y)
#Modulus
print("Modulus of x and y is: ",x // y)
#Power
print("x to the Power y is: ",x ** y)



#OUTPUT:

Addition of x and y is:  15
Subtraction of x and y is:  3
Multiplication of x and y is:  54
Division of x and y is:  1.5
Modulus of x and y is:  1
x to the Power y is:  531441


Python Comparison Operators

The Comparison Operators can be used to compare values. It will return either TRUE  or FALSE based on the condition.


Now, let's see the examples for Comparison operators below

#Comparison Operators - Python
x = 9
y = 6

#Equal to
print("x equal to y: ",x == y)
#Greater than
print("x is greater than y: ",x > y)
#Lesser than
print("x is lesser than y: ",x < y)
#Greater than or equal to
print("x greater than or equal to y: ",x >= y)
#Lesser than or equal to
print("x lesser than or equal to y: ",x <= y)
#Not equal to
print("x not equal to y: ",x != y)


#OUTPUT:

x equal to y:  False
x is greater than y:  True
x is lesser than y:  False
x greater than or equal to y:  True
x lesser than or equal to y:  False
x not equal to y:  True


Python Logical Operators 

Python logical operators perform Logical AND, Logical OR, and Logical NOT operations.


Now, let's see few examples on Logical Operators below,

#Logical Operators - Python
x = True
y = False

#AND
print("x and y is: ", x and y)
#OR
print("x or y is: ", x or y)
#NOT
print("not x is: ", not x)


#OUTPUT:

x and y is:  False
x or y is:  True
not x is:  False


Python Assignment Opertors

Python Assignment operators can be used to assign values to respective variables.


Now, let's see few examples below,

#Assignment Operators - Python
a = 10

# Assign value
b = a
print(b)

# Add and assign value
b += a
print(b)

# Subtract and assign value
b -= a
print(b)

# multiply and assign
b *= a
print(b)

# Divide and assign
b /= a
print(b)

# Modulus and assign
b %= a
print(b)

# Exponent value assignment
b %= a
print(b)


#OUTPUT:

10
20
10
100
10.0
0.0
0.0



Python Bitwise Operators

Bitwise operators act on bits and perform bit-by-bit operations. They are used to operate on binary numbers.


Now, let's see some examples below,

#Bitwise Operators - Python
x = 15
y = 6

# Bitwise AND operation
print(x & y)

# Bitwise OR operation
print(x | y)

# Bitwise NOT operation
print(~x)

# Bitwise XOR operation
print(x ^ y)

# Bitwise right shift operation
print(x >> 2)

# Bitwise left shift operation
print(x << 2)


#OUTPUT:

6
15
-16
9
3
60


Python Identity Operators

Python offers few special type operators, Identity operators are one among them.
Identity operators like 'is' and 'is not'. These are used to check if two values are located on the same part of memory. 


Now, let's see few examples below,

#Identity Operators - Python
a = 10
b = 20
c = a

print(a is b)
print(a is c)

print(a is not b)
print(c is not a)


#OUTPUT:

False
True
True
False


Python Membership Operators

Pythons other special type operators, Membership operators.
'in' and 'not in' are membership operators, used to test the variable or values are in a sequence.


Now, let's see few examples below

#Membership Operators - Python
x = 'Python World!'
y = {1:'p',2:'r'}

print('h' in x)

print('Python' not in x)

print(1 in y)

print('r' in y)


#OUTPUT:

True
False
True
False


Conclusion

We had seen Python Programming Operators, which will help understand Python better moving forward.  

Hope you found this article useful. Happy Coding!
Checkout our other articles for more tech related information.
Stay Connected! For more click here.



Post a Comment

3 Comments

  1. Well, Good Job I find this article is very interesting thanks for sharing the informative post with your experiences.Operators in Python

    ReplyDelete
  2. Very interesting to read...You have provided a nice article....Thanks for sharing. erp software companies

    ReplyDelete