Python Documents
  • Data Types In Python
  • Variables In Python
  • Operators In Python
  • User Input In Python
  • Type Casting In Python
  • Strings In Python
    • String CheatSheet
  • Conditional Statements In Python
  • Lists In Python
    • List CheatSheet
  • Sets
  • Tuples in Python
  • Dictionary in Python
  • Functions In Python
  • File Handling In Python
  • Branching using Conditional Statements and Loops in Python
  • Numerical Computing with Python and Numpy
  • String CheatSheet
Powered by GitBook
On this page
  • What are Operators?
  • 1.Arithematic Operators:%
  • Addition by +:
  • Concatenation by :
  • Subtraction by :
  • Multiplication by :
  • Repetition by :
  • Division by :
  • Exponent(raised to the power) by :
  • Floor Division by :
  • Modulus by % :
  • 2.Comparison Operators:
  • Greater than by > :
  • Greater than or Equals to by >= :
  • Smaller than by < :
  • Smaller than or Equals to by <= :
  • Equals to by :
  • Not Equals to by :
  • 3.Assignment Operators:
  • 4.Logical Operators: , ,
  • Operator Precedence (Priority Of Operators):
  • Assignment:

Operators In Python

PreviousVariables In PythonNextUser Input In Python

Last updated 2 years ago

What are Operators?

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

1.Arithematic 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:

Addition by +:

+++used to add two operands (Variables or Values). For example:

In [1]:

first_number = 20
second_number = 30
add = first_number + second_number
print(add)
50

Concatenation by +++:

+++is also used for Concatenation of two strings. For Example:

In [2]:

first_name = "Console"
last_name = "flare"
add = first_name + last_name
print(add)
Consoleflare

Note: +++ Only concatenate strings, it will give error if you try to concatenate string with any other Data Type.

Subtraction by −−−:

−−−used to perform subtraction on two operands (Variables or Values). For example:

In [3]:

first_number = 30
second_number = 20
sub = first_number - second_number
print(sub)
10

Multiplication by ∗∗∗:

∗∗∗ used to perform subtraction on two operands (Variables or Values). For example:

In [4]:

first_number = 30
second_number = 20
mul = first_number * second_number
print(mul)
600

Repetition by ∗∗∗:

∗∗∗ is also used to perform repetition of a string. For Example:

In [5]:

platform = "CF"
rep = platform * 5
print(rep)
CFCFCFCFCF

Note: ∗∗∗ Only repeats string if string is multiplied only and only by a value with Integer Data Type, Otherwise it will give error.

Division by ///:

///performs division between two Operands. It always gives output in float data type. for example:

In [6]:

first_number = 10
second_number = 5
div = first_number / second_number
print(div)
2.0

Exponent(raised to the power) by ∗∗∗∗∗∗:

∗∗∗∗∗∗ 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]:

first_number = 10
second_number = 2
powr = first_number ** second_number
print(powr)
100

Floor Division by //////:

//////returns the Quotient after the division of two operands . It always gives output in Integer data type. for example:

In [8]:

first_number = 5
second_number = 2
quo = first_number // second_number
print(quo)
2

Modulus by % :

% returns the Remainder after the division of two operands . It always gives output in Integer data type. for example:

In [9]:

first_number = 5
second_number = 2
rem = first_number % second_number
print(rem)
1

2.Comparison Operators:>,<,>=,<=,!=,==>,<,>=,<=,!=,==>,<,>=,<=,!=,==

Comparison operators are used to comparing the value of the two operands and returns Boolean true or false accordingly, as follows:

Greater than by > :

>>>> If the first operand is greater than the second operand, then the condition becomes True, otherwise False. For Example:

In [10]:

x = 7>3
x

Out[10]:

True

Greater than or Equals to by >= :

>=>=>=If the first operand is greater than Or Equals to the second operand, then the condition becomes True, otherwise False. For Example:

In [11]:

x = 5
y = 5
print(x>=y)
True

Smaller than by < :

<<<If the first operand is less than the second operand, then the condition becomes True, otherwise False. For Example:

In [12]:

x = 3
y = 5
out = x<y
print(out)
True

Smaller than or Equals to by <= :

<=<=<=If the first operand is less than Or Equals to the second operand, then the condition becomes True, otherwise False. For Example:

In [13]:

x = 3
y = 5
out = x<=y
print(out)
True

Equals to by ====== :

====== If the value of two operands is equal, then the condition becomes true,otherwise False.For Example:

In [1]:

x = 3
y = 5
out = x==y
print(out)
False

Not Equals to by !=!=!= :

!=!=!=If the value of two operands is not equal, then the condition becomes true,otherwise False.For Example:

In [2]:

x = 3
y = 5
out = x!=y
print(out)
True

3.Assignment Operators:=,+=,−=,∗=,/=,∗∗=,//==,+=,−=,∗=,/=,∗∗=,//==,+=,−=,∗=,/=,∗∗=,//=

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]:

a = 5
print(a)
5

+=+=+=: 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]:

x = 5 
x = x+5
print(x)
10

We can write x = x + 5 as x+=5.

In [7]:

x = 5
x+=5
print(x)
10

Note: +=+=+= performs operation on same variable and store value in the same variable. This is how rest assignment operators(−=,∗=,/=,∗∗=,//=−=,∗=,/=,∗∗=,//=−=,∗=,/=,∗∗=,//=−=,∗=,/=,∗∗=,//=,%=) in Python work.

4.Logical Operators: andandand, ororor , notnotnot

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.

andandand: If both expressions are True , Output will be True otherwise False.For Example:

In [8]:

x = 7>3 and 4>3
print(x)
y = 7>3 and 5>9
print(y)
True
False

ororor : If atleast one expression is True , Output will be True otherwise False.For Example:

In [10]:

x = 7>3 or 4>3
print(x)
y = 7>3 or 5>9
print(y)
z=7<3 and 5<4
print(z)
True
True
False

notnotnot : If expression is True , Output will be False and vice-versa.For Example:

In [11]:

x = 5>3
not(x)

Out[11]:

False

Operator Precedence (Priority Of Operators):

What is Operator Precedence?

To understand Operator Precedence better , let's start with an example:

In [ ]:

x = 2
y = 2
z = 2
p = 2
calc = x-y*p**z
print(calc)

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]:

x = 2
y = 2
z = 2
p = 2
calc = x-y*p**z
print(calc)
-6

Priority Table of Python :

()−Parentheses()−Parentheses()−Parentheses

∗∗−Exponent∗∗−Exponent∗∗−Exponent

∗,/,//∗,/,//∗,/,//,% Multiplication,Division,Floordivision,ModulusMultiplication,Division,Floordivision,ModulusMultiplication,Division,Floordivision,Modulus

+,−+,−+,− Addition,SubtractionAddition,SubtractionAddition,Subtraction

==,!=,>,>=,<,<===,!=,>,>=,<,<===,!=,>,>=,<,<= ComparisonsComparisonsComparisons

LogicalNOTLogicalNOTLogicalNOT

LogicalANDLogicalANDLogicalAND

LogicalORLogicalORLogicalOR

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]:

x = 2
y = 2
z = 2
p = 2
calc = x+y-p/z
print(calc)
3.0

In the Code above firstly //// (division Operator) solved , after that from right to left we solved the expression. first //// then ++++ then −−−−

In [ ]:

 

Assignment:

  • 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 [ ]:

a = False
b = False
x = not(a)
y = not(b)
print(a and b)
print(a and x)
print(y and b)
print(x and y)
  • Write a Program where output is value raised to the power 100.