Spaces:
Running
Running
File size: 12,485 Bytes
854c114 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | {
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\n",
"\n",
"```python\n",
"print(f\"Pi: {pi:.2f}\")\n",
"print(s[::-1])\n",
"```\n",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\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",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\n",
"\n",
"```python\n",
"for name, age in zip(names, ages):\n",
" print(f\"{name} is {age} years old\")\n",
"```\n",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\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",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\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",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\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",
"</details>"
]
},
{
"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": [
"<details>\n",
"<summary><b>Click to see Solution</b></summary>\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",
"</details>"
]
},
{
"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
} |