[02] Python Operators

https://imbooz.com/im-blog-data-science/02-python-operators/

Python Operators:

In Python, operators are symbols or keywords that perform various operations on values or variables. Here are some of the commonly used operators in Python:

Arithmetic Operators:

Relational Operators:

Bitwise Operators: Boolean Operator

Logical Operators:

not

and

or

Assignment Operators

=

Compound Operators:

Membership Operators:

in
not in

Identity Operators:

is
not is

1 Arithmetic operators: These operators perform basic operations like addition, subtraction, multiplication, division, modulo and exponentiation.

Python Code:

a = 10
b = 3

print(a + b) # Output: 13
print(a – b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000

 

2 Comparison operators: These operators are used to compare two values and return a Boolean value (True or False).

Python Code:

a = 10
b = 3

print(a > b) # Output: True
print(a < b) # Output: False
print(a == b) # Output: False
print(a != b) # Output: True
print(a >= b) # Output: True
print(a <= b) # Output: False

Python Operators

3 Logical operators: These operators are used to perform logical operations like AND, OR, and NOT on Boolean values.

Python Code:

a = True
b = False

print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False

 

4 Assignment operators: These operators are used to assign values to variables.

Python Code:

a = 10
b = 3

a += b # same as a = a + b
print(a) # Output: 13

 

5 Membership operators: These operators are used to check whether a value is a member of a sequence or not.

Python Code:

x = [1, 2, 3, 4, 5]

print(3 in x) # Output: True
print(6 not in x) # Output: True

 

6 Identity operators: These operators are used to check if two variables refer to the same object or not.

Python Code:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y) # Output: False
print(x is not y) # Output: True
print(x is z) # Output: True

 

These are some of the commonly used operators in Python. There are more operators in Python like bitwise operators, ternary operators, etc.

Leave a Comment