Operators In Python
Last updated
Last updated
Operators are symbols, that are used to perform operations on Operands (Variables and Values.) There are variety of Operators which are as follows:
Arithmetic operators
Assignment operators
Comparison Operators
Logical Operators
Arithmetic operators are used to perform arithmetic operations between two Variables or Values. There are various arithematic operators that we can use in Python , as follows:
used to add two operands (Variables or Values). For example:
In [1]:
is also used for Concatenation of two strings. For Example:
In [2]:
Note: Only concatenate strings, it will give error if you try to concatenate string with any other Data Type.
used to perform subtraction on two operands (Variables or Values). For example:
In [3]:
used to perform subtraction on two operands (Variables or Values). For example:
In [4]:
is also used to perform repetition of a string. For Example:
In [5]:
Note: Only repeats string if string is multiplied only and only by a value with Integer Data Type, Otherwise it will give error.
performs division between two Operands. It always gives output in float data type. for example:
In [6]:
is used to raise the number on the left to the power of the exponent of the right. That is, in the expression 5 ** 3 , 5 is being raised to the 3rd power which is 125. For example:
In [7]:
returns the Quotient after the division of two operands . It always gives output in Integer data type. for example:
In [8]:
% returns the Remainder after the division of two operands . It always gives output in Integer data type. for example:
In [9]:
Comparison operators are used to comparing the value of the two operands and returns Boolean true or false accordingly, as follows:
> If the first operand is greater than the second operand, then the condition becomes True, otherwise False. For Example:
In [10]:
Out[10]:
If the first operand is greater than Or Equals to the second operand, then the condition becomes True, otherwise False. For Example:
In [11]:
If the first operand is less than the second operand, then the condition becomes True, otherwise False. For Example:
In [12]:
If the first operand is less than Or Equals to the second operand, then the condition becomes True, otherwise False. For Example:
In [13]:
If the value of two operands is equal, then the condition becomes true,otherwise False.For Example:
In [1]:
If the value of two operands is not equal, then the condition becomes true,otherwise False.For Example:
In [2]:
The Assignment Operators are used to assign the value of the right expression to the left operand.
Do Not Confuse: and are both different operators. is assignment operator, while is a comparison operator.
: It assigns the value of the right expression to the left operand.For example:
In [3]:
: We have often used expression like a = a+5 , it will add 5 in the previous value of a and then assign it to a. We can simply write this expression as a+=5.For example:
In [6]:
We can write x = x + 5 as x+=5.
In [7]:
Note: performs operation on same variable and store value in the same variable. This is how rest assignment operators(−=,∗=,/=,∗∗=,//=,%=) in Python work.
The concept of logical operators is simple. They allow a program to make a decision based on multiple conditions.They return output in boolean i.e. True or False.
: If both expressions are True , Output will be True otherwise False.For Example:
In [8]:
: If atleast one expression is True , Output will be True otherwise False.For Example:
In [10]:
: If expression is True , Output will be False and vice-versa.For Example:
In [11]:
Out[11]:
What is Operator Precedence?
To understand Operator Precedence better , let's start with an example:
In [ ]:
What do you think output of this calc would be? This may be confusing as we don't know which operator will be solved at first.
Operator Precedence is known as Priority of Operators, which will tell us which operator will be solved at first.
In the above example, power has higher priority than multiplication and multiplication has higher priority than addition. So the output would be : -6. let' see:
In [12]:
Priority Table of Python :
,%
Do Not Confuse: if you look closely, there are many operators with same priority, so if you come up with expression with same priority operators , you will solve it from left to right. Look at the code below:In [13]:
In the Code above firstly / (division Operator) solved , after that from right to left we solved the expression. first / then + then −
In [ ]:
Make a calulator like program where it perform additions, multiplication, division,subtraction on two values.
Take two variables and divide one with another in such a way that the quotient comes as output.
What will be the output of following program:
In [ ]:
Write a Program where output is value raised to the power 100.