Python Operators
Hello Learners… In this session, you’ll learn about various type of operators with syntax and suitable examples in Python
What are operators in python?
Operators are used to perform operations on variables and values.
The value that the operator operates on is called the operand.
Python divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator | Meaning | Example |
+ | Add two operands or unary plus | x + y+ 2 |
– | Subtract right operand from the left or unary minus | x – y- 2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division – division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent – left operand raised to the power of right | x**y (x to the power y) |
Example: Arithmetic operators in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | x = 15 y = 4 # Output: x + y = 19 print ( 'x + y =' ,x + y) # Output: x - y = 11 print ( 'x - y =' ,x - y) # Output: x * y = 60 print ( 'x * y =' ,x * y) # Output: x / y = 3.75 print ( 'x / y =' ,x / y) # Output: x // y = 3 print ( 'x // y =' ,x / / y) # Output: x ** y = 50625 print ( 'x ** y =' ,x * * y) |
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator | Example | Equivalent to |
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Python Assignment Operator
Python Comparison Operators
Comparison operators are used to compare two values:
Operator | Meaning | Example |
> | Greater than – True if left operand is greater than the right | x > y |
< | Less than – True if 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 left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
Python Comparison Operators
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
Example: Logical Operators in Python
1 2 3 4 5 6 7 8 | x = True y = False print ( 'x and y is' ,x and y) print ( 'x or y is' ,x or y) print ( 'not x is' , not x) |
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
Example : Identity operators in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [ 1 , 2 , 3 ] y3 = [ 1 , 2 , 3 ] # Output: False print (x1 is not y1) # Output: True print (x2 is y2) # Output: False print (x3 is y3) |
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator | Meaning | Example |
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x |
Example: Membership operators in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | x = 'Hello world' y = { 1 : 'a' , 2 : 'b' } # Output: True print ( 'H' in x) # Output: True print ( 'hello' not in x) # Output: True print ( 1 in y) # Output: False print ( 'a' in y) |
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers.
They operate bit by bit, hence the name.
For example, 2 is 10
in binary and 7 is 111
.
In the table below: Let x = 10 (0000 1010
in binary) and y = 4 (0000 0100
in binary).
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | x & y = 0 (0000 0000 ) |
| | Bitwise OR | x | y = 14 (0000 1110 ) |
~ | Bitwise NOT | ~x = -11 (1111 0101 ) |
^ | Bitwise XOR | x ^ y = 14 (0000 1110 ) |
>> | Bitwise right shift | x >> 2 = 2 (0000 0010 ) |
<< | Bitwise left shift | x << 2 = 40 (0010 1000 ) |
No comments:
Post a Comment