Python Fundamentals

Python from the ground up, traced one line at a time: names and objects, strings, loops, lists and dictionaries, functions, classes, errors and generators.

Python · beginner · 28 lessons · 3 h 19 min · free

What you'll learn

Lessons

Python Basics

Names and objects, numbers, text, and true or false.

  1. Variables: Names for Objects: In Python a variable is a name stuck onto an object. Watch names get created, move to new objects, and share one object.
  2. Numbers and Arithmetic: Integers, floats and every arithmetic operator, including the three kinds of division people mix up.
  3. Strings: Indexing and Slicing Text: Pick out characters by position, cut out slices, and see why string methods return new strings instead of changing the old one.
  4. String Methods: strip, split, join, replace: The everyday tools for cleaning and reshaping text, each one returning a new string or list.
  5. f-strings: Putting Values into Text: Put variables and expressions straight into text, and control decimals and alignment with format specs.
  6. Booleans, Comparisons and Logic: True and False, comparison operators, and/or/not, chained comparisons, and which values count as false.

Control Flow

Branching with if, and repeating with while and for.

  1. if, elif and else: Choose between several branches. Python checks each condition in order and runs only the first one that is true.
  2. while Loops: Repeat while a condition stays true. Watch the condition being checked before every round, and the loop stop once it turns false.
  3. for Loops, range and enumerate: Repeat once per item: numbers from range, counting down with a step, and getting both position and item with enumerate.
  4. break and continue: Skip one round with continue, or leave the loop early with break, on a list of sensor readings.
  5. Nested Loops: A loop inside a loop: for every row, the inner loop runs all the way through. Build a 3 × 3 times table.

Collections

Lists, tuples, dictionaries and sets, and what assignment really does to them.

  1. Lists: Adding, Removing and Sorting: A list is an ordered, changeable collection. Use a queue of people to try append, insert, pop, remove and sort.
  2. Slicing Lists: Take parts of a list with [start:stop:step], reverse it, and copy it so changes do not leak back.
  3. Two Names, One List: Assigning a list to a second name does not copy it. Watch two names share one list, then see how copy() makes a separate one.
  4. Tuples and Unpacking: Tuples are fixed-size, unchangeable groups. Unpack them into names, swap two values in one line, and collect "the rest" with a star.
  5. Dictionaries: Keys and Values: Store values under keys: add, update, look up safely with get, delete, and loop over pairs with items().
  6. Sets: Unique Items: Sets keep each value once. Remove duplicates in one step and compare groups with &, | and -.
  7. List and Dict Comprehensions: Build a list in one readable line. Compare a loop with its comprehension, then add a filter, and build a dictionary the same way.

Functions

Defining and calling functions, arguments, scope, recursion and lambdas.

  1. Functions: Define, Call, Return: Package code under a name, call it with arguments, and get a value back. Watch a new frame appear for each call.
  2. Default and Keyword Arguments: Give parameters default values, then call the same function four ways: positions, defaults, and names.
  3. Scope: Local and Global Names: Names made inside a function live only during the call. Read a global from inside a function, and change one with the global keyword.
  4. Recursion: Functions That Call Themselves: factorial(4) calls factorial(3), which calls factorial(2)… Watch the frames stack up until the base case, then unwind with the answers.
  5. lambda, map, filter and sorted: Small throwaway functions for sorting by a key, changing every item, and keeping some items.

Objects, Errors and Generators

Classes, inheritance, special methods, exceptions and generators.

  1. Classes and Objects: A class is a blueprint; objects are made from it, each with its own data. Build two bank accounts and see self in action.
  2. Inheritance and super(): A Dog is an Animal; a Puppy is a Dog. Child classes reuse the parent's code, override what differs, and extend it with super().
  3. Special Methods: __add__, __eq__, __repr__: Make your own objects work with +, == and print by defining double-underscore methods.
  4. Exceptions: try, except, finally and raise: Catch errors so the program keeps going, run clean-up code no matter what, and raise your own errors for bad input.
  5. Generators and yield: A generator hands out values one at a time and pauses in between. Watch it freeze at each yield and pick up exactly where it left off.

Related courses