Python Operators

In this article, we will discuss operands, operators ( Python Operators), types of operators, implementation, and examples of different kinds of Python operators ( ).

Python Operator:

Python operators are special symbols that are used for arithmetic and logical operations. This operation performs on operands from the example we will understand what is operands and what is an operator.

Types of Python Operators:

Arithmetic Operators:

The python arithmetic operators are used mainly with numeric values to perform common mathematical operations.

Example:

# your code goes here

# Examples of Arithmetic Operator
a = 10
b = 20

# Addition of numbers
add = a + b

# Subtraction of numbers
sub = a - b

# Multiplication of number
mul = a * b

# Division(float) of number
div1 = a / b

# Division(floor) of number
div2 = a // b

# Modulo of both number
mod = a % b

# Power
p = a ** b

# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)

Output:

30
-10
200
0.5
0
10
100000000000000000000

Explanation:
In the above python program we have taken variables β€˜a’ and β€˜b’ and signed the values 10 and 20. After this we simply do all the arithmetic operation like addition (which simply add the numbers), subtraction (this operator subtracts one number from another number, multiplication(this python arithmetic operator simply multiply the number ), power(this operator give the power ab) a is base here and b is the exponential power of a.

Assignment Operator:

These python operators are used to assign value to variables.

Example:

# your code goes here

# Examples of Assignment Operators
a = 50

# 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)

# bitwise lishift operator
b <<= a
print(b)

Output:

50
100
50
2500
2814749767106560000

Explanation:
In the above python program we have taken variables β€˜a’ and β€˜b’ and signed the value a =50 and assign b=a, so if print the value of b we got 50 on the output screen .here value of β€˜a’ assigned in b, after that we β€˜b+=a’ which simply mean b=b+a, and similarly b-=a means b=b-a, b=a means b=ba, b<<=a means assigning b =b<< a.

Comparison Operator:

These python operators are used to compare two values.

Example

# your code goes here

# Examples of Relational Operators
a = 23
b = 43

# a > b is False
print(a > b)

# a < b is True
print(a < b)

# a == b is False
print(a == b)

# a != b is True
print(a != b)

# a >= b is False
print(a >= b)

# a <= b is True
print(a <= b)

Output:

False
True
False
True
False
True

Explanation:
In the above python program we have taken variables β€˜a’ and β€˜b’ and signed the values 23 and 43. After this, we simply compare these two values like which is greater, which is lesser, either they are equal or not, or greater equal to, lesser equal to by β€˜>’,’<’,’==’,’>=’,’<=’.

Logical Operator:

These python operators are used to combine conditional statements .it performs Logical AND, Logical OR, and Logical NOT.

Example

# your code goes here

# Examples of Logical Operator
a = True
b = False

# Print a and b is False
print(a and b)

# Print a or b is True
print(a or b)

# Print not a is False
print(not a)

Output:

False
True
False

Explanation:
In the above python program we have taken two boolean variables β€˜a’ and β€˜b’ and simply get the output of the β€˜and’, β€˜or’,’ not’ logical operation.

Identity Operator:

These python operators are used to compare the objects if the objects are equal and they allocate the same location then it returns true and if objects are not equal then it returns false.

Example

# your code goes here

a = 10
b = 20
c = a

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

Output:

True
True

Explanation:
In the above python program we have taken two variables a and b and assigned the values 10 and 20. after this, we have taken c and set c=a. So in output, we observe true for a β€˜is not’ b and a is c give true . β€˜is’ operator check whether the object is equal or not.

Membership Operator:

These python operators are used to test if a sequence is presented in an object or not. if it pre4sent then it returns true and if the sequence is not present then it returns false.

Example

# your code goes here

x=["mango","banana"]


print("mango" in x)

j=["1","2"]

print( "3" not in j)

Output:

True
True

Explanation:
In the above python program we have taken two list names x and j. here we check whether β€œmango” is in the list or not, so we use the β€˜in’ (membership operator) if the element is present in the sequence then it returns true, and β€˜not in’ operator returns false if the element is present and return true if the element is not present.

Bitwise Operator:

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

Example

# your code goes here

# Examples of Bitwise operators
a = 10
b = 4

# Print bitwise AND operation
print(a & b)

# Print bitwise OR operation
print(a | b)

# Print bitwise NOT operation
print(~a)

# print bitwise XOR operation
print(a ^ b)

# print bitwise right shift operation
print(a >> 2)

# print bitwise left shift operation
print(a << 2)

Output:

0
14
-11
14
2
40

Explanation:
In the above python program we have taken variables β€˜a’ and β€˜b’ and signed the values 10 and 4 respectively. So here we have two numbers 10 and 4 .then we use the operator β€˜&’ for and operation,’|’ for or operation, β€˜^’ for xor operation,’>>’ for the right shift, β€˜<<’ for the left shift.

Summary
Python has a variety of operators that allow you to perform operations on variables and values. These include arithmetic operators (e.g. +, -, *, /), comparison operators (e.g. ==, !=, >, <, >=, <=), assignment operators (e.g. =, +=, -=), and logical operators (e.g. and, or, not). Additionally, Python has special operators such as the identity operator (is), membership operator (in), and bitwise operators (e.g. &, |, ~, ^, <<, >>).

Arithmetic operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. Comparison operators compare two values and return a Boolean value (True or False) based on the result. Assignment operators assign values to variables, and logical operators combine multiple conditions to return a single Boolean value.

The identity operator (is) checks if two variables refer to the same object in memory, while the membership operator (in) checks if a value is present in a sequence (such as a list or a string). Bitwise operators perform operations on the bits of a binary number.

Leave a Reply

Your email address will not be published. Required fields are marked *