Python Operators
Hello guys, here we will look
into different types of operators in Python.
Operators in python are special symbols in python that
carry out different types of computation.
We divide these operators differently as per the types
of computation:-
1)
Arithmetic
Operators: - These are used to carry out
mathematical operations like addition,
subtraction and more.
Operator |
Meaning |
+ |
Add two operands or unary plus |
- |
Subtract
right operand from the left or unary minus |
* |
Multiply two operands |
/ |
Divide
left operand by the right one (always results into float) |
% |
Modulus - remainder of the division of left operand by the
right |
// |
Floor
division - division that results into whole number adjusted to the left in
the number line |
** |
Exponent - left operand raised to the power of right |
Example
Output
We can use them where ever we want to in our code according to the need.
2) Comparison Operators: - They are used for comparing values. It either returns True or False according to the condition. It can also be used into so many other programs.
Operator | Meaning |
> | Greater than |
< | Lesser than |
== | Equal to |
!= | Not equal to |
>= | Greater than or equal to |
<= | Lesser than or equal to |
Example
Try out the last two and check the output on your own for better understanding.
3) Logical Operators: -
Operator |
Meaning |
And |
True
if both the operands are true |
Or |
True
if either of the operands is true |
Not |
True
if operand is false |
Output
4)
Bitwise Operators: - Bitwise operators act on operands as
if they were strings of binary digits. They operate bit by bit, hence the name.
Like 2 in binary is 10 and 8 eight in binary is 1000.
Operator |
Meaning |
& |
Bitwise AND |
| |
Bitwise OR |
~ |
Bitwise NOT |
^ |
Bitwise XOR |
>> |
Bitwise right shift |
<< |
Bitwise left shift |
Output
5)
Assignment Operators: -
Assignment operators are used in Python 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 |
Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!