{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Python Syntax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Variables and Basic Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1 Variable Definition and Memory Management\n", "\n", "The essence of Python variables:\n", "- Variables are references to objects (similar to pointers)\n", "- Variables themselves have no type; the type belongs to the object\n", "- Assignment actually establishes a reference relationship\n", "\n", "Memory management mechanism:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Example: Variable Reference\n", "a = 10 # Create an integer object 10, and variable a references it\n", "b = a # Variable b also references the same object\n", "a = 20 # Create a new object 20, a now references it, while b still references 10\n", "print(f\"a: {a}, b: {b}\")\n", "\n", "# View object memory addresses\n", "print(f\"id of a: {id(a)}, id of b: {id(b)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Detailed Data Types\n", "\n", "#### Numeric Types\n", "- Integer (int): Unbounded in size (limited only by memory)\n", "- Floating point (float): Double-precision float, note precision issues\n", "- Complex (complex): Contains a real and an imaginary part\n", "\n", "#### String (str)\n", "- Immutable sequence\n", "- Three forms of representation: single quotes, double quotes, triple quotes\n", "- Raw string (ignores escapes): r'...'\n", "\n", "#### Boolean (bool)\n", "- True and False\n", "- Actually a subclass of integer: True==1, False==0\n", "- Falsy values include: False, None, 0, \"\", [], {}, (), etc." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Numeric types example\n", "big_num = 999999999999999999999999999999999999999999999999999999999999\n", "print(0.1 + 0.2 == 0.3) # Floating point precision issue\n", "c = 3 + 4j\n", "print(f\"Complex number: {c}, Real part: {c.real}, Imaginary part: {c.imag}\")\n", "\n", "# String examples\n", "s1 = 'single quotes'\n", "s2 = \"double quotes\"\n", "s3 = \"\"\"multi-line\n", "string\"\"\"\n", "path = r'C:\\new_folder' # Raw string\n", "\n", "# Boolean type examples\n", "print(bool(\"\")) # Empty string is False\n", "print(bool([])) # Empty list is False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Comprehensive Analysis of Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1 Arithmetic Operators\n", "\n", "| Operator | Name | Description | Example | Result |\n", "|----------|------------|----------------------------|------------|--------|\n", "| `+` | Addition | Number addition / String concatenation | `3 + 2` | `5` |\n", "| `-` | Subtraction| Number subtraction | `5 - 3` | `2` |\n", "| `*` | Multiplication | Number multiplication / String repetition | `3 * 2` | `6` |\n", "| `/` | Division | Always returns a float | `6 / 3` | `2.0` |\n", "| `//` | Floor Division | Rounds down | `7 // 3` | `2` |\n", "| `%` | Modulus | Returns the remainder | `7 % 3` | `1` |\n", "| `**` | Exponentiation | Power calculation | `2 ** 3` | `8` |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Special behavior examples\n", "print(\"Hello\" + \"World\") # HelloWorld\n", "print(\"Hi\" * 3) # HiHiHi\n", "print(5 + 3.14) # 8.14" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2 Comparison Operators\n", "\n", "| Operator | Description | Example | Result |\n", "|----------|-----------------|-----------------|--------|\n", "| `==` | Equal to | `3 == '3'` | False |\n", "| `!=` | Not equal to | `5 != 3` | True |\n", "| `>` | Greater than | `5 > 3.5` | True |\n", "| `<` | Less than | `'a' < 'b'` | True |\n", "| `>=` | Greater than or equal to | `5 >= 5` | True |\n", "| `<=` | Less than or equal to | `3.0 <= 3` | True |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Chained comparison example\n", "x = 5\n", "print(1 < x < 10) # True\n", "print(3 <= x*2 <= 12) # True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 Logical Operators\n", "\n", "| Operator | Description | Example | Result |\n", "|----------|-------------|-------------------------|--------|\n", "| `and` | Logical AND | `True and False` | False |\n", "| `or` | Logical OR | `False or 3` | 3 |\n", "| `not` | Logical NOT | `not 0` | True |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Short-circuit behavior verification\n", "def check():\n", " print(\"Function called\")\n", " return True\n", "\n", "False and check() # No output\n", "True or check() # No output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 Bitwise Operators\n", "\n", "| Operator | Description | Example | Result (decimal) |\n", "|----------|----------------|---------------------|------------------|\n", "| `&` | Bitwise AND | `0b1100 & 0b1010` | 8 |\n", "| `|` | Bitwise OR | `0b1100 | 0b1010` | 14 |\n", "| `^` | Bitwise XOR | `0b1100 ^ 0b1010` | 6 |\n", "| `~` | Bitwise NOT | `~0b0011` | -4 |\n", "| `<<` | Left Shift | `5 << 2` | 20 |\n", "| `>>` | Right Shift | `16 >> 2` | 4 |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Bitwise operation application\n", "print(1 << 4) # 16\n", "num = 7\n", "print(num & 1) # 1 (odd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.5 Assignment Operators\n", "\n", "| Operator | Equivalent Expression | Example | Effect |\n", "|----------|-----------------------|---------------|-------------------|\n", "| `=` | - | `x = 5` | Basic assignment |\n", "| `+=` | `x = x + y` | `x += 3` | Incremental assignment |\n", "| `-=` | `x = x - y` | `x -= 2` | Decremental assignment |\n", "| `*=` | `x = x * y` | `x *= 1.5` | Multiplicative assignment |\n", "| `/=` | `x = x / y` | `x /= 2` | Floating point division assignment |\n", "| `//=` | `x = x // y` | `x //= 2` | Floor division assignment |\n", "| `%=` | `x = x % y` | `x %= 3` | Modulus assignment |\n", "| `**=` | `x = x ** y` | `x **= 2` | Exponentiation assignment |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Mutable object pitfall\n", "lst1 = [1,2]\n", "lst2 = lst1\n", "lst1 += [3]\n", "print(\"lst2:\", lst2) # [1, 2, 3]\n", "\n", "lst1 = lst1 + [4]\n", "print(\"lst2:\", lst2) # [1, 2, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.6 Membership and Identity Operators\n", "\n", "| Type | Operator | Description | Example | Result |\n", "|-------------------|-------------|------------------------------|------------------------------|--------|\n", "| Membership | `in` | Tests element membership | `3 in [1,2,3]` | True |\n", "| | `not in` | Tests element non-membership | `'a' not in 'xyz'` | True |\n", "| Identity | `is` | Tests if objects have same identity | `x = [1]; y = [1]; x is y` | False |\n", "| | `is not` | Tests if objects do not have same identity | `3 is not None` | True |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 256\n", "b = 256\n", "print(a is b) # True\n", "\n", "x = 257\n", "y = 257\n", "print(x is y) # False\n", "\n", "lst = [1,2,3]\n", "print([2] in lst) # False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.7 Operator Precedence\n", "\n", "| Priority | Operators |\n", "|----------|-------------------------------------|\n", "| 1 | `()` |\n", "| 2 | `**` |\n", "| 3 | `~` `+` `-` |\n", "| 4 | `*` `/` `%` `//` |\n", "| 5 | `+` `-` |\n", "| 6 | `<<` `>>` |\n", "| 7 | `&` |\n", "| 8 | `^` |\n", "| 9 | `|` |\n", "| 10 | `==` `!=` `>` `<` `>=` `<=` |\n", "| 11 | `is` `is not` |\n", "| 12 | `in` `not in` |\n", "| 13 | `not` |\n", "| 14 | `and` |\n", "| 15 | `or` |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = 3 + 4 * 2 ** 2 // (1 + 1)\n", "print(result) # 11" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comprehensive Exercises\n", "**Exercise 1**: Use bitwise operations to swap two variables\n", "**Exercise 2**: Determine if a year is a leap year (divisible by 4 but not by 100, or divisible by 400)\n", "**Exercise 3**: Calculate `(True or False) and (not (5 <= 3))`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Answer for Exercise 1\n", "a = 5\n", "b = 3\n", "a ^= b\n", "b ^= a\n", "a ^= b\n", "print(f\"a={a}, b={b}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Answer for Exercise 2\n", "year = 2024\n", "is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n", "print(f\"{year} is a leap year: {is_leap}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Answer for Exercise 3\n", "print((True or False) and (not (5 <= 3))) # True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. In-Depth Analysis of Control Flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.1 Detailed Conditional Expressions\n", "\n", "#### The Essence of Conditional Statements\n", "Python's conditional control structure branches based on Boolean logic. Its core features include:\n", "- **Indentation defines code blocks**: Python uses strict indentation (typically 4 spaces) to delineate code blocks\n", "- **Flexible expressions**: The condition can be a comparison, membership test, identity test, or a Boolean combination\n", "\n", "#### Multi-branch Decision Patterns\n", "```python\n", "if condition1:\n", " # Executed when condition1 is True\n", "elif condition2:\n", " # Executed when condition1 is False and condition2 is True\n", "else:\n", " # Executed when all conditions are False\n", "```\n", "\n", "#### Pattern Matching (Python 3.10+)\n", "Structured pattern matching is clearer than traditional if-elif-else, suitable for:\n", "- Handling complex data structures\n", "- Implementing state machines\n", "- Parsing command-line arguments" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Pattern matching example\n", "def handle_http_status(status):\n", " match status:\n", " case 200:\n", " return \"Success\"\n", " case 404:\n", " return \"Not Found\"\n", " case 500 | 502 | 503: # Multi-value matching\n", " return \"Server Error\"\n", " case _ if 400 <= status < 500: # Conditional matching\n", " return \"Client Error\"\n", " case _:\n", " return \"Unknown Status Code\"\n", "\n", "print(handle_http_status(404)) # Output: \"Not Found\"\n", "print(handle_http_status(403)) # Output: \"Client Error\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2 Advanced Loop Control\n", "\n", "#### Types of Loop Structures\n", "Python provides two loop structures:\n", "1. **while loop**: Repeats based on a condition\n", "2. **for loop**: Iterates over a sequence or iterable\n", "\n", "#### Loop Control Statements\n", "| Statement | Function |\n", "|------------|---------------------------------------|\n", "| `break` | Immediately exits the entire loop |\n", "| `continue` | Skips the current iteration, moves to the next |\n", "| `else` | Executed when the loop terminates normally (not via break) |\n", "\n", "#### Efficient Iteration Techniques\n", "- `enumerate`: Retrieve both index and value simultaneously\n", "- `zip`: Parallel iteration over multiple sequences\n", "- The `itertools` module: Provides advanced iteration tools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Advanced loop usage example\n", "names = ['Alice', 'Bob', 'Charlie']\n", "scores = [85, 92, 78]\n", "\n", "# Using enumerate and zip\n", "for idx, (name, score) in enumerate(zip(names, scores), start=1):\n", " print(f\"{idx}. {name}: {score}\")\n", "else:\n", " print(\"Score tally complete\")\n", "\n", "# Custom iterable object\n", "class CountDown:\n", " def __init__(self, start):\n", " self.current = start\n", " \n", " def __iter__(self):\n", " return self\n", " \n", " def __next__(self):\n", " if self.current <= 0:\n", " raise StopIteration\n", " num = self.current\n", " self.current -= 1\n", " return num\n", "\n", "print(\"Countdown:\")\n", "for num in CountDown(3):\n", " print(num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loop Performance Optimization\n", "1. Avoid unnecessary computations inside the loop\n", "2. Use generator expressions instead of list comprehensions for large data sets\n", "3. Consider using built-in functions like `map()`, `filter()`\n", "\n", "#### Exception Handling in Loops\n", "Properly handling exceptions within loops ensures program robustness:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Loop exception handling example\n", "urls = ['https://example.com', 'invalid_url', 'https://example.org']\n", "\n", "for url in urls:\n", " try:\n", " print(f\"Processing: {url}\")\n", " if 'invalid' in url:\n", " raise ValueError(\"Invalid URL\")\n", " # Simulate successful processing\n", " print(f\"Successfully processed {url}\")\n", " except ValueError as e:\n", " print(f\"Skipping {url}, error: {e}\")\n", " continue\n", " except Exception as e:\n", " print(f\"Critical error: {e}\")\n", " break\n", " else:\n", " print(f\"{url} processed successfully\")\n", " finally:\n", " print(\"Cleaning up temporary resources\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Looping and Functional Programming\n", "Python supports a functional programming style to some extent:\n", "- `map()`: Applies a function to an iterable\n", "- `filter()`: Filters elements\n", "- `functools.reduce()`: Performs cumulative computation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from functools import reduce\n", "\n", "# Functional programming example\n", "numbers = [1, 2, 3, 4, 5]\n", "\n", "# map example\n", "squared = list(map(lambda x: x**2, numbers))\n", "print(f\"Squares: {squared}\")\n", "\n", "# filter example\n", "evens = list(filter(lambda x: x % 2 == 0, numbers))\n", "print(f\"Even numbers: {evens}\")\n", "\n", "# reduce example\n", "product = reduce(lambda x, y: x * y, numbers)\n", "print(f\"Product: {product}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Best Practices for Control Flow\n", "1. Avoid deeply nested structures (generally no more than 3 levels)\n", "2. Extract complex conditions into variables or functions\n", "3. Use generators for processing large data sets\n", "4. Prefer for loops over while loops when the number of iterations is known\n", "5. Use the `itertools` module appropriately to improve efficiency" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Comprehensive control flow example\n", "import itertools\n", "\n", "def process_data(data):\n", " \"\"\"An example of best practices in data processing\"\"\"\n", " # Conditional check\n", " if not data:\n", " print(\"Warning: Empty dataset\")\n", " return []\n", " \n", " # Using a generator expression\n", " filtered = (x for x in data if x > 0)\n", " \n", " # Limit the number of items processed using itertools.islice\n", " results = []\n", " for idx, value in enumerate(itertools.islice(filtered, 100)):\n", " try:\n", " # Complex processing logic\n", " result = value ** 0.5\n", " if result > 10:\n", " continue # Skip values that are too high\n", " results.append(result)\n", " except Exception as e:\n", " print(f\"Processing error: {e}\")\n", " \n", " return results\n", "\n", "data = [1, 4, -5, 9, 16, 0, 25, -1, 36]\n", "print(\"Processing results:\", process_data(data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Control Flow Exercises\n", "\n", "**Exercise 1**: Implement a simple command-line parser using pattern matching\n", "\n", "**Exercise 2**: Implement the Fibonacci sequence using a generator\n", "\n", "**Exercise 3**: Use itertools.groupby to perform grouped statistics on data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 1: Command-line parser\n", "def parse_command(command):\n", " match command.split():\n", " case [\"exit\"]:\n", " print(\"Exiting program\")\n", " case [\"load\", filename]:\n", " print(f\"Loading file: {filename}\")\n", " case [\"save\", filename] if '.' in filename:\n", " print(f\"Saving file: {filename}\")\n", " case _:\n", " print(\"Unknown command\")\n", "\n", "# Test your code here\n", "parse_command(\"load data.txt\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 2: Fibonacci generator\n", "def fibonacci(limit):\n", " a, b = 0, 1\n", " while a < limit:\n", " yield a\n", " a, b = b, a + b\n", "\n", "# Test your code here\n", "print(\"Fibonacci sequence:\", list(fibonacci(100)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 3: Data grouping\n", "from itertools import groupby\n", "\n", "data = [('A', 85), ('B', 92), ('A', 78), ('C', 90), ('B', 88)]\n", "\n", "# Group by letter and calculate average score\n", "# Hint: You need to sort before grouping\n", "# Write your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Advanced Features of Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.1 List Comprehensions and Generators\n", "\n", "List comprehension:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "squares = [x**2 for x in range(10) if x % 2 == 0]\n", "print(squares)\n", "\n", "# Generator expression\n", "sum_of_squares = sum(x**2 for x in range(1000000))\n", "print(f\"Sum of squares for the first 1000000 numbers: {sum_of_squares}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2 Advanced Dictionary Operations\n", "\n", "Dictionary comprehension:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square_dict = {x: x**2 for x in range(5)}\n", "print(square_dict)\n", "\n", "# setdefault() method\n", "d = {}\n", "for k, v in [('a', 1), ('b', 2), ('a', 3)]:\n", " d.setdefault(k, []).append(v)\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.1 List Operations\n", "\n", "Lists are one of the most commonly used data structures in Python, characterized by:\n", "- **Ordered collection**: Elements are stored in the order of insertion\n", "- **Mutability**: Contents can be modified\n", "- **Heterogeneous elements**: Can contain elements of different types\n", "- **Index support**: Supports both positive and negative indexing (-1 represents the last element)\n", "\n", "#### Common List Methods:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Basic list operations example\n", "fruits = ['apple', 'banana']\n", "\n", "# Adding elements\n", "fruits.append('orange') # Append to the end\n", "fruits.insert(1, 'pear') # Insert at index 1\n", "print(\"After adding:\", fruits) # ['apple', 'pear', 'banana', 'orange']\n", "\n", "# Removing elements\n", "fruits.remove('banana') # Remove first matching element\n", "popped = fruits.pop(2) # Remove and return element at index 2\n", "print(f\"After removal: {fruits}, Popped element: {popped}\")\n", "\n", "# Sorting and reversing\n", "nums = [3, 1, 4, 2]\n", "nums.sort() # In-place sort\n", "nums.reverse() # In-place reverse\n", "print(\"Sorted result:\", nums) # [4, 3, 2, 1]\n", "\n", "# Accessing with negative index\n", "print(\"Last element:\", fruits[-1]) # orange" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List Slicing Operations\n", "**Syntax**:\n", "```python\n", "list[start:end:step]\n", "```\n", "- start: Starting index (inclusive), default is 0\n", "- end: Ending index (exclusive), default is the end of the list\n", "- step: Step size, default is 1\n", "\n", "**Key Features**:\n", "- Returns a new list\n", "- Supports negative indexing\n", "- Does not modify the original list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Slicing example\n", "letters = ['a', 'b', 'c', 'd', 'e', 'f']\n", "\n", "print(\"First three elements:\", letters[0:3]) # ['a', 'b', 'c']\n", "print(\"Every other element:\", letters[::2]) # ['a', 'c', 'e']\n", "print(\"Slicing in reverse (subset):\", letters[-3:-1]) # ['d', 'e']\n", "print(\"Reversed list:\", letters[::-1]) # ['f', 'e', 'd', 'c', 'b', 'a']\n", "\n", "# Modifying the original list using slice assignment\n", "letters[1:3] = ['x', 'y']\n", "print(\"After modification:\", letters) # ['a', 'x', 'y', 'd', 'e', 'f']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2 Dictionary Operations\n", "\n", "Dictionaries are collections of key-value pairs with the following features:\n", "- **Unordered** (though insertion order is preserved in Python 3.7+)\n", "- **Immutable keys**: Keys must be of immutable types such as strings, numbers, or tuples\n", "- **Values of any type**: Values can be any Python object\n", "- **Efficient lookup**: Implemented as hash tables with O(1) average lookup time\n", "\n", "#### Common Dictionary Methods:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Basic dictionary operations example\n", "student = {\"name\": \"Alice\", \"age\": 20}\n", "\n", "# Add, update, and delete\n", "student[\"score\"] = 85 # Add a new key-value pair\n", "student[\"age\"] = 21 # Update an existing key\n", "del student[\"score\"] # Delete a key\n", "print(\"Current dictionary:\", student) # {'name': 'Alice', 'age': 21}\n", "\n", "# Common methods\n", "print(\"All keys:\", student.keys()) # dict_keys(['name', 'age'])\n", "print(\"All values:\", student.values()) # dict_values(['Alice', 21])\n", "print(\"Key-value pairs:\", student.items()) # dict_items([('name', 'Alice'), ('age', 21)])\n", "\n", "# Safe access\n", "print(\"Score:\", student.get(\"score\", \"N/A\")) # N/A\n", "\n", "# Merging dictionaries\n", "extra_info = {\"gender\": \"female\", \"age\": 22}\n", "student.update(extra_info)\n", "print(\"After merging:\", student) # {'name': 'Alice', 'age': 22, 'gender': 'female'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary View Objects\n", "Special features in Python 3:\n", "- `keys()`, `values()`, and `items()` return view objects\n", "- View objects dynamically reflect changes to the dictionary\n", "- They support set operations (intersection, union, etc.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# View objects example\n", "d = {'a': 1, 'b': 2}\n", "keys = d.keys()\n", "values = d.values()\n", "\n", "# Demonstrate dynamic changes\n", "d['c'] = 3\n", "print(\"Updated keys view:\", list(keys)) # ['a', 'b', 'c']\n", "print(\"Updated values view:\", list(values)) # [1, 2, 3]\n", "\n", "# Set operations\n", "d2 = {'b': 3, 'c': 4}\n", "common_keys = d.keys() & d2.keys()\n", "print(\"Common keys:\", common_keys) # {'b', 'c'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.3 List Comprehensions and Generators\n", "\n", "#### List Comprehensions\n", "**Syntax**:\n", "```python\n", "[expression for item in iterable if condition]\n", "```\n", "\n", "**Key Features**:\n", "1. Concise syntax for creating new lists\n", "2. Can include conditional filtering (if clause)\n", "3. Supports multiple nested loops\n", "\n", "**Use Cases**:\n", "- Data transformation (e.g., type conversion, numerical computation)\n", "- Data filtering (selecting elements that meet conditions)\n", "- Matrix operations (e.g., processing two-dimensional lists)\n", "\n", "**Notes**:\n", "- Avoid overly complex nesting (more than 2 levels is better handled with conventional loops)\n", "- Be mindful of memory usage when processing large datasets\n", "- Cannot handle exceptions internally (they must be handled externally)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List comprehension example\n", "# Basic form\n", "squares = [x**2 for x in range(10)]\n", "print(\"List of squares:\", squares)\n", "\n", "# With conditional filtering\n", "even_squares = [x**2 for x in range(10) if x % 2 == 0]\n", "print(\"Even squares:\", even_squares)\n", "\n", "# Nested loops\n", "matrix = [[1,2,3], [4,5,6], [7,8,9]]\n", "flatten = [num for row in matrix for num in row]\n", "print(\"Flattened matrix:\", flatten)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Generator Expressions\n", "**Syntax**:\n", "```python\n", "(expression for item in iterable if condition)\n", "```\n", "\n", "**Key Features**:\n", "1. Lazy evaluation: Generates elements on demand to save memory\n", "2. Single-use iterator: Can only be iterated once\n", "3. Can be used with many built-in functions (e.g., sum, max)\n", "\n", "**Best Practices**:\n", "- Prefer for handling large datasets\n", "- Useful for pipelined data processing (chaining multiple generators)\n", "- Convert to other data structures if multiple iterations are needed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Generator expression example\n", "# Basic generator\n", "gen = (x**3 for x in range(5))\n", "print(\"Generator type:\", type(gen))\n", "print(\"Generator contents:\", list(gen))\n", "\n", "# Pipeline processing\n", "data = range(1000000)\n", "sum_squares = sum(x**2 for x in data if x % 3 == 0)\n", "print(\"Sum of squares divisible by 3:\", sum_squares)\n", "\n", "# Comparing memory usage between generator and list\n", "import sys\n", "list_size = sys.getsizeof([x for x in range(1000000)])\n", "gen_size = sys.getsizeof((x for x in range(1000000)))\n", "print(f\"List memory: {list_size} bytes\")\n", "print(f\"Generator memory: {gen_size} bytes\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.4 Advanced Dictionary Operations\n", "\n", "#### Dictionary Comprehensions\n", "**Syntax**:\n", "```python\n", "{key_expr: value_expr for item in iterable if condition}\n", "```\n", "\n", "**Typical Applications**:\n", "- Transforming existing dictionaries (key-value conversion)\n", "- Building dictionaries from other data structures\n", "- Preprocessing for data grouping\n", "\n", "**Special Methods**:\n", "- `setdefault(key, default)`: Safely get and set a default value\n", "- `update()`: Merge dictionaries (modifies in place)\n", "- `get(key, default)`: Safely retrieve a value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Dictionary operations example\n", "# Dictionary comprehension\n", "square_dict = {x: x**2 for x in range(5)}\n", "print(\"Square dictionary:\", square_dict)\n", "\n", "# Key-value inversion\n", "original = {'a': 1, 'b': 2}\n", "inverted = {v: k for k, v in original.items()}\n", "print(\"Inverted dictionary:\", inverted)\n", "\n", "# setdefault usage\n", "word_counts = {}\n", "words = ['apple', 'banana', 'apple', 'cherry']\n", "for word in words:\n", " word_counts.setdefault(word, 0)\n", " word_counts[word] += 1\n", "print(\"Word frequency:\", word_counts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary Merging Methods\n", "1. **update() method**:\n", "```python\n", "dict1.update(dict2) # Modifies dict1 in place\n", "```\n", "\n", "2. **Unpacking operator (Python 3.5+)**:\n", "```python\n", "merged = {**dict1, **dict2}\n", "```\n", "\n", "3. **collections.ChainMap**:\n", "```python\n", "from collections import ChainMap\n", "combined = ChainMap(dict1, dict2)\n", "```\n", "\n", "**Recommendation**:\n", "- Use the unpacking operator when a new dictionary is needed\n", "- Use ChainMap for dynamic updates\n", "- Use update() for in-place modification" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Dictionary merging example\n", "d1 = {'a': 1, 'b': 2}\n", "d2 = {'b': 3, 'c': 4}\n", "\n", "# Method 1: Unpacking operator (preferred)\n", "merged = {**d1, **d2}\n", "print(\"Merged using unpacking:\", merged) # {'a': 1, 'b': 3, 'c': 4}\n", "\n", "# Method 2: update()\n", "d1.update(d2)\n", "print(\"Merged using update:\", d1) # d1 modified in place\n", "\n", "# Method 3: ChainMap\n", "from collections import ChainMap\n", "combined = ChainMap(d1, d2)\n", "print(\"ChainMap value for 'b':\", combined['b']) # Shows the value from the first dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.5 Set Operations\n", "\n", "**Key Features**:\n", "- Unordered collection of unique elements\n", "- Supports mathematical set operations\n", "- Implemented as hash tables with O(1) average lookup efficiency\n", "\n", "**Types of Operations**:\n", "| Operator | Method | Description |\n", "|----------|--------------------|---------------------|\n", "| `|` | union() | Union |\n", "| `&` | intersection() | Intersection |\n", "| `-` | difference() | Difference |\n", "| `^` | symmetric_diff() | Symmetric difference|\n", "\n", "**Special Types**:\n", "- `frozenset`: Immutable set (hashable, can be used as dictionary keys)\n", "- `collections.Counter`: Multiset with counting capabilities" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Set operations example\n", "A = {1, 2, 3, 4}\n", "B = {3, 4, 5, 6}\n", "\n", "print(\"Union:\", A | B) # {1,2,3,4,5,6}\n", "print(\"Intersection:\", A & B) # {3,4}\n", "print(\"Difference:\", A - B) # {1,2}\n", "print(\"Symmetric Difference:\", A ^ B) # {1,2,5,6}\n", "\n", "# Set comprehension\n", "words = ['apple', 'banana', 'cherry']\n", "unique_lengths = {len(word) for word in words}\n", "print(\"Unique lengths set:\", unique_lengths)\n", "\n", "# Frozenset\n", "fs = frozenset([1,2,3])\n", "try:\n", " fs.add(4) # Will raise AttributeError\n", "except AttributeError as e:\n", " print(\"Error:\", e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Applications of Sets\n", "1. **Quick deduplication**:\n", "```python\n", "unique_items = list(set(duplicate_list))\n", "```\n", "\n", "2. **Optimized membership testing**:\n", "```python\n", "if item in large_set: # Much faster than a list\n", " ...\n", "```\n", "\n", "3. **Data relationship analysis**:\n", "- Mutual friends (intersection)\n", "- Unique attributes (difference)\n", "- Combined attributes (union)\n", "\n", "**Performance Comparison**:\n", "| Operation | List | Set |\n", "|------------------|---------|-----------|\n", "| Membership test | O(n) | O(1) |\n", "| Deduplication | O(n²) | O(n) |\n", "| Intersection/Union/Difference | O(n*m) | O(len(s)) |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Performance comparison example\n", "import timeit\n", "\n", "# Membership testing\n", "list_time = timeit.timeit('\"99999\" in lst', \n", " setup='lst = list(map(str, range(100000)))', \n", " number=1000)\n", "\n", "set_time = timeit.timeit('\"99999\" in s', \n", " setup='s = set(map(str, range(100000)))', \n", " number=1000)\n", "\n", "print(f\"List membership test time: {list_time:.4f}s\")\n", "print(f\"Set membership test time: {set_time:.4f}s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comprehensive Exercise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise: Implement a Calculator class that supports addition, subtraction, multiplication, and division\n", "class Calculator:\n", " def __init__(self, initial_value=0):\n", " self.value = initial_value\n", " \n", " def add(self, x):\n", " self.value += x\n", " return self\n", " \n", " def subtract(self, x):\n", " self.value -= x\n", " return self\n", " \n", " def multiply(self, x):\n", " self.value *= x\n", " return self\n", " \n", " def divide(self, x):\n", " try:\n", " self.value /= x\n", " except ZeroDivisionError:\n", " print(\"Error: Divisor cannot be zero\")\n", " return self\n", " \n", " def __str__(self):\n", " return str(self.value)\n", "\n", "# Chained calls\n", "calc = Calculator(10)\n", "calc.add(5).multiply(3).subtract(2).divide(4)\n", "print(f\"Calculation result: {calc}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }