Hey guys! Welcome to another interesting article. Here, we will discuss Python operators and different types of Python Operators with examples.
Operators in Python
Python operators are special symbols which are used in carrying out arithmetic and logical computation. Operators in python are simply used for performing operations on variables and values. And operands are those values which an operator operates.
Types of Python Operators
Here, we will learn about the different types of Python Operators. There are seven types of Python Operators which are discussed as follows;
1. Arithmetic Operators
Arithmetic Operators are those operators which are used in performing mathematical computations such as addition, multiplication, subtraction, division, etc.
Operator | Discription | Example |
---|---|---|
+ | Addition of two operands | x+y |
- | Substraction of two operands | x-y |
* | Multiplication of two operands | x*y |
/ | Division of one operand by other | x/y |
% | Modulus- remainder when one operand is divided by other | x%y |
// | Floor Division- Divides one operand by other and results into whole number that is adjusted to the left in number line | x//y |
** | Exponent- Operand one raised to power of second | x**y |
Arithmetic Operators Example: Here we will take a simple example of Substraction where we will Subtract two digits ie.,
x = 10y = 7
print(x - y)
3
2. Comparison Operators
Comparison Operators are those operators which are used for comparing values. And according to condition, it returns either True or False.
Operator | Discription | Example |
---|---|---|
< | Less than: True if the operand in left is less than the operand in right | x<y |
> | Greater than: True if the operand in left is greater than the operand in right | x>y |
== | Equal to: True if both left and right operands are equal | x==y |
!= | Not equal to: True if both operands are not equal | x!=y |
<= | Less than or equal to: True if the operand in left is less than or equal to the operand in right | x<=y |
>= | Greater than or equal to: True if the operand in left is greater than or equal to the operand in right | x>=y |
x = 10y = 6
print(x < y)
False
3. Logical Operators
Logical Operators are those operators which are used for combining conditional statements such as Or, And, Not operators.
Operator | Discription | Examples |
---|---|---|
and | True if both first & second operands are true | x and y |
or | True if either first or second operands is true | x or y |
not | True if one of the operands is false | not x |
x = 7
print(x > 3 and x < 10)
True
4. Bitwise Operators
Bitwise Operators are those operators which operates bit by bit and are used for operating binary numbers.
Operator | Discription | Example |
---|---|---|
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
~ | Bitwise NOT | ~x |
^ | Bitwise XOR | x^y |
>> | Bitwise right shift | x>> |
<< | Bitwise left shift | x<< |
a = 20b = 8
print("a | b =", a | b)
a | b = 28
5. Identity Operators
Identity operators are those operators that are used to check whether the two values are located in the memory at same part or not. [is not] and [is] are both identity operators.
Operator | Discription | Examples |
---|---|---|
is | True if both operands are identical | x is True |
is not | True if both operands are not identical | x is not True |
x = ["john", "Daniel"]y = ["john", "Daniel"]
z = x
print(x is z)
print(x is y)
print(x == y)
TrueFalse
True
6. Membership Operators
Membership Operators are those operators that are used in testing that whether a variable or value is in sequence or not. [not in] and [in] are both membership operators.
Operator | Discription | Examples |
---|---|---|
in | True if the value is in sequence | 5 in x |
not in | True if value is not in sequence | 5 not in x |
x = ["Mango", "banana"]
print("banana" in x)
True
7. Assignment Operators
Assignment Operators are those operators which are used in assigning values to the variables.
Operator | Discription | Examples |
---|---|---|
= | Assign value of right side expression to left side operand | x=y+z |
+= | Add AND- It adds the operand of right side with left side and then assign to the left side operand | x+=y x=x+y |
-= | Subtract AND- Subtract the operand of right side from left side and then assign to the left side operand | x-=y x=x-y |
*= | Multiply AND- Multiplies the operand of right side with the left side operand and then assign to the left side operand | x*=y x=x*y |
/= | Divide AND- Divide the operand of left side with the right side operand and then assign to the left side operand | x/=y x=x/y |
**= | Exponent AND- Calculate the exponent value and then assign to the left side operand | x**=y x=x**y |
//= | Divide (Floor) AND- Divide the operand of left side with the right side operand and then assign to the value to the left side operand | x//=y x=x//y |
%= | Modulus AND- First take the modulus using the operand of left and right side and then assign the value to the left side operand | x%=y x=x%y |
x = 5x += 3
print(x)
8
Conclusion
Above we have discussed the Python operators and different types of Python Operators with examples. Python operators are special symbols which are used in carrying out arithmetic and logical computation. Operators in python are simply used for performing operations on variables and values. I hope this information is helpful to you all.