Python - Operators

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Python Programming

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

• Operators are used to compare values.


• == Operator to check whether two operands
are equal.
• != or <> Operator to check whether two
operands are not equal.
• >  Operator to check whether first operand is
greater than second operand.
• <  Operator to check whether first operand is
smaller than second operand.
• >= Operator to check whether first operand is
greater than or equal to second operand.
• <= Operator to check whether first operand is
smaller than or equal to second operand. 5
• Likewise other 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.

• & Bitwise AND - Operator performs AND


operation between operands. Operator copies
bit if it exists in both operands.

• | Bitwise OR - Operator performs OR


operation between operands. Operator copies
bit if it exists in either operand.

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.

• << left shift - Operator is used to shift the bits


towards left

• << right shift - Operator is used to shift the


bits towards right

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

Shift right ( >> ) Shift left ( << )


X= 0 0 0 0 1 1 0 0 Move right Move X= 0 0 0 0 1 1 0 0
left
0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0

13
14
Logical Operators

• These operators are used to check two or more


conditions. The result is always a Boolean value

• and logical AND - When both operands are


true, the result become true.

• or logical OR - When any operand is true, the


result becomes true.

• not logical NOT - used to reverse the operand


state.
15
16
Membership Operators
• It is used to check an item or an element that
is part of a string, a list or a tuple.

• A membership operator reduces the effort of


searching an element in the list.

• in - Return true, if item is in list or in sequence.


Return false, if item is not in list or in sequence.

• not in - Return false, if item is in list or in


sequence. Return true, if item is not in list or in
sequence.
17
18
Identity Operators

• These operators are used to check whether


both operands are same or not.

• is - Return true, if the operands are same.


Return false, if the operands are not same.

• not is - Return false, if the operands are same.


Return true, if the operands are not same.

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

You might also like