Operators are symbols in Python that carry out a specific computation, or operation. In python, there are various types of operators, and they are used to write the expression or any condition. Python Operators
Types of Operators:
- Arithmetic Operators
- Assignment Operators
- Comparison / Relational Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
Arithmetic Operators:
The arithmetic operators in Python are used to perform math operations, such as addition, subtraction, multiplication, and division. Python also offers a number of libraries that enable you to perform more complex math tasks.
Here is a list of the arithmetic operators in Python:
Operator | Description | Syntax |
---|---|---|
+ | Addition: adds two operands | x + y |
– | Subtraction: subtracts two operands | x – y |
* | Multiplication: multiplies two operands | x * y |
/ | Division (float): divides the first operand by the second | x / y |
// | Division (floor): divides the first operand by the second | x // y |
% | Modulus: returns the remainder when the first operand is divided by the second | x % y |
** | Power: Returns first raised to power second | x ** y |
Example:
a = 6
b = 4
c = a + b #addition
print(c)
c = a - b #Subtraction
print(c)
c = a * b #Multiplication
print(c)
c = a / b #Division
print(c)
c = a % b #Modulus
print(c)
a = 2
b = 3
c = a**b #Power
print(c)
a = 10
b = 5
c = a//b #Division floor
print(c)
10
2
24
1.5
2
8
2
Assignment Operators:
Assignment operators are operators that are used to assign a value to a variable. The most basic assignment operator is the equal sign =
, which is used to assign a value to a variable.
Here is a list of the assignment operators in Python:
Operator | Description | Syntax |
---|---|---|
= | Assign value of right side of expression to left side operand | x = y + z |
+= | Add AND: Add right-side operand with left side operand and then assign to left operand | a+=b a=a+b |
-= | Subtract AND: Subtract right operand from left operand and then assign to left operand | a-=b a=a-b |
*= | Multiply AND: Multiply right operand with left operand and then assign to left operand | a*=b a=a*b |
/= | Divide AND: Divide left operand with right operand and then assign to left operand | a/=b a=a/b |
%= | Modulus AND: Takes modulus using left and right operands and assign the result to left operand | a%=b a=a%b |
//= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand | a//=b a=a//b |
**= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand | a**=b a=a**b |
&= | Performs Bitwise AND on operands and assign value to left operand | a&=b a=a&b |
|= | Performs Bitwise OR on operands and assign value to left operand | a|=b a=a|b |
^= | Performs Bitwise xOR on operands and assign value to left operand | a^=b a=a^b |
>>= | Performs Bitwise right shift on operands and assign value to left operand | a>>=b a=a>>b |
<<= | Performs Bitwise left shift on operands and assign value to left operand | a <<= b a= a << b |
Example:
a = 5
# 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)
5
10
5
25
Comparison / Relational Operators:
Comparison operators are used to comparing the values of two operands and determine whether a certain condition is met.
Here is a list of the Comparison operators in Python:
Operator | Description | Syntax |
---|---|---|
> | Greater than: True if the left operand is greater than the right | x > y |
< | Less than: True if the left operand is less than the right | x < y |
== | Equal to: True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to True if the left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to True if the left operand is less than or equal to the right | x <= y |
is | x is the same as y | x is y |
is not | x is not the same as y | x is not y |
Example:
a = 1
b = 2
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) # a >= b is False
print(a <= b) # a <= b is True
False
True
False
True
False
True
Logical Operators:
Logical operators are used on conditional statements (either True or False). There are three logical operators.
Here is a list of the Logical operators in Python:
Operator | Description | Syntax |
---|---|---|
and | Logical AND: True if both the operands are true | x and y |
or | Logical OR: True if either of the operands is true | x or y |
not | Logical NOT: True if the operand is false | not x |
Identity Operators:
Identity operators are is
and is not
, which are used to determine if two objects are the same object in memory. These operators compare the memory addresses of the objects, rather than their values.
Here is a list of the Identity operators in Python:
Operator | Description |
---|---|
is | True if the operands are identical |
is not | True if the operands are not identical |
Example of is Operator:
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is y) # False, because x and y are different objects in memory
print(x is z) # True, because x and z refer to the same object in memory
False
True
Example of is not Operator:
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is not y) # True, because x and y are different objects in memory
print(x is not z) # False, because x and z refer to the same object in memory
True
False
Bitwise Operators:
Bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in decimal format.
Here is a list of the Bitwise operators in Python:
Operator | Description | Syntax |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
~ | Bitwise NOT | ~x |
^ | Bitwise XOR | x ^ y |
>> | Bitwise right shift | x>> |
<< | Bitwise left shift | x<< |
Membership Operators:
Membership operator in Python is an operator that checks the membership of a value or variable. Membership operator is used to check whether a value, variable, or element exists in the given sequences like string, tuple, list, etc., or not.
Here is a list of the Membership operators in Python:
Operator | Description |
---|---|
in | True if value is found in the sequence |
not in | True if value is not found in the sequence |
- Operators are symbols in Python that carry out a specific computation, or operation.
- Arithmetic operators in Python are used to perform math operations, such as addition, subtraction, multiplication, and division.
- Assignment operators are operators that are used to assign a value to a variable.
- Logical operators are used on conditional statements (either True or False). There are three logical operators.
- Identity operators are
is
andis not
, which are used to determine if two objects are the same object in memory. - Bitwise operators are used to perform bitwise calculations on integers.
- Membership operator in Python is an operator that checks the membership of a value or variable.