PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • What are Operators?
  • 1.Arithematic Operators:%
  • Addition 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:
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

3.Operators In Python

Previous2.Variables In PythonNext4.User 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:

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

600

In [5]:

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

In [6]:

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

In [7]:

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

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

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

Greater than by > :

In [10]:

x = 7>3
x

Out[10]:

True

Greater than or Equals to by >= :

In [11]:

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

Smaller than by < :

In [12]:

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

Smaller than or Equals to by <= :

In [13]:

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

In [1]:

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

In [2]:

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

The Assignment Operators are used to assign the value of the right expression to the left operand.

In [3]:

a = 5
print(a)
5

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

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.

In [8]:

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

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

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 :

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

 

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.

Repetition by ∗∗∗:

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

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:

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:

Floor Division by //////:

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

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

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

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

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

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

Equals to by ====== :

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

Not Equals to by !=!=!= :

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

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

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:

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

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

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

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

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

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

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

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

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

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

LogicalNOTLogicalNOTLogicalNOT

LogicalANDLogicalANDLogicalAND

LogicalORLogicalORLogicalOR

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