Python tricks

format numbers in code

x = 10_000_000

underscores are ignored.

format output

total = 1000 * 1000
print(f"{total:,}")

outputs

1,000,000

loop count

use enumerate to get the index of the loop.

for index, item in enumerate(items):
    print(index, item)

links

social