If it is dark, turn on a light or fetch a torch (flashlight) or light a (candle, lamp). Namely, do something to bring some light to the situation before one stumbles and stubs a toe.
Else, well there is no else. The lights are on or it isn’t dark. What more need one do?
If True and False are still a mystery, then be sure to keep looping through these exercises until that is no longer the case. Don’t go forward until this is completely ingrained. Seriously.
We see above the logical operator, and
. One can only assume full comprehension of True
and False
is indicated when we see this. Further to that, we might assume an understanding of truth values, otherwise known as, truthy and falsy which refer to the evaluation of expressions which are bool
class when type queried.
>>> def is_perfect(x):
from math import sqrt as root
return not root(x) % 1
>>> is_perfect(4)
True
>>> is_perfect(9)
True
>>> is_perfect(25)
True
>>> is_perfect(36)
True
>>> is_perfect(32)
False
>>>
We needn’t get into what perfect squares are, only that the return
statement is not an explicit boolean primitive, but an evaluation that can then be cast to a boolean. Such an expression is for all an intents, a bool
type.
>>> n = -4
>>> type(-5 <= n < 5)
<class 'bool'>
>>> n = None
>>> type(-5 <= n < 5)
Traceback (most recent call last):
File "<pyshell#115>", line 1, in <module>
type(-5 <= n < 5)
TypeError: unorderable types: int() <= NoneType()
>>>
One for the record. None
has no type that it can relationally be compared to. Not even itself…
>>> None < None
Traceback (most recent call last):
File "<pyshell#116>", line 1, in <module>
None < None
TypeError: unorderable types: NoneType() < NoneType()
>>>
Identity is okay…
>>> None == None
True
>>>
Considering that and
is used above, are given awareness to the two operands and what’s going on there. It’s two expressions both being evaluated separately then jointly to yield a boolean.
But if the yield is a boolean, do we need literals (primitives) in our code? Answer: No, we don’t. The expression yield is sufficient to fill that need.
return num > num and num < num
The fact that you actually used num
in this response is totally commendable and we are happy to see it. Don’t leave this topic until you are completely comfortable. It’s crucial.