Advanced Python Course Lecture Notes

Jun 30, 2024

Advanced Python Course Lecture Notes

Instructor: Patrick

Introduction

  • Advanced Python course by Patrick.
  • For intermediates, not for beginners.
  • Covers advanced topics to improve Python skills.

Lists

  • Definition: Ordered, mutable, and allows duplicates.
  • Creating Lists:
    • Square brackets my_list = ["banana", "cherry", "apple"]
    • Empty list with list() function.
  • Manipulation:
    • Access: item = my_list[index] (0-based, supports negative indices).
    • Iterate: for item in my_list: print(item).
    • Membership: if "banana" in my_list:.
    • Methods: append(), insert(), pop(), remove(), clear(), reverse(), sort(), sorted().
  • Slicing:
    • sub_list = my_list[start:stop] (stop is exclusive).
    • Steps and negative indices are supported.
  • Copying:
    • Assignment vs. deep copy: .copy(), list(), slicing [:].
  • Comprehension:
    • Simple and clean syntax for creating lists: [expression for item in iterable].

Tuples

  • Definition: Ordered and immutable.
  • Creating Tuples:
    • Parentheses my_tuple = ("Max", 28, "Boston").
    • Single element tuple needs trailing comma.
  • Manipulation:
    • Access: item = my_tuple[index] (supports negative indices).
    • Methods: count(), index().
    • Conversion between list and tuple: list(my_tuple), tuple(my_list).
    • Slicing and unpacking: Similar to lists.
  • Use Cases: Efficient due to immutability, used with large data.

Dictionaries

  • Definition: Unordered, mutable collection of key-value pairs.
  • Creating Dictionaries:
    • Braces: my_dict = {"name": "Max", "age": 28}.
    • dict() function without quotes for keys.
  • Manipulation:
    • Access: value = my_dict[key].
    • Methods: pop(), popitem(), del my_dict[key], clear(), keys(), values(), items().
    • Membership: if key in my_dict:.
  • Copying:
    • Assignment vs. deep copy: .copy(), dict().
  • Merging: update() method merges dictionaries.
  • Key Types: Strings, numbers, tuples (immutable).

Sets

  • Definition: Unordered, mutable, no duplicates.
  • Creating Sets:
    • Braces {1, 2, 3}, set() function.
    • Empty set with set() only.
  • Manipulation:
    • Access: No indexing, similar methods to lists.
    • Methods: add(), remove(), discard(), clear(), pop(), union(), intersection(), difference(), symmetric_difference().
  • Copying:
    • Assignment vs deep copy: .copy(), set().
  • Special Sets: frozenset immutable version.

Strings

  • Definition: Ordered, immutable, used for text.
  • Creating Strings:
    • Single, double, or triple quotes for multi-line strings.
  • Manipulation:
    • Access: char = my_string[index] (supports negative indices).
    • Slicing, concatenation, membership: Similar to lists.
    • Methods: strip(), upper(), lower(), find(), count(), replace().
    • Conversion: split(), join() with lists.
  • Formatting:
    • Old %, str.format(), f-strings for Python 3.6+.

Collections Module

  • Special Data Types:
    • Counter: Counts elements frequency.
    • namedtuple: Lightweight object-like tuples.
    • OrderedDict: Remembers entry order.
    • defaultdict: Default factory for new keys.
    • deque: Double-ended queue.

Itertools Module

  • Iterators Functions:
    • product(), permutations(), combinations(), accumulate(), groupby().
    • Infinite iterators: count(), cycle(), repeat().

Lambda Functions

  • Small, anonymous functions: lambda args: expression.
  • Uses:
    • Combined with sorted(), map(), filter(), reduce().

Exceptions

  • Types: Syntax errors and exceptions.
  • Common Built-in Exceptions: ImportError, NameError, FileNotFoundError, ValueError, IndexError, KeyError.
  • Raising: raise Exception("message").
  • Handling: try...except, else, finally blocks.
  • Custom Exceptions: Subclassing Exception.

Logging**

  • Setup: import logging, logging.basicConfig().
  • Levels: debug, info, warning, error, critical.
  • Configuration: logging.conf, dictConfig.
  • Handlers: StreamHandler, FileHandler, RotatingFileHandler, SMTPHandler.
  • Advanced Techniques: Stack traces, rotating file handlers.

JSON Module

  • Usage: Encoding and decoding JSON.
  • Functions: json.dump(), json.dumps(), json.load(), json.loads().
  • Custom Encoding/Decoding: default(), object_hook with JSONEncoder.

Random Module

  • Pseudo-Random Numbers: random(), uniform(), randint(), choice(), shuffle(), seed().
  • Cryptographically Strong Random Numbers: secrets module.

Decorators**

  • Function and Class Decorators: Extend functionality.
  • Syntax: @decorator_name.
  • Common Use Cases: Timing, debugging, authentication, caching.
  • Implementation:
    • Function decorators: Uses *args, **kwargs.
    • Class decorators: Implements __call__.

Generators

  • Lazy Iterators: yield keyword.
  • Usage: next(), for loops, sum(), sorted() on generator.
  • Use Cases: Memory efficient for large datasets.

Threading and Multiprocessing

  • Threading: Lightweight, for I/O-bound tasks, limited by GIL.
  • Multiprocessing: Separate memory space, better for CPU-bound tasks.
  • Usage: Thread, Process, Pool, Queue from threading and multiprocessing modules.
  • Synchronization: Lock, Semaphore, Event.

Function Arguments

  • Types:
    • Positional, keyword, default, variable-length *args, **kwargs.
  • Unpacking:
    • Lists, tuples, dicts into function arguments.
  • Local vs Global Variables: Scope rules.
  • Passing By Reference/Value: Immutable vs mutable types.

Asterisk/Star Operator

  • Multiplication, Power Operations.
  • Creating Repeated Elements in Lists, Tuples, Strings.
  • Unpacking:
    • Arguments and containers.
    • Merging containers.

Copying**

  • Assignment: Reference, not a copy.
  • Shallow vs Deep Copy: copy.copy(), copy.deepcopy().
  • Custom Objects: Implementing __copy__, __deepcopy__ methods.

Context Managers

  • Usage: with statements for resource management (files, locks).
  • Implementation: Classes with __enter__, __exit__ or generators with contextmanager decorator.