Python is one of the most popular languages out there due to the vast community and advance use of it. From web development to artificial intelligence Python is everywhere. As like other programming languages, Python also has many operators.
What are the Operators? | Python Operators
Operators are the special symbols that operate on the operands to show some output. The Output received by applying Operators can be anything; it could be a Float, Integer, None, Boolean, String or any other data type. The return value or the output depends upon the operands and operator.
As we discussed that python has different operators, here are the types of Python Operators:
- Arithmetic operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership operators
- Bitwise Operators
Arithmetic operators mainly contain the basic Math symbols. The arithmetic operator performs on two variables and returns a value. Mostly the variables on which Arithmetic operators apply are Float and Integers. But some Arithmetic’s Operators such as “+” and “*” works on strings and list too.
The output of the Arithmetic’s operators depends upon the type of Operator applied and data type of operands.
|Arithmetic’s Operators||Operators’ Name||Example||Discerption|
|+||Addition||a + b||Addition operators Add Two Operands and return a value. E.g. print(2+3) Output: 5|
|–||Subtraction||a – b||Subtraction Operator returns the difference between the two operands. E.g. print(55-30) Output: 25|
|*||Multiplication||a * b||Multiply Operators return the multiply of two numbers. E.g. print(5.5*6) Output: 33.0|
|/||Division||a / b||Divide operators divide a by b and return a Float value e.g. print(55 / 5) Output: 11.0|
|%||Modulus||a % b||Modulus operators return the remainder of the value a when it divided by b. e.g. print(5 % 4) Output: 1|
|**||Exponential||a ** b||Exponential operator raises the power of value a by b. e.g. print(3**2) Output: 9|
|//||Floor Division||a // b||Floor Division divides a by b and return the result but in the integer form. print(5// 2) Output: 2|
Assignment operators assign the new value to the variable. The new value assigned to the variable could be any data type such as object, class, integer, float, list etc. When we call the assigned variable it returns the assigned value of the variable.
The output of the Assignment operators depends upon the data type assigned to the Variable. When we assign any value to the variable it does not return anything instead it stores the assigned value.
|Assignment Operators||Operators’ Name||Example||Discerption|
|=||Assign||a = b||It will assign the value of b to a. e.g. a=5 print(a) Output: 5|
|+=||ADD and Assign||a += b||It can also be written as a=a+3. This will add 3 in a and assigned new value to an a=3. a+=3 print(a) Output: 6|
|-=||Subtract and Assign||a -= b||It will subtract b from a, and assigned the new value back to a. e.g. a=4 a-=2 print(a) Output: 2|
|*=||Multiply and Assign||a*=b||It will multiply b to a, and assign the new value to a. e.g. a=3 a*=4 print(a) Output: 12|
|/=||Divide and Assign||a/=b||It will divide a by b and assigned the result to a. e.g. a = 3 a/=2 print(a) Output: 1.5|
|%=||Assign Reminder||a%=b||It will divide a by b and assigned the reminder to a. e.g. a=5 a%=3 print(a) Output: 2|
|//=||Assign Floor Division||a//=b||It will divide a by b and assigned the result to a but in integer form a=3 a//=2 print(a) Output: 1|
|**=||Assign Exponential||a**=b||It will raise the power of a by b and assigned it back to a. a=2 a**=3 print(a) Output: 8|
Comparison operators compare two value and return the output in the form of Boolean. If the comparison goes successfully it returns ‘True’ otherwise ‘False’.
The Comparison operators always return a Boolean value (True or False) depends upon the Operators and Operands.
|Comparison Operators||Operators’ Name||Example||Discerption|
|==||Equal to||a == b||It will compare that whether a and b are equal. e.g. 5==5 Output: True 5==6 Output: False|
|!=||Not equal||a != b||If a and b are not equal it will return true else false. e.g. 5 != 5 Output: False 5 != 6 Output: True|
|>||Greater Than||a > b||If a is greater than b it will return True else false. e.g. 5 > 4 Output: True 5 > 5 Output: False|
|<||Less than||a < b||It will compare a with b, return True if a is smaller than b else False. e.g. 5 < 6 Output: True 6 < 5 Output: False|
|>=||Greater than or equal to||a >= b||It will compare a with b and return True when a is greater or equal to b else it will return False. e.g. 4 >= 4 Output: True 7 >= 8 Output: False 74 >= 50 Output: True|
|<=||Less than or equal to||a <= b||It will compare a with b and return True when a is less than or equal to b else it will return False. e.g. 4 <= 4 Output: True 7 <= 8 Output: True 74 <= 50 Output: False|
Logic operators used to compare two statements and return a Boolean value True or False depends upon the statement.
There are three types of logical operators
|Logical Operators||Operators’ Name||Example||Discerption|
|And||and||x>y and y>z||It will return true if the statement on its left and right both are true else it will return false. e.g. 4 > 5 and 4 < 6 Output: False 4 > 3 and 2==2 Output: True|
|Or||or||a == b or a>b||It will return True if either one of the statement is true if both the statement is false it returns False. e.g. 55 > 45 or 55 >65 Output: True 56 < 49 or 56 < 40 Output: False|
|not||not||not(a > b)||It will return True if the state is False or it returns false if the statement is True. E.g. not(6 ==6) Output: False not( 7 < 4) True|
Identity operators are special kind of comparison operators that does not only compare the value but the data type too.
The Identity operators return the Boolean value i.e. True or false.
|Identity Operators||Operators’ Name||Example||Discerption|
|is||Is Equal to||a is b it can be also written as a === b||It will compare that whether a and b are equal and have a same data type: e.g. 5 is 5 Output: True 5 is 5.0 Here 5 is an integer and 5.0 is a float. Output: False|
|is not||Is Not Equal to||a is not b||If a and b are not equal it will return true else false: e.g. 5 is not 5 Output: False 5 is not 5.0 Output: True|
Membership operator checks whether the given value is present or not. If the value which is asked is not there it will return false else it will return true:
Return type: Boolean (True or False)
|Membership Operators||Operators’ Name||Example||Discerption|
|in||In||a in b||in operator checks whether a in b or not if a lay-in b it will return True else it will return False: e.g. 4 in [2,4,5,7] Output: True 4 in [2,3,4.0,45] Output: True 4 in [2,3,5] Output: False|
|not in||Not in||a not in b||not in operator checks whether a in b or not if a lay-in b it will return False else it will return True: not in invert the value of in e.g. 4 in [2,4,5,7] Output: False 4 in [2,3,4.0,45] Output: False 4 in [2,3,5] Output: True|
In python 0 is treated as False and other than 0 and ‘None’ treated as True. So here are some bitwise operators which are not often used in Python.
|<<||Zero fill left shift|
|>>||Signed right shift|