Python - Operators
Python - Operators
Operators
1
Operators
❖ Operators are constructs used to modify the values of
operands.
• Arithmetic operator
• Comparison operator
• Assignment operator
• Logical operator
• Bitwise operator
• Membership operator
• Identity operator
2
Arithmetic operator
• + Addition operator to add two operands.
• – Subtraction operator to subtract two
operands.
• * Multiplication operator to multiply two
operands.
• / Division operator to divide left hand
operator by right hand Operator.
• ** Exponential operator to calculate power.
• % Modulus operator to find remainder.
• // Floor division operator to find the
quotient and remove the fractional part.
3
4
Comparison operator / relational operators
6
Assignment Operators
• This operator is used to store right side
operand in the left side operand
• = Store right side operand in left side
operand.
• += Add right side operand to left side operand
and store the result in left side operand.
• – = Subtract right side operand from left side
operand and store the result in left side
operand.
• * = Multiply right side operand with left side
operand and store the result in left side
operand.
7
• / = Divide left side operand by right side
operand and store the result in left side
operand.
• % = Find the modulus and store the remainder
in left side operand.
• ** = Find the exponential and store the result
in left side operand.
• // = Find the floor division and store the result
in left side operand.
8
9
Bitwise Operators
• Operators perform bit level operation on
operands.
10
• ^ Bitwise XOR - Operator performs XOR
operation between operands. Operator copies
bit if it exists only in one operand.
• ~ bitwise inverse - Operator is a unary
operator used to opposite the bits of operand.
11
12
• Take binary value x=1010 (10) y=1100 (12)
Bitwise AND ( & ) Bitwise OR ( | )
X=1 0 1 0 X=1 0 1 0
Y=1 1 0 0 Y=1 1 0 0
1 0 0 0 1 1 1 0
Bitwise XOR ( | ) Bitwise Inverse ( ~ )
X=1 0 1 0 X= 0 0 0 0 1 0 1 0
Y=1 1 0 0
0 1 1 0 1 1 1 1 0 1 0 1
13
14
Logical Operators
19
• Likewise not is operator also
20
Precedence of Operators
• Precedence is the condition that specifies the
importance of each operator relative to the
others.
• When an expression has two or more operators,
we need to identify the correct sequence to
evaluate these operators
21
Operator Description
NOT, OR AND Logical operators
in , not in Membership operator
is, not is Identity operator
=, %=, /=, //=, -=, Assignment operators.
+=, *=, **==
<>, ==, != Equality comparison operator
<=, <, >, >= Comparison operators
^, | Bitwise XOR and OR operator
& Bitwise AND operator
<<, >> Bitwise left shift and right shift
+, - Addition and subtraction
*, /, %, // Multiplication, Division, Modulus and
floor division
** Exponential operator
22
Associativity
• It decides the order in which the operators
with same precedence are executed.
• 2 Types of associativity., One is left-to-right
and other is right-to-left.
• Most of the operators in Python have
left-to-right associativity.
Eg:
• left-to-right associative operators are
multiplication, floor division, etc and **
operator is right-to-left associative.
23
24
• Based on the number of operands, operators
are classified into following two types:
1. Unary Operator : operators are operators with
only one operand. (sign +,-,~)
2. Binary Operator : operators with two operands
that are manipulated to get the result. It used
to compare numeric values and string values.
(**, *, /, %, +, -, <<,>>, &, | ,^,<,>,<= ,>=, == ,!= ,<>)
25
syllabus
• UNIT I
Introduction:
Introduction to Python, Python overview, Getting started with
python, Comments, Identifiers, Reserved keywords, Variables,
Standard Data Types, Operators, Statements and Expressions, String
Operations, Boolean Expressions. Control Statements- for, while, if
elif else, while.
26