This is a notebook that is a collection of 'cool things'.
Either Language or Library features demonstrated in isolation, or 'clever' algorithms with better explanations.
Introduced in Python 3.6, f-strings are now the simplest and fastest way to print values in interesting formats;
val = 'world'
print(f'hello {val}')
hello world
However, considering that the f-string effectively puts the entire kernel at your disposal, becareful not to get too clever, otherwise you end up with unreadable messes
print(f'Hello {input("What is your name?")}!')
Hello Andrew!
Introduced in Python 3.8, a new language construct was added; the walrus operator; :=
To understand why this is cool; start off with the case where it doesn't exist.
For instance, we have some basic conditional logic; if a list is too long to be processed, complain about it
def check_input(a):
if len(a) > 10:
raise ValueError(f'List is too long; got {len(a)}, expected < 10')
else:
return a
check_input(range(100))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-5f2ed2d07aed> in <module> 5 return a 6 ----> 7 check_input(range(100)) 8 <ipython-input-3-5f2ed2d07aed> in check_input(a) 1 def check_input(a): 2 if len(a) > 10: ----> 3 raise ValueError(f'List is too long; got {len(a)}, expected < 10') 4 else: 5 return a ValueError: List is too long; got 100, expected < 10
But we're calling len
twice when we really don't have to (and len
can have some nasty side effects on some types of iterators)
The walrus operator allows us to 'grab' the value of the output of len
while also comparing it; like this
def check_input(a):
a_len = len(a)
if a_len > 10:
raise ValueError(f'List is too long; got {a_len}, expected < 10')
else:
return a
check_input(range(100))
def check_input(a):
if (a_len :=len(a)) > 10:
raise ValueError(f'List is too long; got {a_len}, expected < 10')
else:
return a
check_input(range(100))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-4-a2c3c04f8116> in <module> 5 return a 6 ----> 7 check_input(range(100)) <ipython-input-4-a2c3c04f8116> in check_input(a) 1 def check_input(a): 2 if (a_len :=len(a)) > 10: ----> 3 raise ValueError(f'List is too long; got {a_len}, expected < 10') 4 else: 5 return a ValueError: List is too long; got 100, expected < 10
However, be careful with your brackets
def check_input(a):
if (a_len :=len(a) > 10):
raise ValueError(f'List is too long; got {a_len}, expected < 10')
else:
return a
check_input(range(100))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-7-02a3c4512b45> in <module> 5 return a 6 ----> 7 check_input(range(100)) <ipython-input-7-02a3c4512b45> in check_input(a) 1 def check_input(a): 2 if (a_len :=len(a) > 10): ----> 3 raise ValueError(f'List is too long; got {a_len}, expected < 10') 4 else: 5 return a ValueError: List is too long; got True, expected < 10