DEV Community

Cover image for Operators in Python
Rakshambika
Rakshambika

Posted on

Operators in Python

What is Operator?

  • Operators in Python are special symbols or keywords used to perform operations on variables.

Types of Operators:

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

Arithmetic Operator:

  • Used to perform mathematical operations.
Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
// Floor Division 10 // 3 3
% Modulus 10 % 3 1
** Exponentiation 2 ** 3 8

Example:

a = 10
b = 3

print(a + b)
print(a-b)
print(a*b)
print(a // b)
print(a % b)
print(a ** b)
Enter fullscreen mode Exit fullscreen mode
13
7
30
3
1
1000
Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators

Used to assign values to variables or update their values.

Operator Example Equivalent
= a = 10 Assign 10
+= a += 5 a = a + 5
-= a -= 5 a = a - 5
*= a *= 5 a = a * 5
/= a /= 5 a = a / 5
//= a //= 5 a = a // 5
%= a %= 5 a = a % 5
**= a **= 2 a = a ** 2

Example:

a=10

a += 5;
print(a)

a -= 5;
print(a)

a *= 5
print(a)

a /= 5
print(a) 
Enter fullscreen mode Exit fullscreen mode
15
10
50
10.0
Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators

Used to compare two values. They return either True or False.

Operator Meaning Example Result
== Equal to 10 == 10 True
!= Not equal to 10 != 5 True
> Greater than 10 > 5 True
< Less than 10 < 5 False
>= Greater than or equal to 10 >= 10 True
<= Less than or equal to 5 <= 10 True

Example:

a = 10
b = 5

print(a == b)  
print(a > b)   
print(a != b)  
Enter fullscreen mode Exit fullscreen mode
False
True
True
Enter fullscreen mode Exit fullscreen mode

4. Logical Operators

Used to combine multiple conditions.

Operator Meaning Example Result
and True if both conditions are True True and False False
or True if at least one condition is True True or False True
not Reverses the result not True False

Example:

age = 24

print(age > 18 and age < 30)  
print(age > 30 or age == 24)  
print(not age > 18)           
Enter fullscreen mode Exit fullscreen mode
True
True
False
Enter fullscreen mode Exit fullscreen mode

5. Identity Operators

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

Operator Meaning
is Returns True if both variables refer to the same object.
is not Returns True if both variables refer to different objects.

Example:

a = [10, 20]
b = a
c = [10, 20]

print(a is b)  
print(a is c)  
print(a == c)  
Enter fullscreen mode Exit fullscreen mode
True
False
True
Enter fullscreen mode Exit fullscreen mode

6. Membership Operators

Used to check whether a value exists in a sequence such as a string, list or tuple.

Operator Meaning
in Checks whether a value exists in a sequence.
not in Checks whether a value does not exist in a sequence.

Example:

fruits = ["apple", "banana", "orange"]

print("apple" in fruits)       
print("grapes" not in fruits)  
Enter fullscreen mode Exit fullscreen mode
True
True
Enter fullscreen mode Exit fullscreen mode

7. Bitwise Operators

Used to perform operations on the binary (bit-level) representation of integers.

Operator Name
& AND
^ XOR
~ NOT
<< Left Shift
>> Right Shift

Example:

a = 5   # 0101
b = 3   # 0011

print(a & b)   
print(a | b)   
print(a ^ b)  
print(a << 1)  
print(a >> 1) 
Enter fullscreen mode Exit fullscreen mode
1
7
6
10
2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)