Python Operators

Python provides Python Operators as most of the languages do. Every programming language has operators. Operators are necessary to work with logics in a program. They tell the compiler or interpreter to perform some mathematical, relational, or logical operations and produce a result. These are special symbols to the language like +, , *, and /. They are like functions which take one or more argument and produces the result.


Python has the following group of operators:


  • Arithmetic operators
  • Comparison operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators


Assume a=10, b=5


OperatorDescriptionExample
+ (Addition)It is used to add two operands.a+b=15
(Subtraction)It is used to subtract the second operand from the first perand.a-b=5
* (Multiplication)Used to multiply two operands.a*b=50
/ (divide)Used to divide first operand by second and results quotient.a/b=2.0
% (reminder)Returns the remainder after dividing first operand by second operand.a%b=0
** (Exponent)Returns the result after calculating the first operand to the power of second operand.a**b=100000
// (Floor division)It returns the floor value of the quotient produced by dividing the two operands.a//b=2

Comparison operators


Comparison operators are simple to understand as they return a boolean value i.e. True or False.


OperatorDescription
==Condition becomes True if two operands are equal
!=Condition becomes True if two operands are not equal
<=Condition becomes True if first operand is less than or equal to second operand
>=Condition becomes True if first operand is greater than or equal to second operand
>Condition becomes True if first operand is greater than second operand
<Condition becomes True if first operand is less than second operand

Assignment Operators


Assignment operators are used to assigning a calculated value from right-hand side expression(after calculation) to the left-hand side operand.


OperatorDescriptionExample
=Simply assigns right side expression to left side operanda=5
+=Adds left operand to right side operand and assigns the value back to left operanda += b is same as a = a+b
-=Decreases left operand value by right operand value and assigns value back to left operanda -= b is same as a = a-b
*=Multiply left operand value with right operand value and assigns the multiplied value back to left operanda *= b is same as a=a*b
%=Divides left operand by right operand and returns remainder which is assigned to left operanda %= b is same as a=a%b
**=Increases left operand to power of right operand and assigns result to left operanda **= b is same as a=a**b
//=It returns floor value as it divides left operand by right operand and assigns result to left operanda //= b is same as a=a//b


Logical Operators


OperatorExplanation
andLet two expressions be a==>True, b==>True then the expression a and b will be True while if anyone from a and b is False then the expression will be False
orLet two expressions be a==>False, b==>False then the expression a and b will be False while if anyone from a and b is True then the expression will be True
notif a is True then not a is False and vice-versa

Bitwise Operators


The bitwise operators are used to perform binary operations on operands.


OperatorDescription
& (binary and)If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied
| (binary or)The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1
^ (binary xor)The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0
~ (negation)It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa
<< (left shift)The left operand value is moved left by the number of bits present in the right operand
>> (right shift)The left operand is moved right by the number of bits present in the right operand

Membership Operators


Using Membership Operators, you can check whether a value is present in a Python data structure or not. It returns True if the value is present in the Python data structure else returns False.


OperatorDescription
inIt returns True if the value is present in the data structure(list, tuple)
not inIt returns False if the value is present in the data structure(list, tuple)

Identity Operators


The identity operators are used to decide whether an element certain class or type.


OperatorDescription
isIt is evaluated to be true if the reference present at both sides point to the same object
is notIt is evaluated to be true if the reference present at both sides do not point to the same object


Python Operator Precedence


We now know all operators and their uses but when they are applied with one another then we should know their precedence otherwise the result may not be the desired one.


OperatorDescription
**The exponent operator is given priority over all the others used in the expression.
~ + –The negation, unary plus, and minus.
* / % //The multiplication, divide, modules, reminder, and floor division.
+ –Binary plus, and minus
>> <<Left shift. and right shift
&Binary and.
^ |Binary xor, and or
<= < > >=Comparison operators (less than, less than equal to, greater than, greater then equal to).
<> == !=Equality operators.
= %= /= //= -= +=
*= **=
Assignment operators
is
is not
Identity operators
in
not in
Membership operators
not
or
and
Logical operators



Also Read:


  • YouTube Video Downloader Using Python
    This is just a two-line program but to convert it to GUI YouTube Video Downloader is a bit longer still, this program of GUI YouTube Video Downloader is less than 25 lines. But still, if you want that two-line program then click here. We have explained this program in every line to make it understand…
  • Word Mix in Python | Assignment Expert
    Problem Statement: We are given a sentence, we need to word mix in Python means we need to mix the words of the sentence such that new words get formed. How? We need to select a letter from each word from the same index. If there is no letter in the word, do nothing and…
  • WHY YOU NEED HAND WRITTEN NOTES?
    Handwritten notes are very important while learning any new programming language. WHO AM I? Owner of Python.hub Instagram Page with more than 1.2 million followers. Running this website copyassignment.com which helps in learning programming. Helped more than 3000 people with their projects and doubts. Running a freelancing IT business and helping startups in building their…
  • Why Learn Python? |Explained|
            Why Learn Python? complete explanation-> Nowadays, we all are wondering through our sights, imagination, and thoughts about the scenario after and along in this pandemic time! But, Yes, ‘Learning doesn’t stop and it Shouldn’t!’, right?  So, in this article, we will found the answer to “Why Learn Python?”.                There are a lot of…
  • Why are pre-cleaning steps important to complete prior to data cleaning?
    Data cleaning is the process of ensuring data is correct, consistent, and usable. You can clean data by identifying errors or corruptions, correcting or deleting them, or manually processing data as needed to prevent the same errors from occurring. The quality of data will have a huge impact on the quality of the model. Data…