Python Operators are the constructs that can manipulate the value of operands. In Python programming, operators are used to performing operations on values and variables. For logical and arithmetical operations, some standard symbols are used.
Here, we will look into different types of Python operators.
- Arithmetic operators
- Logical operators
- Comparison operators
- Bitwise operators
- Assignment operators
- Special operators
Arithmetic Operators
Arithmetic operators are utilized to perform numerical operations like addition, subtraction, multiplication and division.
lets suppose x= 5 , y = 4
Operators | Description/ Syntax | Example | Output |
+ Addition | Adds two operands x + y | =5+4 | =9 |
– Subtraction | Subtracts two operands x – y | =5-4 | =1 |
* Multiplication | Multiplies two operands x * y | =5*4 | =20 |
/ Division | Divides the first operand by the second x / y | =5/4 | =1.25 |
// Floor Division | Divides the first operand by the second x // y | =5//4 | =1 |
% Modulus | Returns the remainder when the first operand is divided by the second x % y | =5%4 | =1 |
** Power/Exponent | Returns first raised to power second x ** y | =5**4 | =625 |
Logical Operators
Logical Operators are the symbols that perform logical operations on variables. The value the operator operates on is known as Operand. The result of logical operations can either be true or false. There are 3 types of logical operators:
- AND
- OR
- NOT
In Python, Logical operators used on conditional statements and they perform Logical AND, Logical OR and Logical NOT operations.
Here is the small description related to these operators,
Operators | 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 | |
AND Operator:
This logical operator holds true value only when both A and B operands are true. Otherwise, it’s false.
Syntax is as given below,
Following are the possible cases:
A | B | Output |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
a = True b = False print('a and b is',a and b)
OR Operator:
Logical or operator returns True if either of the operands is True.
Syntax is as given below,
Following are the possible cases:
A | B | Output |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
a = True b = False print('a or b is', a or b)
NOT Operator:
Logical not operator work with the single value. If the given value is True it return False sand vice-versa.
Syntax is as given below,
Following are the possible cases:
A | Not A |
True | False |
False | True |
a = True print('not a is',not a)
Comparison Operators
The comparison operators, also known as Relational Operator, compare the given values and result in the form relationships between them.
Let two operands are p and q:
Operator | Description | Example |
---|---|---|
> | It can be true only If the value of the left operand is greater than the value of the right operand. | (p > q) not true. |
< | It can be true only If the value of the left operand is less than the value of the right operand. | (p < q) true. |
<> | It can be true only If the values of the two operands are not equal. | (p <> q) true. |
>= | It can be true only If the value of the left operand is greater than or equal to the value of the right operand. | (p>= q) not true. |
!= | It can be true only If the values of the two operands are not equal. | (p!= q) true. |
<= | It can be true only If the value of the left operand is less than or equal to the value of the right operand. | (p <= q) true. |
== | It can be true only If the values of the two operands are equal. | (p == q) not true. |
Bitwise Operators
Bitwise operators act on operands as if they were strings of binary digits. As their name shows, They operate bit by bit. In Python, bitwise operators are used to performing bitwise calculations on integers.
Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise NOT |
^ | Bitwise XOR |
>> | Bitwise right shift |
<< | Bitwise left shift |
Bitwise AND Operator:
This is equivalent to a product of two bits. It returns 1 if both the bits are 1 else 0.
a = 10 #1010 (Binary) b = 5 #0101 (Binary c = 0 c = a & b #1010 & 0101 = 0000 print ("value of c is", c)
Bitwise OR Operator:
The bitwise OR operator (|
) performs logical junction. For each given pair of bits, it returns a one if at least one of them is one. Tt is a combination of a sum and a product of the bit values.
a = 10 #1010 (Binary) b = 5 #0101 (Binary c = 0 c = a | b #1010 | 0101 = 1111 print ("value of c is", c)
Bitwise Not Operator:
This operator returns one’s complement of the given value.
a = 10 #1010 (Binary) c = 0 c = ~a #1010 , -10 print ("value of c is", c )
Bitwise XOR Operator:
This operator returns 1 if one of the bit is 1 and other is 0 else returns false.
a = 10 #1010 (Binary) b = 5 #0101 (Binary c = 0 c = a ^ b #1010 ^ 0101= 1111 print ("value of c is", c )
Bitwise Right Shift:
This shift operator is used to shift the bits of a number right thereby multiplying or dividing the number by two. This can be used when we have to multiply or divide a number by two.
Right Shifts operator , shift the bits of the number to the right and fills 0 on voids left as a result. By dividing the number with some power of two.
a = 10 c = 0 c = a >> 1 #10/2 = 5 print (" value of c is" , c)
Bitwise Left Operator:
Shifts the bits of the number to the left and fills 0 on voids left as a result. By multiplying the number with some power of two.
a = 10 #1010 b = 5 #0101 a << 1 #1010 = 20 b << 2 #0101 = 20 print ("a << 1= ",a << 1) print ("b << 2= ",b << 2)
Assignment Operators:
In Python, Assignment operators are used to assign values to the variables. It allows skipping of steps and, in a way, act as a shortcut. The application of each of them is according to the user’s need in the given context.
Here are the basic Assignment Operators given below,
Operators | Description | Syntax |
= | Assign the value of the right side of expression to the left side operand | c=a+b |
+= | Adds the value given on the right-hand side operator to the left-hand side of the operator. | a+=b a=a+b |
-= | Subtracts the value given on the right-hand side of the operator from the variable on the left-hand side of the operator. | a-=b a=a-b |
*= | Multiplies the variable given on the left-hand side of the operator with the value on the right-hand side of the operator. | a*=b a=a*b |
/= | Divides the variable given on the left-hand side of the operator by the value specified on the right-hand side of the operator. | a/=b a=a/b |
%= | Divides the variable on the left-hand side of the operator by the value given on the right-hand side of the operator, and places the remainder into the variable on the left-hand side. | a%=b a=a%b |
**= | Raises the value of the variable given on the left-hand side of the operator to the power of value given on the right-hand side of the operator. | a**=b a=a**b |
//= | Divides the variable on the left-hand side of the operator by the value given on the right-hand side of the operator, and places the integer part of the result in the variable. | a//=b a=a//b |
&= | Performs the bitwise AND operation on value. | a&=b a=a&b |
|= | Performs the bitwise inclusive-OR operation on value. | a|=b a=a|b |
^= | Performs the bitwise X-OR operation on value. | a^=b a=a^b |
>>= | Shifts the left side operand value by a number on the right side and assigns the result to the left side operand. | a>>=b a=a>>b |
<<= | Shifts the left side operand value by a number on the right side and assigns the result to the left side operand. | a <<= b a= a << b |
Special Operator:
In Python language, there are some Special types of operators such as the Identity operator and the Membership operator. They are described below with examples.
Identity Operator:
Identity operators compare the memory locations of two objects. 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.
Here are two Identity operators explained below.
Operator | Description |
is | x is y, here is results in true if id(x) equals id(y). |
is not | x is not y, here is not results in true if id(x) is not equal to id(y). |
a = 5 b = 5 if ( a is b ): print ("a and b have the same identity") else: print ("a and b do not have the same identity") if ( a is not b ): print ("a and b do not have the same identity") else: print ("a and b have the same identity")
Membership Operator:
in and not in are the Membership Operators, used to test whether a value is in a sequence or not. Such as strings, lists, or tuples.
There are two membership operators explained below ,
Operators | Description |
in | Evaluates true if it finds a variable in the specified sequence. |
not in | Evaluates true if it does not find a variable in the specified sequence. |
a = 5 b = 10 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("a is available in the list") else: print ("a is not available in the list") if ( b not in list ): print ("b is not available in the list") else: print ("b is available in the list")