{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Mastery: The COMPLETE Practice Notebook\n",
"\n",
"This is your one-stop shop for mastering Core Python. To be a professional Data Scientist, you don't just need libraries; you need to understand the language that powers them. This notebook covers every major concept from basic types to Multithreading and Software Design Patterns.\n",
"\n",
"### Complete Curriculum:\n",
"1. **Basics**: Types, Strings, F-Strings, and Slicing.\n",
"2. **Data Structures**: Lists, Dictionaries, Tuples, and Sets.\n",
"3. **Control Flow**: Loops, Conditionals, Enumerate, and Zip.\n",
"4. **Productivity**: List/Dict Comprehensions & Generators.\n",
"5. **Functions**: Args, Kwargs, Lambdas, and Decorators.\n",
"6. **OOP (Advanced)**: Inheritance, Dunder Methods, and Static Methods.\n",
"7. **High-Level Programming**: Asynchronous Python (Async/Await).\n",
"8. **Concurrency**: Multithreading and Multi-processing.\n",
"9. **Software Design Patterns**: Singleton and Factory Patterns.\n",
"10. **Systems**: File I/O, Error Handling, and Datetime.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Strings, F-Strings & Slicing\n",
"\n",
"### Task 1: Formatting & Slicing\n",
"1. Use f-strings to print `pi = 3.14159` to 2 decimal places.\n",
"2. Reverse the string `\"DataScience\"` using slicing."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pi = 3.14159\n",
"s = \"DataScience\"\n",
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"print(f\"Pi: {pi:.2f}\")\n",
"print(s[::-1])\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Advanced Data Structures\n",
"\n",
"### Task 2: Dictionaries & Sets\n",
"1. Convert the list `[1, 2, 2, 3, 3, 3]` to a set to find unique values.\n",
"2. Given `d = {'a': 1, 'b': 2}`, print all keys and values using a loop and `.items()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d = {'a': 1, 'b': 2}\n",
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"unique_vals = set([1, 2, 2, 3, 3, 3])\n",
"for k, v in d.items():\n",
" print(f\"Key: {k}, Value: {v}\")\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Control Flow: Enumerate & Zip\n",
"\n",
"### Task 3: Pairing Data\n",
"Combine `names = ['Alice', 'Bob']` and `ages = [25, 30]` using `zip` and print them as pairs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"names = ['Alice', 'Bob']\n",
"ages = [25, 30]\n",
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"for name, age in zip(names, ages):\n",
" print(f\"{name} is {age} years old\")\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Advanced Functions: Decorators & Generators\n",
"\n",
"### Task 4.1: Custom Decorator\n",
"Create a decorator called `@timer` that prints \"Starting...\" before a function runs and \"Finished!\" after it runs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"def timer(func):\n",
" def wrapper(*args, **kwargs):\n",
" print(\"Starting...\")\n",
" result = func(*args, **kwargs)\n",
" print(\"Finished!\")\n",
" return result\n",
" return wrapper\n",
"\n",
"@timer\n",
"def say_hello():\n",
" print(\"Hello!\")\n",
"\n",
"say_hello()\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Object-Oriented Programming (Advanced)\n",
"\n",
"### Task 5: Dunder Methods & Static Methods\n",
"Create a class `Book` that:\n",
"1. Uses `__init__` for `title` and `author`.\n",
"2. Uses `__str__` to return `\"[Title] by [Author]\"`.\n",
"3. Has a `@staticmethod` called `is_valid_isbn(isbn)` that returns True if length is 13."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"class Book:\n",
" def __init__(self, title, author):\n",
" self.title = title\n",
" self.author = author\n",
" \n",
" def __str__(self):\n",
" return f\"{self.title} by {self.author}\"\n",
" \n",
" @staticmethod\n",
" def is_valid_isbn(isbn):\n",
" return len(str(isbn)) == 13\n",
"\n",
"b = Book(\"1984\", \"George Orwell\")\n",
"print(b)\n",
"print(Book.is_valid_isbn(1234567890123))\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. High-Level Concepts: Concurrency\n",
"\n",
"### Task 6: Multithreading vs Multi-processing\n",
"Explain in a comment why you would use `threading` for I/O tasks and `multiprocessing` for CPU-bound tasks in Python (Hint: GIL)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import threading\n",
"import multiprocessing\n",
"\n",
"# YOUR ANSWER HERE (AS A COMMENT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"# Multithreading: Efficient for I/O-bound tasks (like waiting for a web response)\n",
"# because the GIL (Global Interpreter Lock) prevents multiple threads from \n",
"# executing Python bytecode at once, but allows waiting for I/O.\n",
"\n",
"# Multiprocessing: Efficient for CPU-bound tasks (like heavy math/ML matrix multiplication)\n",
"# because it creates separate memory spaces and separate GILs for each process,\n",
"# bypassing the GIL limitation entirely.\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Software Design Patterns\n",
"\n",
"### Task 7: The Singleton Pattern\n",
"Implement a Singleton class called `DatabaseConnection` that ensures only one instance of the class can ever be created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Click to see Solution
\n",
"\n",
"```python\n",
"class DatabaseConnection:\n",
" _instance = None\n",
" \n",
" def __new__(cls):\n",
" if cls._instance is None:\n",
" print(\"Initializing new database connection instance...\")\n",
" cls._instance = super(DatabaseConnection, cls).__new__(cls)\n",
" return cls._instance\n",
"\n",
"db1 = DatabaseConnection()\n",
"db2 = DatabaseConnection()\n",
"print(\"Are they the same instance?\", db1 is db2)\n",
"```\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--- \n",
"### 🏆 You are now a Python Master Engineer! \n",
"With these additions, you have covered everything from basic variables to Singleton patterns and GIL-based concurrency. \n",
"You are fully prepared to build high-scale machine learning systems."
]
}
],
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}