Fast track for programmers

You already program, so you do not need to be told what a loop or a function is. This route skips the lessons that teach ideas you already know and keeps the ones that cover how Python does things differently: its truthiness rules, slicing, unpacking, comprehensions, argument passing, exceptions, classes, generators, decorators and tooling.

The full course is about 21.4 hours. This route keeps 52 lessons (about 14.7 hours) and lets you skim 20 (about 6.7 hours). Skipped lessons stay available, and you can open any of them at any time.

What is different in Python

Each row shows an idea you know from other languages, the Python way, and a short example with its real output.

Variables and types

Elsewhere: Variables are declared with a type and the type is fixed.

In Python: A variable is just a name for a value. The type belongs to the value, so the same name can refer to different types over time.

x = 5
x = "five"
print(type(x).__name__, isinstance(x, str))

Prints:

str True

Numbers

Elsewhere: Integers overflow, and / on integers divides as integers.

In Python: int has no size limit, / is always true division, // floors (towards minus infinity) and % follows the sign of the divisor.

print(2 ** 100, 7 / 2, 7 // 2, -7 // 2, -7 % 3)

Prints:

1267650600228229401496703205376 3.5 3 -4 2

Truthiness

Elsewhere: Conditions must be booleans.

In Python: Any value can be a condition: 0, None, empty text and empty containers are falsy. and/or return one of their operands, not just True or False.

print(bool([]), bool("0"), [] or "default", 0 and 5)

Prints:

False True default 0

Strings and slicing

Elsewhere: Strings are indexed one character at a time, and you build copies by hand.

In Python: Strings are immutable sequences. Slices [start:stop:step] work on every sequence, and negative indexes count from the end.

s = "python"
print(s[1:4], s[::-1], s[-2:])

Prints:

yth nohtyp on

Looping

Elsewhere: for (i = 0; i < n; i++) with an index variable.

In Python: for loops walk over items directly. Use enumerate when you need the position and zip to walk several sequences together.

for i, (letter, number) in enumerate(zip("ab", [10, 20])):
    print(i, letter, number)

Prints:

0 a 10
1 b 20

Tuples and unpacking

Elsewhere: Functions return one value, or you create a struct.

In Python: Tuples group values, and unpacking takes them apart in one line, including swapping and collecting the rest with *.

first, (second, *rest) = 1, (2, 3, 4)
print(first, second, rest)
a, b = 1, 2
a, b = b, a
print(a, b)

Prints:

1 2 [3, 4]
2 1

Dictionaries and sets

Elsewhere: Hash maps with a rigid API; sets from a library.

In Python: dict and set are built in and have literals. dict.get gives defaults, dicts keep insertion order, and sets support | & - operators.

counts = {}
for ch in "hello":
    counts[ch] = counts.get(ch, 0) + 1
print(counts, {1, 2} | {2, 3})

Prints:

{'h': 1, 'e': 1, 'l': 2, 'o': 1} {1, 2, 3}

Function arguments

Elsewhere: Overloads and optional parameters.

In Python: One function takes defaults, keyword arguments, *args and **kwargs. Default values are created once, when the function is defined.

def f(a, b=2, *args, **kw):
    return a, b, args, kw

print(f(1, 3, 4, x=5))

Prints:

(1, 3, (4,), {'x': 5})

Names, scope and mutation

Elsewhere: Pass by value or pass by reference.

In Python: Arguments are names bound to the same objects. Mutating the object is visible to the caller, but rebinding the name is not.

def add(items):
    items.append(1)
    items = [99]

mine = []
add(mine)
print(mine)

Prints:

[1]

Exceptions

Elsewhere: Check first, then act (look before you leap).

In Python: Python prefers to try the operation and handle the failure. try/except/else/finally is the full form; raise your own exceptions with built-in or custom classes.

try:
    value = int("12")
except ValueError:
    print("bad")
else:
    print("ok", value)
finally:
    print("done")

Prints:

ok 12
done

Resources and with

Elsewhere: try/finally or destructors to release resources.

In Python: A with statement guarantees clean-up for files, locks and your own context managers.

from io import StringIO

with StringIO("a\nb") as f:
    print([line.strip() for line in f])
print(f.closed)

Prints:

['a', 'b']
True

Classes

Elsewhere: Declared fields, access modifiers, getters and setters.

In Python: self is explicit, fields are created by assignment, and privacy is a naming convention (_name). Dunder methods such as __repr__ and __eq__ plug your class into the language.

class P:
    def __init__(self, x):
        self.x = x

    def __repr__(self):
        return f"P({self.x})"

    def __eq__(self, other):
        return self.x == other.x

print(P(1), P(1) == P(1))

Prints:

P(1) True

Dataclasses and properties

Elsewhere: Boilerplate constructors, equals and toString, plus getters and setters.

In Python: @dataclass writes the boilerplate for you, and @property turns a method into a validated attribute without changing how callers use it.

from dataclasses import dataclass

@dataclass(order=True)
class V:
    major: int
    minor: int = 0

print(V(1, 2) < V(1, 3), V(1))

Prints:

True V(major=1, minor=0)

Decorators

Elsewhere: Annotations, aspects or wrapper classes.

In Python: Functions are values, so a decorator is a function that takes a function and returns a new one. @name above a def applies it.

def log(func):
    def wrapper(*args):
        print("call", func.__name__)
        return func(*args)
    return wrapper

@log
def add(a, b):
    return a + b

print(add(1, 2))

Prints:

call add
3

Type hints

Elsewhere: Static typing checked by the compiler.

In Python: Type hints are optional documentation that tools such as mypy check. Python itself ignores them at runtime.

def f(x: int) -> str:
    return x

print(f("hi"))

Prints:

hi

Your route

One click marks the 20 skimmable lessons below as skipped (they show a ↷ on the course page). Lessons you have already completed are left alone, and nothing is deleted.

Show the 20 lessons that will be skipped

Or take the placement testBack to the course