Operators in Python

Operators in Python

Operators

Operators are used to performing some operation between some values of any data types. We have seven different types of Operators in Python and can perform any operation but are differentiated according to their usage. Like when we add two numbers we use addition operators between those numbers which can be int, or float, or can be of another data type.

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, division, etc. Arithmetic operators can be set in between operands. In mathematical operations elements on which operation is supposed to be done are called operand and the type of operation is performed by the operator used between two operands.

OperatorName of OperatorExample
+AdditionPrep + Insta
SubtractionPrep – Insta
*MultiplicationPrep * Insta
/DivisionPrep / Insta
%ModulusPrep % Insta
**ExponentiationPrep ** Insta
//Floor DivisionPrep // Insta

The assignment operator is used to assigning values to the variable in many different ways, we can assign values by incrementing, decrementing, or by performing any other operation and then assigning the value to the variable. We have 13 assignment operators to assign values to the variable.

 

OperatorAlso be used asExample
=a = 5a = 5
+=a = a+5a+=5
-=a = a-5a-=5
*=a = a * 5a*=5
/=a = a/5a/=5
%=a = a%5a%=5
//=a = a//5a//=5
**=a = a **5a**=5
&=a = a&5a&=5
|=a = a|5a|=5
^=a = a^5a^=5
>>=a = a>>5a>>=5
<<=a = a<<5a<<=5

 

A comparison operator is used to comparing two or more variables or constants having some values. We can compare different variables or constants in six different ways that include, comparing if two values are equal, or if two values are not equal, and many other ways.

OperatorName of OperatorExample
==Equala == b
!=Not equala != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

 

The logical operator is used to perform some logical operations or combine conditional statements. Mainly logical operators are used in control statements like if, else, and elif where we can check more conditions together just by using these operators. Logical operators always return TRUE or FALSE values.

OperatorAboutExample
andTRUE if both are truea > 5 and a > 10
orTRUE if any one is truea > 5 or a > 10
notConverts TRUE to FALSEnot(TRUE)

Identity operators are used in Python programing language to check if the two values, variables, or constants are not only the same but also have the same memory location. These types of operators are used to compare and check the exact match of the values between two objects.

OperatorAboutExample
isReturns TRUE if a and b both are samea is b
is notReturn TRUE if a and b both are not samea is not b

Membership operators are used for checking if the iterator or the given value is present in the sequence or not. These types of operators always return values in a TRUE or FALSE format. If the value is found in the sequence then the operator will return TRUE else the operator will return FALSE.

OperatorAboutExample
inReturns TRUE if the iterator is present in sequencea in b
not inReturns TRUE if the iterator is not present in sequencea not in b

These operators are used to compare the binary numbers. We can perform different operations on binary numbers such as and, or, xor, not, left shift, and right shift. Sometimes these operators are also called bitwise-or, bitwise-and, and bitwise-xor and so on. Shift operators are used to shifting the values of the binary number to left or right.

OperatorNameAbout
&ANDReturn 1 if both are TRUE or 1
|ORReturn 1 if one of them are TRUE or 1
^XORReturn 1 if only one is TRUE or 1
~NOTInverse
<<Left shiftShifting bits to the left
>>Right shiftShifting bits to the right