Python Operators

Operators are used to perform certain operations in programming languages. These are nothing but symbols. Python provides the following set of operators.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used with numeric values to perform mathematical operations.

Operator Symbol Name of  the Operator  Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division a / b
% Modules a % b
// Floor division a //  b
** Exponentiation  a ** b
Practice these in your Python IDLE.

a=10
b=2
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)

Assignment Operators

You can use assignment operator to assign value to the variable.

Operator Example Same As
= a = 10 a = 10
+= a += 10 a = a + 10
-= a -= 10 a = a – 10
*= a *= 10 a = a * 10
/= a /= 10 a = a / 10
//= a //= 10 a = a //10
**= a **= 10 a = a ** 10
&= a &= 10 a = a & 10
!=  a != b Not equal 
^= x ^= 10 x = x ^ 10
>>= a >>= 10 a = a >> 10
<<= a <<= 10 a = a << 10
%=   a %= 10 a = a % 10
|=   a |= 10 a = a | 10
Practice these in your Python IDLE.

x = 5

print(x)
print(x += 10)
print(x -= 10)
print(x *= 10)
print(x /= 10)
print(x %= 10)
print(x //= 10)
print(x **= 10)
print(x &= 10)
print(x |= 10)
print(x ^= 10)
print(x >>= 10)
print(x <<= 10)

Comparison Operators

These operators are used to compare two arguments.

Operator  Name  Example
==  Equal  a == b
!=  Not equal a != b
Greater than a > b
Less than  a < b
>=  Greater than or equal to a >= b
<=  Less than or equal to a <= b
Practice these in your Python IDLE.

a = 5
b = 10

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)

Logical Operators

You can use Logical operators to combine conditional statements.

Operator Example Description
and  x < 15 and x < 20 If both arguments are True then only result is True
or x < 15 or x < 14 If atleast one argument is True then result is True
not not(x < 15 and x < 20) Reverse the result, returns False if the result is true
Practice these in your Python IDLE.

x = 5

print(x < 15 and x < 20)
print(x < 15 or x < 14)
print(not(x < 15 and x < 20))

Identity Operators

You can compare the objects using Identity operators.

Operator Example Description
is a is b Returns True if both variables are the same object
is not a is not b Returns True if both variables are not the same object
Practice these in your Python IDLE.

a=10
b=10
print(a is b)

x=True
y=True
print(x is not y)

Membership Operators

Membership operators are used to test whether a sequence is presented in an object or not.

Operator Example Description
in a in b Returns True if a sequence with the specified value is present in the object
not in a not in b Returns True if a sequence with the specified value is not present in the object
Practice these in your Python IDLE.

a = ["Python", "Awesome"]

print("Python" in a)
print("Python" not in a)

Bitwise Operators

You can use bitwise operators to compare binary numbers.

Operator Symbol Name of Operator  Description
& AND If both bits are 1 then only result is 1 otherwise result is 0
| OR If atleast one bit is 1 then result is 1 otherwise result is 0
^ XOR If bits are different then only result is 1 otherwise result is 0
~ NOT bitwise complement operator i.e 1 means 0 and 0 means 1
<< Zero Fill Left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed Right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

 

Practice these in your Python IDLE.

a = 4
b = 5

print(a & b) ==> 4
print(a | b) ==> 5
print(a ^ b) ==> 1

Previous Read : Python Booleans

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment