0% found this document useful (0 votes)
15 views

Operators Complete

Uploaded by

Rahul Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Operators Complete

Uploaded by

Rahul Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

OPERATORS

Musarrat Ahmed
Outline
● Arithmetic operators
● Relational operators
● Equality comparison on floats
● Logical operators
● Setting a default value using short-circuit evaluation
● Bitwise operators
● Identity operators
● Integer caching in Python
● Precedence of operators
Arithmetic Operators
● The result of standard division (/) is always a float, even if the dividend is evenly divisible by the
divisor.

● When the result of floor division (//) is positive, it is as though the fractional
portion is truncated off, leaving only the integer portion.
● When the result is negative, the result is rounded down to the next smallest
(greater negative) integer.
Relational Operators
● Relational operators yield values: True, False
● When relational operators are applied to strings,
then ASCII values are compared.
● Thus, ‘h’>’H’ yields True since ASCII code of
‘H’ is 104 and that of ’H’ is 72.
Equality Comparison on Floats
● Value stored internally for a float object may not be precisely what you’d think
it would be.
● It is poor practice to compare floating-point values for exact equality.
● Floating point numbers are represented internally as binary (base-2) fractions. Most decimal
fractions cannot be represented exactly as binary fractions.
● So in most cases the internal representation of a floating-point number is an approximation of the
actual value.
● The preferred way to determine whether two floating-point values are “equal” is to compute whether
they are close to one another, given some tolerance.
Logical Operators
● Precedence -> not > and > or
● Short-circuit evaluation for and and or.
● You can determine the “truthiness” of
an object or expression with the
built-in bool() function.
● bool() returns True if its argument is
truthy and False if it is falsy.
● Logical operators can also be applied
on non-boolean operands.
● The result depends on the “truthiness”
of the operands.
● Here the expression x or y does not evaluate to either True or False, but
instead to one of either x or y.
● It is still the case that the expression x or y will be truthy if either x or y is
truthy, and falsy if both x and y are falsy.

● As with or, the expression x and y does not evaluate to either True
or False, but instead to one of either x or y.
● x and y will be truthy if both x and y are truthy, and falsy otherwise.
Setting a default value using Short-circuit evaluation
● Suppose you want to assign a variable s to the value contained in another variable called string. But
if string is empty, you want to supply a default value.

● If string is non-empty, it is truthy, and the


expression will be true at that point. Evaluation
stops, and the value of string is returned and
assigned to s.
● On the other hand, if string is an empty
string, it is falsy.
● Evaluation of the expression continues to
the next operand, '<default_value>', which is
returned and assigned to s.
Bitwise Operators
● Bitwise operators treat operands as sequences of binary digits and operate on them bit by bit.
Note: The purpose of the '0b{:04b}'.format() is to format the numeric output of the bitwise operations, to
make them easier to read.
For more on bitwise operators - Bitwise Operators in Python – Real Python
Identity Operators
● Python provides two operators, is and is not, that determine whether the given operands
have the same identity—that is, refer to the same object.
● This is not the same thing as equality, which means the two operands refer to objects that
contain the same data but are not necessarily the same object.
Integer Caching in Python
● No explicit limit on the size of an integer. Therefore, no need to worry about integer overflows.
● But then, trivial operations like addition, multiplication, division can be inefficient.
● To overcome this, frequently used smaller integers in the range -5 to 256 are kept in an array of
integer objects.
● When you create an int in that range you actually just get back a reference to the existing object.
● This process is called interning and all
the numbers in the range -5 to 256 are
Singleton objects.
● Singleton objects exist only once, and
any variables that point to them will all
point to the same object at the same
memory address.
Precedence of Operators
● Operator precedence can be overridden
using parentheses. Expressions in
parentheses are always performed first.
Not 9 == 8 and 7 + 1 != 8 or 6 < 4.5
Quiz Time!
1. Consider the following code snippet:

After these are executed, what is the value of y?


a) True
b) None
c) 1
d) 0
e) False
Ans: (a)
2. Which of the following are truthy:

a) 0.00001
b) 0
c) True
d) []
e) False
f) ‘None’

Ans: (a),(c),(f)
3. Let a = 100, b = 200. What is the value of the expression a and b?

a) 0
b) 100
c) True
d) False
e) 200

Ans: (e)
4. The function sqrt() from the math module computes the square root of a number.

Will the highlighted line of code raise an exception?

a) Yes
b) No

Ans: (b)
5. For two objects x and y:
x is y is True if and only if id(x) == id(y).

a) True
b) False

Ans: (a)
I hope you guys are still alive!

You might also like