Operators Complete
Operators Complete
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.
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.
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!