Built in Function Review 19/19


#1

def distance_from_zero(numero):
if type(numero) == int or type(numero) == float:
return abs(numero)
else:
return “Nope”

How does python read if when numero is boolean it returns the absolute value for the integer equivalent of the boolean argument? Just curious? The correct code for the exercise is above.

ie. for the code below [distance_from_zero(True)] when called would return 1

def distance_from_zero(numero):
if type(numero) == int or float
return abs(numero)
else:
return “Nope”


#2
>>> abs(True)
1
>>> abs(False)
0
>>> 

Then consider,

>>> print (float)
<class 'float'>
>>> print (float and True)
True

So, float is truthy and when we write an OR expression composed of a condition as one operand, and a truthy as the other, the expression will always yield True.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.