Python tip 3: expression and result with f-string
In case you haven't yet discovered this awesome f-string feature, you can add =
after an expression to get both the expression and the result in the output.
>>> num1 = 42
>>> num2 = 7
>>> f'{num1 + num2 = }'
'num1 + num2 = 49'
>>> f'{num1 + (num2 * 10) = }'
'num1 + (num2 * 10) = 112'
I use it often to quickly test a function:
>>> def isodd(n):
... return bool(n % 2)
...
>>> print(f'{isodd(42) = }')
isodd(42) = False
>>> print(f'{isodd(123) = }')
isodd(123) = True
See docs.python: Format String Syntax, docs.python: Formatted string literals and fstring.help for documentation and examples.
Video demo:
See my 100 Page Python Intro ebook for a short, introductory guide for the Python programming language.