Adding underscores to numbers

num1 = 10000000000
num2 = 90000000

sum1 = num1 + num2

print(sum1)
10090000000
num1 = 10_000_000_000
num2 = 90_000_000

sum2 = num1 + num2

print(sum2)
10090000000
sum1 == sum2
True

More readability

print(f'{sum2:,}')

Note that the type of the output gets changed, obviously

print(type(f'{sum2:,}'))
<class 'str'>

Use context manager to manage file handling

f = open('myfile.csv', 'r')
file_contents = f.read()
f.close()

Try a context manager instead. This way you don't have to remember to close the file handle

with open('myfile.csv', 'r') as f:
    file_contents = f.read()

The above concept of context managers is applicable not only to files, but to any other resource you might use such as threads.

Using the enumerate function

words = ['A', 'and', 'B', 'play', 'God of war', 'together']

index = 1
for word in words:
    print(index, word)
    index += 1
1 A
2 and
3 B
4 play
5 God of war
6 together

There's a better way to do this

for ix, word in enumerate(words, start=1):
    print(ix, word)
1 A
2 and
3 B
4 play
5 God of war
6 together

Using the ZIP function

persons = ['Bruce Wayne', 'Clark Kent', 'Peter Parker']
heroes = ['Batman', 'Superman', 'Spiderman']
universes = ['DC', 'DC', 'Marvel']

for person, hero, universe in zip(persons, heroes, universes):
    print(f'{hero} is actually {person} created by {universe}')
Batman is actually Bruce Wayne created by DC
Superman is actually Clark Kent created by DC
Spiderman is actually Peter Parker created by Marvel

Quick note about unpacking values

The following two lines of code result in a ValueError

a,b,c = (1,2,3,4,5)

a,b,c,d,e = (1,2,3)

Unpacking fewer variables

a, b, *c = (1,2,3,4,5)

print(f'a = {a}')
print(f'b = {b}')
print(f'c = {c}')
a = 1
b = 2
c = [3, 4, 5]

Accepting passwords

from getpass import getpass
username = input("Username: ")
password = getpass("Password: ")

print("Logging in...")
Logging in...

That's it for now!