SlideShare a Scribd company logo
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

Python ppt
Python pptPython ppt
Python ppt
Anush verma
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
Jothi Thilaga P
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
Arockia Abins
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Operators in python
Operators in pythonOperators in python
Operators in python
eShikshak
 
Python Operators
Python OperatorsPython Operators
Python Operators
Adheetha O. V
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Python set
Python setPython set
Python set
Mohammed Sikander
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Python : Data Types
Python : Data TypesPython : Data Types
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Edureka!
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 

What's hot (20)

Python ppt
Python pptPython ppt
Python ppt
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Python set
Python setPython set
Python set
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python programming
Python  programmingPython  programming
Python programming
 

Similar to Operators in Python

Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
Atul Sehdev
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
AbhishekMondal42
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
ErnieAcuna
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
AllanGuevarra1
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
KalaiVani395886
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
TejaValmiki
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
Sireesh K
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
Thesis Scientist Private Limited
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
HarithaRanasinghe
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
Mohd 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
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
Jaya Kumari
 

Similar to Operators in Python (20)

Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
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)
 
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.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.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
 
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
 
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
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 

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.pptx
Anusuya123
 
Types of Data-Introduction.pptx
Types of Data-Introduction.pptxTypes of Data-Introduction.pptx
Types of Data-Introduction.pptx
Anusuya123
 
Basic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptxBasic Statistical Descriptions of Data.pptx
Basic Statistical Descriptions of Data.pptx
Anusuya123
 
Data warehousing.pptx
Data warehousing.pptxData warehousing.pptx
Data warehousing.pptx
Anusuya123
 
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
Anusuya123
 
Introduction to Data Science.pptx
Introduction to Data Science.pptxIntroduction to Data Science.pptx
Introduction to Data Science.pptx
Anusuya123
 
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
Anusuya123
 
5.1.3. Chord.pptx
5.1.3. Chord.pptx5.1.3. Chord.pptx
5.1.3. Chord.pptx
Anusuya123
 
3. Descriptive statistics.ppt
3. Descriptive statistics.ppt3. Descriptive statistics.ppt
3. Descriptive statistics.ppt
Anusuya123
 
1. Intro DS.pptx
1. Intro DS.pptx1. Intro DS.pptx
1. Intro DS.pptx
Anusuya123
 
5.Collective bargaining.pptx
5.Collective bargaining.pptx5.Collective bargaining.pptx
5.Collective bargaining.pptx
Anusuya123
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
Runtimeenvironment
Anusuya123
 
Think pair share
Think pair shareThink pair share
Think pair share
Anusuya123
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
Anusuya123
 

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

21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
sanabts249
 
Exploring Deep Learning Models for Image Recognition: A Comparative Review
Exploring Deep Learning Models for Image Recognition: A Comparative ReviewExploring Deep Learning Models for Image Recognition: A Comparative Review
Exploring Deep Learning Models for Image Recognition: A Comparative Review
sipij
 
Trends in Computer Aided Design and MFG.
Trends in Computer Aided Design and MFG.Trends in Computer Aided Design and MFG.
Trends in Computer Aided Design and MFG.
Tool and Die Tech
 
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NECNEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
PANIQUEPOMAJAELYOSSE
 
LSA Code Chaper II - Personal Life Saving Appliances.pdf
LSA Code Chaper II - Personal Life Saving Appliances.pdfLSA Code Chaper II - Personal Life Saving Appliances.pdf
LSA Code Chaper II - Personal Life Saving Appliances.pdf
danielpensoscarcella
 
Conservation of Taksar through Economic Regeneration
Conservation of Taksar through Economic RegenerationConservation of Taksar through Economic Regeneration
Conservation of Taksar through Economic Regeneration
PriyankaKarn3
 
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
doner6
 
Ludo system project report management .pdf
Ludo  system project report management .pdfLudo  system project report management .pdf
Ludo system project report management .pdf
Kamal Acharya
 
Response & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITHResponse & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITH
IIIT Hyderabad
 
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-IDUNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
GOWSIKRAJA PALANISAMY
 
Lecture 3 Biomass energy...............ppt
Lecture 3 Biomass energy...............pptLecture 3 Biomass energy...............ppt
Lecture 3 Biomass energy...............ppt
RujanTimsina1
 
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
PradeepKumarSK3
 
Social media management system project report.pdf
Social media management system project report.pdfSocial media management system project report.pdf
Social media management system project report.pdf
Kamal Acharya
 
Stiffness Method for structure analysis - Truss
Stiffness Method  for structure analysis - TrussStiffness Method  for structure analysis - Truss
Stiffness Method for structure analysis - Truss
adninhaerul
 
Thermodynamics Digital Material basics subject
Thermodynamics Digital Material basics subjectThermodynamics Digital Material basics subject
Thermodynamics Digital Material basics subject
JigneshChhatbar1
 
Introduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer NetworkingIntroduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer Networking
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
Jet Propulsion and its working principle.pdf
Jet Propulsion and its working principle.pdfJet Propulsion and its working principle.pdf
Jet Propulsion and its working principle.pdf
KIET Group of Institutions
 
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY pptCONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
ASHOK KUMAR SINGH
 
Software Engineering and Project Management - Introduction to Project Management
Software Engineering and Project Management - Introduction to Project ManagementSoftware Engineering and Project Management - Introduction to Project Management
Software Engineering and Project Management - Introduction to Project Management
Prakhyath Rai
 
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
Mani Krishna Sarkar
 

Recently uploaded (20)

21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
 
Exploring Deep Learning Models for Image Recognition: A Comparative Review
Exploring Deep Learning Models for Image Recognition: A Comparative ReviewExploring Deep Learning Models for Image Recognition: A Comparative Review
Exploring Deep Learning Models for Image Recognition: A Comparative Review
 
Trends in Computer Aided Design and MFG.
Trends in Computer Aided Design and MFG.Trends in Computer Aided Design and MFG.
Trends in Computer Aided Design and MFG.
 
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NECNEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
NEC 2023 NATIONAL ELECTRICAL DODE 2023 NEC
 
LSA Code Chaper II - Personal Life Saving Appliances.pdf
LSA Code Chaper II - Personal Life Saving Appliances.pdfLSA Code Chaper II - Personal Life Saving Appliances.pdf
LSA Code Chaper II - Personal Life Saving Appliances.pdf
 
Conservation of Taksar through Economic Regeneration
Conservation of Taksar through Economic RegenerationConservation of Taksar through Economic Regeneration
Conservation of Taksar through Economic Regeneration
 
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
6G TECHNOLOGY:REVOTION FROM 1G TO 6G AND ADVANTAGES-DISADVANTAGES
 
Ludo system project report management .pdf
Ludo  system project report management .pdfLudo  system project report management .pdf
Ludo system project report management .pdf
 
Response & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITHResponse & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITH
 
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-IDUNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
 
Lecture 3 Biomass energy...............ppt
Lecture 3 Biomass energy...............pptLecture 3 Biomass energy...............ppt
Lecture 3 Biomass energy...............ppt
 
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
 
Social media management system project report.pdf
Social media management system project report.pdfSocial media management system project report.pdf
Social media management system project report.pdf
 
Stiffness Method for structure analysis - Truss
Stiffness Method  for structure analysis - TrussStiffness Method  for structure analysis - Truss
Stiffness Method for structure analysis - Truss
 
Thermodynamics Digital Material basics subject
Thermodynamics Digital Material basics subjectThermodynamics Digital Material basics subject
Thermodynamics Digital Material basics subject
 
Introduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer NetworkingIntroduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer Networking
 
Jet Propulsion and its working principle.pdf
Jet Propulsion and its working principle.pdfJet Propulsion and its working principle.pdf
Jet Propulsion and its working principle.pdf
 
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY pptCONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
CONFINED SPACE ENTRY TRAINING FOR OIL INDUSTRY ppt
 
Software Engineering and Project Management - Introduction to Project Management
Software Engineering and Project Management - Introduction to Project ManagementSoftware Engineering and Project Management - Introduction to Project Management
Software Engineering and Project Management - Introduction to Project Management
 
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
1239_2.pdf IS CODE FOR GI PIPE FOR PROCUREMENT
 

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