cookbook

Python

Strings

Format numbers

x = 3.14159265
print(f'pi = {x:.2f}')
print( 'pi = %0.2f' % x)
ten = 10
print(f'x = {x:.2f}')
print('x = %0.2f' % x)

Date and Time

Create datetime with Specific Offset

Create a timezone object which you can pass to datetime() or datetime.now().

from datetime import datetime, timezone, timedelta

# offset is in seconds
def make_tz(offset):
    return timezone(timedelta(seconds=offset))

# UTC -400
x = -4 * 60 * 60
datetime.now(tzinfo=make_tz(x))

Language

Magic Methods

Tidbits