Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Contents
Arithmetic Operators
Arithmetic python operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.
Operator | Description |
+ (Add) | Addition |
– (Subtract) | Subtraction |
/ Division (float) | Divides the first operand by the second |
// Division (floor) | Divides the first operand by the second |
* (Multiply) | Multiplication |
** | Power : Returns first raised to power second |
% (Modulo) | Returns the integer remainder of a division. |
Please read the previous topic for more understanding
Number in Python
Python String
Python String Formatting
Boolean in Python
Please find the below examples for more understanding
x = 36
y = 23
# Addition of numbers
add = x + y
print('Addition : ', add)
# Subtraction of numbers
sub = x - y
print('Subtraction : ', sub)
# Multiplication of number
mul = x * y
print('Multiplication : ', mul)
# Division(float) of number
div1 = x / y
print('Division : ', div1)
# Division(floor) of number
div2 = x // y
print('Division(floor) : ', div2)
# Modulo of both number
mod = x % y
print('Modulo : ', mod)
# Power
pw = x ** y
print('Power :', pw)
Output-
Addition : 59 Subtraction : 13 Multiplication : 828 Division : 1.565217391304348 Division(floor) : 1 Modulo : 13 Power : 623673825204293256669089197883129856
Comparison Operators
Comparison or Relational python operators compare the values. It either returns True or False according to the condition.
== IF the value of two operands are equal then the condition becomes True otherwise false
!= If the values of two operands are not equal then the condition becomes True
> If the value of left operands is greater than right operands then True
< If the value of left operands is less than right operands then True
<= If the value of left operands is greater than or equal to right operands then True
<= If the value of left operands is less than or equal to right operands then True
x = 45
y = 98
print('x = ', x, 'y = ', y)
# x > y is False
print('x > y ', x > y)
# x < y is True
print('x < y ', x < y)
# x == y is False
print('x == y ', x == y)
# x != y is True
print('x != y ', x != y)
# x >= y is False
print('x >= y ', x >= y)
# x <= y is True
print('x <= y ', x <= y)
Output-
x = 45 y = 98 x > y False x < y True x == y False x != y True x >= y False x <= y True
Logical Operators
We can use logical python operators to combine a comparison
and – True if both the operands are true.
or – True if either of the operands is true
not – rue if the operand is false (complements the operand)
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Output-
x and y is False x or y is True not x is False
Bitwise Operators
Bitwise python operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.
Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise NOT |
^ | Bitwise XOR |
>> | Bitwise right shift |
<< | Bitwise left shift |
x = 20
y = 16
print('x = ', x, 'y = ', y)
# Print bitwise AND operation
print('x & y : ', x & y)
# Print bitwise OR operation
print('x | y : ', x | y)
# Print bitwise NOT operation
print('~x : ', ~x)
# print bitwise XOR operation
print('x ^ y : ', x ^ y)
# print bitwise right shift operation
print('x >> 3 : ', x >> 3)
# print bitwise left shift operation
print('x << 3 : ', x << 3)
Output-
x = 20 y = 16 x & y : 16 x | y : 20 ~x : -21 x ^ y : 4 x >> 3 : 2 x << 3 : 160
Identity Python Operators
is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
is: True if the operands are identical
is not: True if the operands are not identical
x = 10
y = x
print(x is not y)
print(x is y)
Output-
False True
Membership Python Operators
in and not in are the membership operators; used to test whether a value or variable is in a sequence.
in: True if value is found in the sequence
not in: True if value is not found in the sequence
x = 3
y = 50
list = [1, 2, 3, 4, 5]
if (x in list):
print("x is present in given list")
else:
print("x is NOT present in given list")
if (y not in list):
print("y is NOT present in given list")
else:
print("y is present in given list")
Output-
x is present in given list y is NOT present in given list
Assignment Python Operators
Assignment operators are used to assign values to the variables.
Operator | Description |
= | Assign value of right side of expression to left side operand |
+= | Add AND: Add right side operand with left side operand and then assign to left operand |
-= | Subtract AND: Subtract right operand from left operand and then assign to left operand |
*= | Multiply AND: Multiply right operand with left operand and then assign to left operand |
/= | Divide AND: Divide left operand with right operand and then assign to left operand |
%= | Modulus AND: Takes modulus using left and right operands and assign result to left operand |
//= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand |
**= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand |
&= | Performs Bitwise AND on operands and assign value to left operand |
|= | Performs Bitwise OR on operands and assign value to left operand |
^= | Performs Bitwise xOR on operands and assign value to left operand |
>>= | Performs Bitwise right shift on operands and assign value to left operand |
<<= | Performs Bitwise left shift on operands and assign value to left operand |