We started the morning off with this article.
False are also integers 1 and 0:
This means that can be used to count things with
Python uses shortcutting when evaluating logical expressions, so if it already knows the answer, the other statements will never be executed.
is operator validates the identity of an object.
in operator validates the membership of an object.
You can chain comparisons, but sometimes the output isn’t what you would expect.
Apparently this is because Python is going to break this statement down into 2 different statements:
a is ais True
a < 1is True
True and True is
True ? I really can’t figure this rule out, usually I’d just rely on parentheses … until I found this fantastic article:
Under the Hood: Python Comparison Breakdown
When it solidified the fact that this must be how the interpreter is doing it’s job:
In the Repl I actually used the disassembler
to see how this function gets decomposed, and it starts to make more sense. The use of
allow you to consider that the “join” of the two comparisons isn’t so much of an “and-ing” of the two comparisons, but
simply that the first comparison is simply not false.
Anyways we go on to learn that
None is a special keyword in Python to
mean the “lack of value”. It’s pretty clever and evaluates to
False, but does not equal
Functions are also always truthy, which has the odd side effect that if you forget to put parentheses at the end of the
name of a function, it will silently always return
all() are sort of like
or except they don’t return their last value that’s been evaluated.
any() , like
True if any of its arguments are truthy.
all() , like
True if all of its arguments are truthy.
Lists and Tuples
- are ordered.
- can contain any arbitrary objects.
- List elements can be accessed by index.
- can be nested to arbitrary depth.
- are mutable.
- are dynamic.
Tuples are the same except:
- are defined by enclosing the elements in parentheses (()) instead of square brackets ().
- are immutable.
- can be packed and unpacked.
There’s some discussion on when to use each. The consensus seems to be that you should use Tuples when you know the values inside the container aren’t going to change, and you’d like to iterate over them repeatedly. Similarly, you can use Tuples as dictionary keys, which is sorta nice because a Tuple can be a sort of composite container of information.