1. Use type hinting wherever possible to introduce strong typing and remove possibilities of the code being used incorrectly
# argument: argument type, def func(args) -> return type
def calculate_hypotenuse(x: float, y: float) -> float:
	return sqrt(x**2 + y**2)

Type hinting in Python is similar to mentioning the argument and return types in statically-typed languages like C++. The difference is, type hinting is optional in Python. The worst that happens when you write the wrong type hints is that you get warnings from linter and type checker programs like mypy

  1. Use dataclasses for strongly typed interfacing and to show what the function is actually returning (both types and nature of content)
# dataclasses decorator @
 
@dataclasses.dataclass
class Person:    
	name: str
	age: int
	employed: bool
 
def find_person(...) -> Person:
	...
  1. You can use typing.Union() or dataclass1 | dataclass2 | ... to unionize different types to enable efficient pattern matching
  2. Enclose an integer literal in () when using methods directly on it, in order to avoid a SyntaxError. This is because Python will interpret 42.as_integer_ratio() as a decimal given the point, resulting in an error. The right way to do this is (42).as_integer_ratio()
  3. num.hex() will convert num to hex. To convert a hexadecimal to a float, use fromhex()
  4. To center a string by padding it with a specified character, use center(width[, fillchar]). Width is the final length of the string and fillchar is the character you want to pad the string with
  5. itertools.groupby() groups adjacent elements together
def tabs_to_spaces(s: str):
    i = 0
    sl = list(s)
    original_length = len(sl)
    while i < len(s):
        if sl[i] == '\t':
            spaces = 8 - (i - (8*(i//8)))
            sl.pop(i)
            for j in range(spaces):
                sl.insert(i+j, ' ')
 
            i += spaces
        else:
            i += 1
 
    print(''.join(sl))
    print(s.expandtabs())