Skip to content
academia.sh

Course Beginner

Python Fundamentals

By the end of this course

Start course

01

Language Fundamentals

The interpreter and bytecode, indentation's grammatical role, name binding and object identity, the special methods operators call, string formatting, and type conversion's results tied to dynamic typing.

  1. 01 Python's Execution Model The source is compiled as a whole before it is executed; syntax is a shorthand — eleven forms call 12 distinct special methods 18 times in total, and a loop over a three-item object calls `__next__` one more time than the item count, 4 times.
  2. 02 Syntax and Indentation Indentation is not a style preference but part of the grammar: the same 21 content tokens produce two different trees and two different results under two different indentations, four different widths give a single tree, and only 5 of ten parts are expressions.
  3. 03 Variables and Data Types A name carries no type, the object does: a single name binds to seven different types without a declaration, the `+=` notation rebinds the name in three of four types and mutates the object in one, and whether two equal values turn out to be the same object depends on the implementation.
  4. 04 Operators Eleven operator forms call 10 distinct special methods and one goes unmatched: `n + m` calls `__add__`, `n += m` calls `__iadd__`; if `x in n` cannot find `__contains__`, it falls back to the iteration protocol and pays five calls instead of one.
  5. 05 Working with Strings Because a string is immutable, every producing method returns a new object: joining seven parts with `+` builds 7 intermediate objects, `join` builds 1, and eight formatting notations call 3 distinct special methods.
  6. 06 Type Conversion Conversion built-ins call distinct special methods across eight notations, and two notations with no counterpart go unmatched; twenty string attempts split into 7 `ValueError` and 4 `TypeError`, and numeric promotion is shown not to be magic through `NotImplemented`.

02

Control Flow and Functions

Comparison chains and truthiness rules, loops through the iteration protocol, positional and keyword arguments, the four scope levels, and the contract of built-ins.

  1. 01 Conditionals A condition does not expect a logical value, it puts the object through the truthiness rule: eight protocol calls are measured across twelve trials, four return true without calling any method, and a comparison chain produces its middle term once.
  2. 02 Loops A loop is not a control structure, it is a protocol: on a three-item object, __next__ is called four times, the last one throws StopIteration, and what ends the loop is not a condition but that exception.
  3. 03 Function Definition A default value is evaluated once when the definition is read, not on every call: five calls make one evaluation, a mutable default accumulates five items across five calls, and four of six parameters can be given only one way.
  4. 04 Scope Rules Name lookup runs through four levels, and what decides which level it stops at is not where the name is read but where it is assigned: adding a single assignment line to a body turns the same read from a valid value into UnboundLocalError.
  5. 05 Built-in Functions Built-ins are not functions doing the work themselves, they are thin shells calling a protocol: eight built-ins produce twenty-one calls, every collector builds the same five-call sequence, and on an object defining no protocol, three return a value while five throw TypeError.

03

Error Handling and Files

The exception hierarchy and catching behavior, custom exception placement, text and binary file modes with encoding, the context manager protocol, and regular expressions.

  1. 01 Exceptions An exception is an instance of a class, and catching looks at the class lineage: eleven events and seven catch classes match in 28 of 77 pairs, Exception catches ten events and swallows three program defects, and BaseException catches all eleven and swallows the exit request too.
  2. 02 Custom Exceptions A custom exception class's place in the hierarchy decides who catches it: five classes and six catches match in 13 of 30 pairs, the domain root handles seven errors with a single name, the borrowed base catches only three of them, and writing the broad clause first runs the narrow clause zero times.
  3. 03 File Operations The same 41 bytes give a byte sequence of length 41 in binary mode, and a 34-character string in text mode with utf-8; a wrong encoding breaks at read time, not open time, and latin-1 never breaks at all, silently producing 41 wrong characters.
  4. 04 Context Managers The with block calls __enter__ on entry and __exit__ on exit; an exception-free block produces 2 calls, an exception-carrying one 3, and __exit__ sees the exception — when the return value is True, the count drops back to 2 because the exception does not escape.
  5. 05 Regular Expressions Two of the pattern's seven opening parentheses do not capture, leaving five numbered groups; the number is given by the opening parenthesis's order, a non-match is None rather than an exception, and the match object carries every group's text, span, and name.

Start typing to search.

↑↓ Esc navigate · open · close