0% found this document useful (0 votes)
13 views31 pages

Understanding Python Operators Guide

Uploaded by

pranaykhoje111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

Understanding Python Operators Guide

Uploaded by

pranaykhoje111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Operators

Operators
1
Types of Operators
2

agenda Precedence and Associativity


3
Operators
Python Operators
• Python operators are special symbols used to perform specific
operations on one or more operands.
• e.g. +, -, *, /, **,//,<, &, |, ^,~, <<, >>
• The variables, values, or expressions can be used as operands.
• 5+3
• 2 * ( 5 + 3 * 8) / (5 * 7)
• a*a+(2*a*b)+b*b
• Unary Operators – Takes one operand like ~
• Binary Operators – Takes 2 operands like +, -, *
• Operands - Variables, values, or expressions that are used with the
operator to perform a specific operation.

4
Types of
Operators
Types of Operators
• Arithmetic operators
• Comparison operators
• Bitwise operators
• Assignment operators
• Logical operators
• Membership operator
• Identity operator

6
Arithmetic
operators
Arithmetic Operators
• Python Arithmetic operators are used to perform basic
mathematical operations such as addition, subtraction,
multiplication, etc.

8
Comparison
operators
Comparison Operators
• Python Comparison
operators compare the
values of both the
operands and decide the
relation among them.
• Hence they are also called
Relational operators.

10
Comparison Operators
• Although complex object is a number data type in Python, its behavior is different from
others.
• Python doesn't support < and > operators with complex number
• However, it does support equality (==) and inequality (!=) operators.

11
Comparison Operators
• In Python, comparison of only similar sequence objects can be performed.
• A string object is comparable with another string only.
• A list cannot be compared with a tuple, even if both have same items.

12
Comparison Operators
• Sequence objects are compared by lexicographical ordering mechanism.
• The comparison starts from item at 0th index. If they are equal, comparison moves to next
index till the items at certain index happen to be not equal, or one of the sequences is
exhausted.
• If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller
(lesser) one.
• Which of the operands is greater depends on the difference in values of items at the index
where they are unequal. For example, 'BAT'>'BAR' is True, as T comes after R in Unicode
order.
• If all items of two sequences compare equal, the sequences are considered equal.

13
Bitwise operators
Bitwise Operators
• Python Bitwise operator works on bits and performs bit by bit
operation.

15
Bitwise Operators
• and operator - &
• or operator - |
• xor operator - ^
• not operator - ~

16
Bitwise Operators
• Shift operators
• Left shift << - like multiply with power of 2
• Right shift >> - like divide with power of 2
• In case of left shift operator, always add 0s to right regardless of sign
• In case of right shift operator
• Always add 0s on the left, if number is positive
• Always add 1s on the left, if number is negative

17
Assignment
operators
Assignment Operators
• Python Assignment operators are used to assign values to variables.

19
Logical operators
Logical Operators
• Python logical operators are used to combine two or more
conditions and check the final result.

21
Membership
operators
Membership Operators
• Python's membership operators test for membership in a sequence,
such as strings, lists, or tuples.

23
Identity operators
Identity Operators
• Python identity operators compare the memory locations of two
objects.

25
Operator
Precedence &
Associativity
Operator Precedence & Associativity
• Operator’s precedence decides the order of the evaluation in which an operator is evaluated.
• In Python, operators have different levels of precedence.
• When multiple operators are present in an expression, the ones with higher precedence are
evaluated first.
• Python operators have different levels of precedence.
• 5 + 2 * 3 here * will be executed before + hence result is 11
• In the case of operators with the same precedence, their associativity comes into play,
determining the order of evaluation.
• e.g. ~a * +b / -c – Here ~, + and – has same precedence and right to left associativity, * and /
has same precedence and left to right associativity, sequence of execution will be unary -, unary
+, unary ~, binary *, unary /

27
Operator Precedence

28
Built-in functions
• abs - Returns the absolute value of a number
• chr - Returns a character from the specified Unicode code.
• divmod - Returns the quotient and the remainder when argument1
is divided by argument2
• eval - Evaluates and executes an expression
• pow - Returns the value of x to the power of y
• round - Rounds a numbers

29
References
• [Link]

• [Link]

• [Link]

30
DR. KAULWAR BHAGWAT

Thank you bhagwat_kaulwar@[Link]

Dr. Bhagwat Kaulwar | LinkedIn

You might also like