name = "Sabine"
print("My name is %s." % name)
My name is Sabine.
print("My name is {}.".format(name))
My name is Sabine.
name = "Sabine"
print(f"My name is {name}.")
My name is Sabine.
import decimal
width = 10
precision = 4
value = decimal.Decimal("12.34567")
f"result: {value:{width}.{precision}}" # nested fields
'result: 12.35'
hundred_million = 100_000_000
hundred_million
100000000
Image(filename="images/flask.png")
import os
os.urandom(24)
b'\xc9\x19C7\xb3\x8e.\xb7f\xd42\xa6\x85t\x86\xe0,\xc16\x19\xfb\x99\xe0n'
import secrets
secrets.token_bytes(32)
b'\x16\x1f\xe8H,e\x8ar*\xb1\x8d\x7f\x00U\xc5E\x844\xa0\xc8F\x08\x16\xc49\xcd5\x11\x191\r\xd3'
Always use the secrets module for cryptographical secure identifiers!
# always produces the same sequence 9, 0, 6
import random
random.seed(10)
x = random.randint(0, 9)
y = random.randint(0, 9)
z = random.randint(0, 9)
print(x, y, z)
9 0 6
import os
path = '/usr/lib'
type(path)
str
os.path.dirname(path)
'/usr'
Image(filename="images/path.png")
import pathlib
pathobject = pathlib.Path('/usr/lib')
type(pathobject)
pathlib.PosixPath
os.path.dirname(pathobject)
'/usr'
Image(filename="images/asyncio.png")
async def ticker(delay, to):
for i in range(to):
yield i
await asyncio.sleep(delay)
async def run():
starttime = datetime.datetime.now()
print(f"Start at second {starttime.second}")
async for i in ticker(5, 10):
outputtime = datetime.datetime.now()
print(f"output: {i} at second: {outputtime.second}")
import asyncio
import datetime
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
Start at second 53 output: 0 at second: 53 output: 1 at second: 58 output: 2 at second: 3 output: 3 at second: 8 output: 4 at second: 13 output: 5 at second: 18 output: 6 at second: 23 output: 7 at second: 28 output: 8 at second: 33 output: 9 at second: 38
Image(filename="images/Type_hints.png")
def add(x: int, y:int):
return x + y
add(3,4)
7
add.__annotations__
{'x': int, 'y': int}
Type annotations are now possible everywhere!
from typing import List, Dict
primes: List[int] = []
captain: str # Note: no initial value!
class Starship:
stats: Dict[str, int] = {}
s = Starship()
s.__annotations__
{'stats': typing.Dict[str, int]}
def orderpreserved(**kwargs):
print(list(kwargs.keys()))
orderpreserved(c=1,b=2,a=3)
['c', 'b', 'a']
class OrderPreserved:
c = 1
b = 2
def a(self):
pass
list(OrderPreserved.__dict__.keys())
['__module__', 'c', 'b', 'a', '__dict__', '__weakref__', '__doc__']
Version | DictSize | Dict Ordering |
---|---|---|
Python 2.7 | 280,280,280 | Scrambled (predictable) |
Python 3.5 | 196,196,196 | Randomized (unpredictable): key-sharing dictionary |
Python 3.6 | 112,112,112 | Ordered |
Image(filename="images/pydict.png")