Operators in Python
Operators
Operators are used to performing some operation between some values of any data types. We have seven different types of Operators in Python and can perform any operation but are differentiated according to their usage. Like when we add two numbers we use addition operators between those numbers which can be int, or float, or can be of another data type.
Arithmetic Operators in Python
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, division, etc. Arithmetic operators can be set in between operands. In mathematical operations elements on which operation is supposed to be done are called operand and the type of operation is performed by the operator used between two operands.
Operator | Name of Operator | Example |
---|---|---|
+ | Addition | Prep + Insta |
– | Subtraction | Prep – Insta |
* | Multiplication | Prep * Insta |
/ | Division | Prep / Insta |
% | Modulus | Prep % Insta |
** | Exponentiation | Prep ** Insta |
// | Floor Division | Prep // Insta |
a = 5 b = 9 print(a+b) #Addition print(a-b) #Subtraction print(a*b) #Multiplication print(a/b) #Division print(a%b) #Modulus print(a**b) #Exponentiation print(a//b) #Floor Division
Output: 14 -4 45 0.55555556 5 1953125 0
Assignment Operators in Python
The assignment operator is used to assigning values to the variable in many different ways, we can assign values by incrementing, decrementing, or by performing any other operation and then assigning the value to the variable. We have 13 assignment operators to assign values to the variable.
Operator | Also be used as | Example |
---|---|---|
= | a = 5 | a = 5 |
+= | a = a+5 | a+=5 |
-= | a = a-5 | a-=5 |
*= | a = a * 5 | a*=5 |
/= | a = a/5 | a/=5 |
%= | a = a%5 | a%=5 |
//= | a = a//5 | a//=5 |
**= | a = a **5 | a**=5 |
&= | a = a&5 | a&=5 |
|= | a = a|5 | a|=5 |
^= | a = a^5 | a^=5 |
>>= | a = a>>5 | a>>=5 |
<<= | a = a<<5 | a<<=5 |
a = 5 b = a #Assign Operator print(b) b += a #Add and assign operator print(b) b -= a #Subtract and assign operator print(b) b %= a #Modulus and assign operator print(b) b >>= a # Bitwise right shift operator print(b)
Output: 5 10 5 0 0
Comparison Operators in Python
A comparison operator is used to comparing two or more variables or constants having some values. We can compare different variables or constants in six different ways that include, comparing if two values are equal, or if two values are not equal, and many other ways.
Operator | Name of Operator | Example |
---|---|---|
== | Equal | a == b |
!= | Not equal | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
a = 5 b = 10 print(b == a) #Equal operator print(b != a) #Not equal operator print(b > a) #Greater than operator print(b < a) #Less than operator print(b >= a) #Greater than or equal to operator print(b <= a) #Less than or equal to operator
Output: False True True False True False
Logical Operators in Python
The logical operator is used to perform some logical operations or combine conditional statements. Mainly logical operators are used in control statements like if, else, and elif where we can check more conditions together just by using these operators. Logical operators always return TRUE or FALSE values.
Operator | About | Example |
---|---|---|
and | TRUE if both are true | a > 5 and a > 10 |
or | TRUE if any one is true | a > 5 or a > 10 |
not | Converts TRUE to FALSE | not(TRUE) |
a = True b = False print(a and b) #AND operator print(a or b) #OR operator print(not a) #NOT operator
Output: False True False
Identity Operators in Python
Identity operators are used in Python programing language to check if the two values, variables, or constants are not only the same but also have the same memory location. These types of operators are used to compare and check the exact match of the values between two objects.
Operator | About | Example |
---|---|---|
is | Returns TRUE if a and b both are same | a is b |
is not | Return TRUE if a and b both are not same | a is not b |
a = 5 b = 9 print(a is b) #is operator print(a is not b) #is not operator
Output: False True
Membership Operators in Python
Membership operatorsin python are used for checking if the iterator or the given value is present in the sequence or not. These types of operators always return values in a TRUE or FALSE format. If the value is found in the sequence then the operator will return TRUE else the operator will return FALSE.
Operator | About | Example |
---|---|---|
in | Returns TRUE if the iterator is present in sequence | a in b |
not in | Returns TRUE if the iterator is not present in sequence | a not in b |
a = 5 b = 9 list = [5,10,15,20] print(a in list) #in operator print(a not in list) #not in operator
Output: True False
Bitwise Operators in Python
These operators are used to compare the binary numbers. We can perform different operations on binary numbers such as and, or, xor, not, left shift, and right shift. Sometimes these operators are also called bitwise-or, bitwise-and, and bitwise-xor and so on. Shift operators are used to shifting the values of the binary number to left or right.
Operator | Name | About |
---|---|---|
& | AND | Return 1 if both are TRUE or 1 |
| | OR | Return 1 if one of them are TRUE or 1 |
^ | XOR | Return 1 if only one is TRUE or 1 |
~ | NOT | Inverse |
<< | Left shift | Shifting bits to the left |
>> | Right shift | Shifting bits to the right |
a = 5 b = 9 print(a & b) #Bitwise AND operator print(a | b) #Bitwise OR operator print(a ^ b) #Bitwise XOR operator print(~a) #Bitwise NOT operator print(a << b) #Bitwise Left Shift operator print(a >> b) #Bitwise Right Shift operator
Output: 1 13 12 -6 2560 0
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment