Python language supports the following types of operators:-
- Arthmetic Operators
- Comparison (Relational) Operators
- Assignment Operaators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Assume: a=10, b=20
Operator | Description | Example |
---|---|---|
(+) Addition | Add values on either side of the operator | a+b = 30 |
(-) Subtraction | Substracts right hand operand fro left hand operand | a-b = -10 |
(*) Multipliction | Multiplies values on either side of the operator | a*b = 200 |
(/) Division | Divides left hand operands by right operand | a/b = 2 |
(%) Modulus | Divides left operands by right hand operand and returns the remainder | b%a = 0 |
(**) Exponnent/Power | Performs exponential calculation on operator | a**b = 10 to the power of 20 = 1020 |
(//) Floor | The division of operands where the result is quotient in which the digits after the decimal points are removed | 9//2 = 4 9.0 // 2.0 = 4.0 9.4523//2.4512 = 3.0 |
These operators compare the values on either sides of them and decide the relation among them. They are also called Relation Operators.
Assume a = 10, b = 20
Operator | Description | Example |
---|---|---|
== | If the values of two operands are qual then the condition becomes true | a==b # false |
!= | If the values of two operands are not equal then the condition becomes true | a!=b # true |
<> | If values of two operands are not equal then the condition becomes true | a<>b # true |
> | If the values of the left operand is greater than the values right operand, then condition becomes true | a>b # false |
< | If the values of the left operand is less than the value of right operand, then condition becomes true | a < b # true |
>= | If the value of left operand is greater than or qual to the value of right operand, then condition becomes true | a>=b # false |
<= | If the value of left operand is less than or qual to the value of right operand, then condition becomes true | a<=b # true |
An assignment operator is the operator used to assign a new value a variable
Assume a=10, b=20
Operator | Description | Example |
---|---|---|
= | Assign values from right side operands to left side operand | c=a+b # assigns value of a+b into c |
+= | If adds right operand to the left operand and assign the result to left operand | a+=b # which is equivalent to a = a+b |
-= | It substracts right operand from the left operand and assign the result to left operand | a-=b # which is equivalent to a = a-b |
*= | It multiplies right operands with the left operand and assign the result to left operand | a*=b # which is equivalent to a = a*b |
/= | It divides the left operand with the right operand and assign the result to left operand | a/=b # which is equivalent to a = a/b |
%= | It takes modulus using two operands and assign the resilt to left operand | a%=b # which is equivalent to a = a%b |
**= | It performs exponential(power) calculation on operands and assign value to left operand | a**=b # which is equivalent to a = a**b |
//= | It performs floor division on operators and assign value to left operand | a**=b # which is equivalent to a = a**b |
Logical operators are typically used with Boolean values
Assume a = True, b = False
Operator | Description | Example |
---|---|---|
and (Logical AND) | If both the operands are true then condition becomes true | a and b # false |
or (Logical OR) | If any of the two operands are true then condition becomes true | a or b # true |
not (Logical NOT) | Used to reverse the logical state of its operand | !a # false |
Y (output) : A.B (dot(.) represent multiplication) Above line can also be written as Y = A and B
A | B | Y = A and B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Y (output) : A+B (plus(+) represent addition) Above line can also be written as Y = A or B
A | B | Y = A + B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Y (output) : !A (! used to give opposite of value; if TRUE then it will false) Means if A = 1, then Y = !A = 0
A | Y = !A |
---|---|
0 | 1 |
1 | 0 |
Bitwise operator works on bits and performs bit-by-bit operation
Assume a = 60, b = 13
Operator | Description | Example |
---|---|---|
& (Binary AND) | Operators copies a bit to the result if it exists in both operands | a = 0b0011 1100 b = 0b0000 1101 a & b = 0b0000 1100 a & b = 12 |
| (Binary OR) | It copies a bit, if it exists in either operand | a = 0b0011 1100 b = 0b0000 1101 a | b = 0b0011 1101 a | b = 61 |
^ (Binary XOR) | It copies the bit, if it is set in one operand but not both | a = 0b0011 1100 b = 0b0000 1101 a ^b = 0b0011 0001 a ^ b = 49 |
~ (Binary one's complement) | It is unary and has the effect of 'flipping bits' | a = 0b0011 1100 ~a = 1100 0011 ~a = -61 |
<< (Binary left shift) | The left operands value is moved left by the left number of bit specified by the right operand | a = 0b0011 1100 b = a << 2 b = 240 b = 0b1111 0000 |
>> (Binary right shift) | The right operands value is moved right by the right number of bit specified by the right operand | a = 0b0011 1100 b = a >> 2 b = 15 b = 0b0000 1111 |
Membership operators test for membership in a sequence such as strings, list or tuples.
Operator | Description | Example |
---|---|---|
in | Evaluates to True, if it finds a variable in the specified sequence and False otherwise | (x in y) is True when x is a member of sequence y. Example: x = 2 y = [1, 2, 3] print(x in y) # True |
not in | Evaluates to True; if it does not find a variable specified sequence and False otherwise | (x not in y) is True when x is not a member of sequence y. Example: x = 4 y = [1, 2, 3] print(x not in y) # True |
Identity operators compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Evaluates to True, if the variables on either side of the operator point to the same object and False otherwise | (x is y) is True, when id(x) equals to id(y) Example: x = 1 y = 1 print(x is y) # True |
is not | Evaluates to False, if the variables on either side of the operator point to the same object and True otherwise | (x is not y) is True, when id(x) is not equals to id(y) Example: x = 1 y = 1 print(x is not y) # False |
PEMDAS: Power Exponent Multiplication Divide Addition Substraction The following table list all the operators from highest precedence to the lowest:
Operator | Description |
---|---|
** | Exponentation (raise to the power) |
~, +, - | Complement, unary plus and minus |
*, /, %, // | Multiply, Divide, Modulo and Floor Division |
+, - | Addition and Substraction |
>>, << | Right and Left bitwise shift |
& | Bitwise AND |
^, | | Bitwise exclusive 'OR' and 'OR' |
<=, <>, >= | Comparison operators |
<, >, ==, != | Equality operators |
=, %=, /=, //=, -=, +=, *=, **= | Assignment operator |
is, is not, in, not in, not, or, and | Membership operators and Logical operators |
Three ways by which you can format a variable.
- Using format() function and {}
Example:
a = 10, b = 20
print("This value of a: {} and b: {}".format(a, b))
# output: The value of a: 10 and b: 20
- Using format() function with indexes and {}
Example:
a = 10, b = 20
print("This value of b: {1} and a: {0}".format(a, b))
# output: The value of b: 20 and a: 10
- Using Shorthand of format() function[f]
Example:
a = 10, b = 20
print(f"The value of a: {a} and b: {b}")
# output: The value of a: 10 and b: 20