SlideShare a Scribd company logo
1 of 15
Operators in Python
V.Anusuya
Assistant Professor(SG)/CSE
Ramco Institute of Technology
• Operators are the constructs which can
manipulate the value of operands.
• In an expression, an operator is used on
operands.
• Example: 4 + 5 = 9.
• 4 and 5 are called operands
• + is called an Operator
• There are various operators in python:
1. Arithmetic Operator
2. Comparison/Relational Operator
3. Bitwise Operator
4. Assignment Operator
5. Logical Operator
6. Membership Operator
7. Identity Operator
Arithmetic Operators
Arithmetic operators are used to perform some basic arithmetic
operations. These operators can be applied on numbers as well
as on variables to perform the corresponding operations.
Example:
>>>x=10
>>>y=20
>>>z=x+y
>>>print z
30 #output
>>> z=x*y
>>>print z
200
>>>z=x**2
>>>print z
100
Operator Description Example
** Exponent
Performs exponential (power) calculation on
operators
c=a**b
*
Multiplicatio
n
Multiplies values on either side of the operator c=a * b
/ Division Divides left hand operand by right hand operand c=b / a
% Modulus
Divides left hand operand by right hand operand
and returns remainder
c=b % a
//
Floor Division - The division of operands where
the result is the quotient in which the digits after
the decimal point are removed. But if one of the
operands is negative, the result is floored, i.e.,
rounded away from zero (towards negative
infinity):
9//2=4
+ Addition Add values on either side of the operator. c=a + b
- Subtraction
Subtracts right hand operand from the left hand
operand.
c=a – b
Comparison Operators
Comparison operators also known as relational operators are used to compute
the values on its either sides and determines the relation between them.
Example, a=100 and b=200
Operator Description Example
==
If the values of two operands are equal, then the
condition becomes true.
(a == b) is
not true.
!=
If the values of two operands are not equal, then
condition becomes true.
(a!=b) is true
>
If the value of left operand is greater than the value of
right operand, then condition becomes true.
(a > b) is not
true.
<
If the value of left operand is less than the value of right
operand, then condition becomes true.
(a < b) is
true.
>=
If the value of left operand is greater than or equal to the
value of right operand, then condition becomes true.
(a >= b) is
not true.
<=
If the value of left operand is less than or equal to the
value of right operand, then condition becomes true.
(a <= b) is
true
Bitwise Operators
• These operators perform bit level operations on operands. Let
us take two operands x=10 and y=4. In binary format this can
be written as x=1010 and y=0100.
Operator Description Example
& Binary AND
Operator copies a bit to the result if it exists in
both operands
x& y = 0
(0000 0000)
| Binary OR It copies a bit if it exists in either operand.
x | y = 14
(0000 1110)
^ Binary XOR
It copies the bit if it is set in one operand but not
both.
x ^ y = 14
(0000 1110)
~ Binary Ones
Complement
It is used to opposite the bits of operand. ~x results 0110
<< Binary Left
Shift
The left operands value is moved left by the
number of bits specified by the right operand.
x<< 2 = 40
(0010 1000)
>> Binary Right
Shift
The left operands value is moved right by the
number of bits specified by the right operand.
x>> 2 = 2
(0000 0010)
Assignment Operator
• This operator is used to store the right side operand in the left
side operand
Operator Description Example
=
Assigns values from right side operands to left
side operand
c = a + b assigns value
of a + b into c
+=
It adds right operand to the left operand and
assign the result to left operand
c += a is equivalent to c
= c + a
-=
It subtracts right operand from the left operand
and assign the result to left operand
c -= a is equivalent to c
= c - a
*=
It multiplies right operand with the left operand
and assign the result to left operand
c *= a is equivalent to c
= c * a
/=
It divides left operand with the right operand
and assign the result to left operand
c /= a is equivalent to c
=c/a
%=
It takes modulus using two operands and assign
the result to left operand
c %= a is equivalent to
c = c % a
**= Performs exponential (power) calculation on
operators and assign value to the left operand
c **= a is equivalent to
c = c ** a
//= It performs floor division on operators and
assign value to the left operand
c //= a is equivalent to c
= c // a
Logical Operators
• These operators are used to check two or more conditions. The
resultant operator is always a Boolean value. Here, x and y are
two operands that store either true or false Boolean values.
• Assume x is true and y is false.
Operator Description Example
and (logical
and)
When both operands are true, the
resultant become true.
x and y results
false
or (logical or)
When any operand is true, the
resultant become true.
x or y results true
not (logical
NOT)
This operator is used to reverse the
operand state.
not x results false
Membership Operators
Python supports two types of membership operators–in and not in. These
operators, test for membership in a sequence such as strings, lists, or tuples.
in Operator: The operator returns true if a variable is found in the
specified sequence and false otherwise.
not in Operator: The operator returns true if a variable is not found in the
specified sequence and false otherwise.
Example:
>>>x=10
>>>y=12
>>>list=[21,13,10,17]
>>>10 in list
True
>>>5 in list
False
>>>10 not in list
False
Identity Operators
• is Operator: Returns true if operands or values
on both sides of the operator point to the same
object and false otherwise.
• is not Operator: Returns true if operands or
values on both sides of the operator does not point
to the same object and false otherwise. Example:
>>>x=12
>>>y=12
>>> x is y
True
Precedence of Python Operators
Operator Description
** Exponentiation (raise to the power)
~ + - Complement, unary plus(+a) and minus (-a)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^ | Bitwise XOR and OR
<= < > >= Comparison operators
== != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Example
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d
Expressions
• An expression is any legal combination of symbols (like
variables, constants and operators) that represents a value.
• In Python, an expression must have at least one operand
(variable or constant) and can have one or more operators.
• On evaluating an expression, we get a value.
• Constant Expressions: One that involves only constants.
Example: 8 + 9 – 2
• Integral Expressions: One that produces an integer result after
evaluating the expression. Example: a = 10
• Floating Point Expressions: One that produces floating point
results. Example: a * b / 2
• Relational Expressions: One that returns either true or false
value. Example: c = a>b
• Logical Expressions: One that combines two or more
relational expressions and returns a value as True or False.
Example: a>b & y! = 0
• Bitwise Expressions: One that manipulates data at bit level.
Example: x = y&z
• Assignment Expressions: One that assigns a value to a
variable.
Example: c = a + b

More Related Content

What's hot

Control statements in c
Control statements in cControl statements in c
Control statements in cSathish Narayanan
 
Python Operators
Python OperatorsPython Operators
Python OperatorsAdheetha O. V
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythondeepalishinkar1
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptxAkshayAggarwal79
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variablessangrampatil81
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handlingMohammed Sikander
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageLaxman Puri
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingSrinivas Narasegouda
 

What's hot (20)

C functions
C functionsC functions
C functions
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
Python basics
Python basicsPython basics
Python basics
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 

Similar to Operators in Python

itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in javaAtul Sehdev
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
python operators.ppt
python operators.pptpython operators.ppt
python operators.pptErnieAcuna
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).pptKalaiVani395886
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.pptTejaValmiki
 
11operator in c#
11operator in c#11operator in c#
11operator in c#Sireesh K
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Mohd Harris Ahmad Jaal
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++Shobi P P
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type Asheesh kushwaha
 

Similar to Operators in Python (20)

Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 

More from Anusuya123

Unit-III Correlation and Regression.pptx
Unit-III Correlation and Regression.pptxUnit-III Correlation and Regression.pptx
Unit-III Correlation and Regression.pptxAnusuya123
 
Types of Data-Introduction.pptx
Types of Data-Introduction.pptxTypes of Data-Introduction.pptx
Types of Data-Introduction.pptxAnusuya123
 
Basic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptxBasic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptxAnusuya123
 
Data warehousing.pptx
Data warehousing.pptxData warehousing.pptx
Data warehousing.pptxAnusuya123
 
Unit 1-Data Science Process Overview.pptx
Unit 1-Data Science Process Overview.pptxUnit 1-Data Science Process Overview.pptx
Unit 1-Data Science Process Overview.pptxAnusuya123
 
Introduction to Data Science.pptx
Introduction to Data Science.pptxIntroduction to Data Science.pptx
Introduction to Data Science.pptxAnusuya123
 
5.2.2. Memory Consistency Models.pptx
5.2.2. Memory Consistency Models.pptx5.2.2. Memory Consistency Models.pptx
5.2.2. Memory Consistency Models.pptxAnusuya123
 
5.1.3. Chord.pptx
5.1.3. Chord.pptx5.1.3. Chord.pptx
5.1.3. Chord.pptxAnusuya123
 
3. Descriptive statistics.ppt
3. Descriptive statistics.ppt3. Descriptive statistics.ppt
3. Descriptive statistics.pptAnusuya123
 
1. Intro DS.pptx
1. Intro DS.pptx1. Intro DS.pptx
1. Intro DS.pptxAnusuya123
 
5.Collective bargaining.pptx
5.Collective bargaining.pptx5.Collective bargaining.pptx
5.Collective bargaining.pptxAnusuya123
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
RuntimeenvironmentAnusuya123
 
Think pair share
Think pair shareThink pair share
Think pair shareAnusuya123
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 

More from Anusuya123 (14)

Unit-III Correlation and Regression.pptx
Unit-III Correlation and Regression.pptxUnit-III Correlation and Regression.pptx
Unit-III Correlation and Regression.pptx
 
Types of Data-Introduction.pptx
Types of Data-Introduction.pptxTypes of Data-Introduction.pptx
Types of Data-Introduction.pptx
 
Basic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptxBasic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptx
 
Data warehousing.pptx
Data warehousing.pptxData warehousing.pptx
Data warehousing.pptx
 
Unit 1-Data Science Process Overview.pptx
Unit 1-Data Science Process Overview.pptxUnit 1-Data Science Process Overview.pptx
Unit 1-Data Science Process Overview.pptx
 
Introduction to Data Science.pptx
Introduction to Data Science.pptxIntroduction to Data Science.pptx
Introduction to Data Science.pptx
 
5.2.2. Memory Consistency Models.pptx
5.2.2. Memory Consistency Models.pptx5.2.2. Memory Consistency Models.pptx
5.2.2. Memory Consistency Models.pptx
 
5.1.3. Chord.pptx
5.1.3. Chord.pptx5.1.3. Chord.pptx
5.1.3. Chord.pptx
 
3. Descriptive statistics.ppt
3. Descriptive statistics.ppt3. Descriptive statistics.ppt
3. Descriptive statistics.ppt
 
1. Intro DS.pptx
1. Intro DS.pptx1. Intro DS.pptx
1. Intro DS.pptx
 
5.Collective bargaining.pptx
5.Collective bargaining.pptx5.Collective bargaining.pptx
5.Collective bargaining.pptx
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
Runtimeenvironment
 
Think pair share
Think pair shareThink pair share
Think pair share
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 

Recently uploaded

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 

Recently uploaded (20)

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 

Operators in Python

  • 1. Operators in Python V.Anusuya Assistant Professor(SG)/CSE Ramco Institute of Technology
  • 2. • Operators are the constructs which can manipulate the value of operands. • In an expression, an operator is used on operands. • Example: 4 + 5 = 9. • 4 and 5 are called operands • + is called an Operator
  • 3. • There are various operators in python: 1. Arithmetic Operator 2. Comparison/Relational Operator 3. Bitwise Operator 4. Assignment Operator 5. Logical Operator 6. Membership Operator 7. Identity Operator
  • 4. Arithmetic Operators Arithmetic operators are used to perform some basic arithmetic operations. These operators can be applied on numbers as well as on variables to perform the corresponding operations. Example: >>>x=10 >>>y=20 >>>z=x+y >>>print z 30 #output >>> z=x*y >>>print z 200 >>>z=x**2 >>>print z 100
  • 5. Operator Description Example ** Exponent Performs exponential (power) calculation on operators c=a**b * Multiplicatio n Multiplies values on either side of the operator c=a * b / Division Divides left hand operand by right hand operand c=b / a % Modulus Divides left hand operand by right hand operand and returns remainder c=b % a // Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity): 9//2=4 + Addition Add values on either side of the operator. c=a + b - Subtraction Subtracts right hand operand from the left hand operand. c=a – b
  • 6. Comparison Operators Comparison operators also known as relational operators are used to compute the values on its either sides and determines the relation between them. Example, a=100 and b=200 Operator Description Example == If the values of two operands are equal, then the condition becomes true. (a == b) is not true. != If the values of two operands are not equal, then condition becomes true. (a!=b) is true > If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true. < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true. <= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true
  • 7. Bitwise Operators • These operators perform bit level operations on operands. Let us take two operands x=10 and y=4. In binary format this can be written as x=1010 and y=0100. Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands x& y = 0 (0000 0000) | Binary OR It copies a bit if it exists in either operand. x | y = 14 (0000 1110) ^ Binary XOR It copies the bit if it is set in one operand but not both. x ^ y = 14 (0000 1110) ~ Binary Ones Complement It is used to opposite the bits of operand. ~x results 0110 << Binary Left Shift The left operands value is moved left by the number of bits specified by the right operand. x<< 2 = 40 (0010 1000) >> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. x>> 2 = 2 (0000 0010)
  • 8. Assignment Operator • This operator is used to store the right side operand in the left side operand Operator Description Example = Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c += It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -= It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *= It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c =c/a %= It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **= Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a
  • 9. Logical Operators • These operators are used to check two or more conditions. The resultant operator is always a Boolean value. Here, x and y are two operands that store either true or false Boolean values. • Assume x is true and y is false. Operator Description Example and (logical and) When both operands are true, the resultant become true. x and y results false or (logical or) When any operand is true, the resultant become true. x or y results true not (logical NOT) This operator is used to reverse the operand state. not x results false
  • 10. Membership Operators Python supports two types of membership operators–in and not in. These operators, test for membership in a sequence such as strings, lists, or tuples. in Operator: The operator returns true if a variable is found in the specified sequence and false otherwise. not in Operator: The operator returns true if a variable is not found in the specified sequence and false otherwise. Example: >>>x=10 >>>y=12 >>>list=[21,13,10,17] >>>10 in list True >>>5 in list False >>>10 not in list False
  • 11. Identity Operators • is Operator: Returns true if operands or values on both sides of the operator point to the same object and false otherwise. • is not Operator: Returns true if operands or values on both sides of the operator does not point to the same object and false otherwise. Example: >>>x=12 >>>y=12 >>> x is y True
  • 12. Precedence of Python Operators Operator Description ** Exponentiation (raise to the power) ~ + - Complement, unary plus(+a) and minus (-a) * / % // Multiply, divide, modulo and floor division + - Addition and subtraction >> << Right and left bitwise shift & Bitwise 'AND' ^ | Bitwise XOR and OR <= < > >= Comparison operators == != Equality operators = %= /= //= -= += *= **= Assignment operators is is not Identity operators in not in Membership operators not or and Logical operators
  • 13. Example a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d
  • 14. Expressions • An expression is any legal combination of symbols (like variables, constants and operators) that represents a value. • In Python, an expression must have at least one operand (variable or constant) and can have one or more operators. • On evaluating an expression, we get a value. • Constant Expressions: One that involves only constants. Example: 8 + 9 – 2 • Integral Expressions: One that produces an integer result after evaluating the expression. Example: a = 10 • Floating Point Expressions: One that produces floating point results. Example: a * b / 2
  • 15. • Relational Expressions: One that returns either true or false value. Example: c = a>b • Logical Expressions: One that combines two or more relational expressions and returns a value as True or False. Example: a>b & y! = 0 • Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z • Assignment Expressions: One that assigns a value to a variable. Example: c = a + b