Operators in python is a symbol that perform certain operation on one or more variable or a value.
There are 7 types of operators in python
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
1. Arithmetic operators
Arithmetic operators are used to solve arithmetic problems like Addition(+), Subtraction(-), Multiplication(*), Division(/) and so on.
|Addition||+||a + b|
|Subtraction||–||a – b|
|Multiplication||*||a * b|
|Division||/||a / b|
|Modulus||%||a % b|
|Exponentiation||**||a ** b|
|Floor Division||//||a // b|
You are familiar with Addition, Subtraction, Multiplication and Division. But maybe you are not familiar with remaining 3 arithmetic operators. Modulus, Exponentiation and Floor Division.
So, let me explain
Modulus : This operator will give us the reminder
Exponentiation : Raises the first number to the power of the second.
Floor Division : Division that results into whole number
These are the arithmetic operators in python.
Comparison operators are used to compare two values in python.
|Equal to||=||a = b|
|Not equal to||!=||a != b|
|Greater than||>||a > b|
|Less than||<||a < b|
|Greater than or equal to||>=||a >= b|
|Less than or equal to||<=||a <= b|
These are the comparison operators in python.
In short assignment operators are used to assign values to variable.
|=||a = 10||a =10|
|+=||a += 10||a = a + 10|
|-=||a -= 10||a = a – 10|
|*=||a *= 10||a = a * 10|
|/=||a /= 10||a = a / 10|
|%=||a %= 10||a = a % 10|
|**=||a **= 10||a = a ** 10|
|//=||a //= 10||a = a // 10|
These are the most basic assignment operators in python.
These are conjunctions that we can use to combine more than one condition. There are three logical operators in python. AND, OR and NOT
|and||It returns True if both condition is true||5 > 2 and 3 != 30|
|or||Return True if one of these condition is true||5 > 50 or 8 = 8|
|not||Return true if condition is false||10 >= 30|
We have studied this in our school time. “Logical Gates” same like that.
This will return True if identity of both operands is True. Otherwise it return false. There are 2 identity operators in python
|is||Return True if the operands are identical||a is b|
|is not||Return True if the operands are not identical||a is not b|
Membership Operators are used to testing whether a value or variable is found in a sequence. See in and not in are the membership operators in python.
|in||Return True if value/variable is found in the sequence||a in b|
|not in||Return True if value/variable is not found in the sequence||a in not b|
Bitwise Operators In Python
Bitwise operators act on operands as if they were strings of binary digits In python. Here are some examples NOT, AND, OR, XOR, and so on. Have a look at this table.
|&||Bitwise AND||Sets each bit to 1 if both bits |
||||Bitwise OR||Sets each bit to 1 if one of two bits is 1|
|^||Bitwise XOR||Sets each bit to 1 if only one of two bits is 1|
|~||Bitwise NOT||Reverse all the bits|
|<<||Bitwise right shift||Shift left by pushing zeros in from the right and let the leftmost bits fall off|
|>>||Bitwise left shift||Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off|
This is all about operators in python.
If you have any doubt you can ask me on Instagram
Posts You may Like : Data Types In Python