7 python Operators that you Must Know

Python language operators are must for python programmer. Python language is an interpreted, object-oriented and high-level programming language very easy to learn for new python beginners.

In Python, operators are special symbols that perform specific operations on one or more operands (values or variables). Some common types of operators in Python include:

Arithmetic operators

These operators perform basic arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/).

Examples of  Arithmetic operators

x = 5
y = 2
z = x + y # z is 7
z = x – y # z is 3
z = x * y # z is 10
z = x / y # z is 2.5

Assignment operators

These operators assign a value to a variable. The most basic assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left.

Example of  Assignment operator

a = 5
b = 10
c = a + b # c is 15

Comparison operators

These operators compare two values and return a Boolean value indicating whether the comparison is True or False. Some common comparison operators include equal to (==), not equal to (!=), greater than (>), and less than (<).

Example of Comparison operators

d = 5
e = 10
f = 5
print(d == e) # False
print(d != e) # True
print(d > e) # False
print(d < e) # True
print(d == f) # True

Logical operators

These operators perform logical operations, such as AND (&&) and OR (||). They are often used in conjunction with comparison operators to create more complex conditions.

Example of Logical operators

g = True
h = False
print(g and h) # False
print(g or h) # True

Membership operators

These operators test for membership in a sequence, such as a list or a string. The membership operators are “in” and “not in”.

Example of Membership operator

i = [1, 2, 3, 4, 5]
print(3 in i) # True
print(6 in i) # False

Identity operators

These operators compare the memory addresses of two objects to determine if they are the same object. The identity operators are “is” and “is not”.

Example of Identity operator

j = [1, 2, 3]
k = [1, 2, 3]
l = j
print(j is k) # False
print(j is l) # True

bitwise operators

In Python, bitwise operators are used to perform operations on the individual bits of an integer value.

# Bitwise AND (&)
x = 0b1010 # 10 in binary
y = 0b1100 # 12 in binary
z = x & y # 8 in binary, or 8 in decimal
print(z) # 8

# Bitwise OR (|)
x = 0b1010 # 10 in binary
y = 0b1100 # 12 in binary
z = x | y # 14 in binary, or 14 in decimal
print(z) # 14

# Bitwise XOR (^)
x = 0b1010 # 10 in binary
y = 0b1100 # 12 in binary
z = x ^ y # 6 in binary, or 6 in decimal
print(z) # 6

# Bitwise NOT (~)
x = 0b1010 # 10 in binary
y = ~x # -11 in binary, or -11 in decimal
print(y) # -11

# Bitwise left shift (<<)
x = 0b1010 # 10 in binary
y = x << 2 # 40 in binary, or 40 in decimal
print(y) # 40

# Bitwise right shift (>>)
x = 0b1010 # 10 in binary
y = x >> 2 # 2 in binary, or 2 in decimal
print(y) # 2

Bitwise operators are often used in Python for low-level operations, such as setting or testing individual bits in a value or masking bits .

 

 

Watch this Short video to learn about the  python operators for a super quick reference.

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.