Introduction

Start in JupyterHub

Python is not designed to produce fast programs, but to produce programs fast.

Justus Sebastian Alexander Michael Henneberg (2020)

We use Python 3.10. We assume basic knowledge of Python. Make sure you have at least some version of Python 3 installed!

Possible IDEs:

  • Visual Studio Code (recommend)

  • PyCharm Community/Professional

  • vim/emacs (for users with prior experience)

  • Jupyter Notebook/Jupypter Hub (popular in Data Science for reports)

  • Command line and text editor (cumbersome, no immediate feedback on code quality)

Workflow overview:

  • You are introduced to a problem

  • Think about how you would solve it using prior knowledge!

  • Identify potential problems in your solution (verbosity can be problematic, too)!

  • You are presented a new language feature or approach for solving the problem

  • We implement a better solution using the newly gained insights

Remember: Get out of your comfort zone, try new approaches! There is no wrong solution to a problem. There are, however, solutions that are more likely to backfire eventually.

If you find any mistakes, please report them with the „Open Issue“-Button.

Java: Is dividing by 7 too expensive? You probably do not want to deal with this level of detail when programming.

// Inexpensive approximation of length / 7
int seventh = (length >> 3) + (length >> 6) + 1;

https://github.com/AdoptOpenJDK/openjdk-jdk8u/blob/master/jdk/src/share/classes/java/util/DualPivotQuicksort.java#L1695

Motivating Examples

Shortest Pairwise Distance

from math import dist

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return f"({self.x}, {self.y})"
    
    def __repr__(self):
        return f"Point({self.x}, {self.y})"

    def dist(self, other):
        return dist((self.x, self.y), (other.x, other.y))
# naive solution
def min_dist(points):
    if len(points) < 2:
        raise ValueError
    md = points[0].dist(points[1])
    result = points[0], points[1]
    for i in range(len(points)):
        for j in range(i):
            p, q = points[i], points[j]
            if p.dist(q) < md:
                md = p.dist(q)
                result = p, q
    return result
from itertools import combinations

def min_dist(points):
    return min(combinations(points, 2), key=lambda x: Point.dist(*x))
p = [Point(2, 3), Point(1, 2), Point(6, 1), Point(5, -1), Point(5, 3), Point(-1, 7)]
min_dist(p)
(Point(2, 3), Point(1, 2))

Estimation of \(\pi\) using the Monte Carlo Method

from random import uniform

num_samples = 10_000_000

s = 0
for i in range(num_samples):
    if uniform(0, 1) ** 2 + uniform(0, 1) ** 2 < 1:
        s += 1
my_pi = s * 4 / num_samples

print(my_pi)
import numpy as np

num_samples = 10_000_000

random_x = np.random.uniform(low=0, high=1, size=num_samples)
random_y = np.random.uniform(low=0, high=1, size=num_samples)
in_circle = random_x ** 2 + random_y ** 2 < 1
my_pi = in_circle.sum() * 4 / num_samples

print(my_pi)
import numpy as np

num_samples = 10_000_000

# using array shapes instead
random = np.random.uniform(low=0, high=1, size=(num_samples, 2))
my_pi = np.sum(np.sum(random ** 2, axis=1) < 1) * 4 / num_samples

print(my_pi)

Fetching the current Python version

import requests
import re

response = requests.get("https://docs.python.org/")
response.raise_for_status()
version = re.search(r"<h1>Python (?P<ver>[a-z0-9.]+) documentation</h1>", response.text).group('ver')
print(version)
import sys

# currently installed version
print(sys.version)