Member-only story

36 Things I Didn’t Know About Python Until Recently (Compilation)

# Despite learning Python since 2017

Liu Zuo Lin
9 min readApr 7, 2023

1) We can make stuff run AFTER the return statement in a function

def test():
print('apple')
return 1
print('orange') # this won't ever be printed

^ in typical functions, NOTHING happens after a return statement is executed. The print('orange') line won’t ever execute because it happens after a return statement.

def test():
try:
print('apple')
return 1
finally:
print('orange') # this will run even after a return statement

^ however, if 1) we use a try-finally block 2) the return statement is inside the try/except block, the code inside the finally block will run even after the return statement.

2) we can use .update() to combine 2 dicts/sets

dict1 = {'apple':4, 'orange':5}
dict2 = {'pear': 6}
dict1.update(dict2)

# dict1 is now {'apple':4, 'orange':5, 'pear':6}
set1 = {'apple', 'orange'}
set2 = {'pear'}
set1.update(set2)

# set1 is now {'apple', 'orange', 'pear'}

3) int() evaluates to 0

x = int()
print(x) # 0

--

--

Liu Zuo Lin
Liu Zuo Lin

Written by Liu Zuo Lin

SWE @ Meta | [Ebook] 101 Things I Never Knew About Python: https://payhip.com/b/vywcf

Responses (13)