diff --git a/python/top-10-python-tricks.ipynb b/python/top-10-python-tricks.ipynb new file mode 100644 index 00000000..557f1ed6 --- /dev/null +++ b/python/top-10-python-tricks.ipynb @@ -0,0 +1,198 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "722cb34d", + "metadata": { + "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", + "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", + "papermill": { + "duration": 0.001725, + "end_time": "2025-07-02T13:57:55.591248", + "exception": false, + "start_time": "2025-07-02T13:57:55.589523", + "status": "completed" + }, + "tags": [] + }, + "source": [ + "\n", + "## 🐍 Top 10 Python Tricks Every Data Scientist Should Know\n", + "\n", + "*Boost your productivity and write cleaner, faster, and more efficient code in data science!*\n", + "\n", + "---\n", + "\n", + "### 📌 **1. List Comprehensions**\n", + "\n", + "**Why:** Faster and cleaner than traditional loops.\n", + "\n", + "```python\n", + "squares = [x**2 for x in range(10)]\n", + "# Equivalent to:\n", + "# squares = []\n", + "# for x in range(10):\n", + "# squares.append(x**2)\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **2. Enumerate for Index + Value**\n", + "\n", + "**Why:** Clean way to loop with index and item.\n", + "\n", + "```python\n", + "for idx, val in enumerate(['a', 'b', 'c']):\n", + " print(f\"Index: {idx}, Value: {val}\")\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **3. Unpacking in Loops**\n", + "\n", + "**Why:** Makes code cleaner when working with tuples/lists.\n", + "\n", + "```python\n", + "pairs = [(1, 'a'), (2, 'b'), (3, 'c')]\n", + "for num, letter in pairs:\n", + " print(num, letter)\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **4. Using `zip()` to Combine Lists**\n", + "\n", + "**Why:** Pair elements from multiple lists.\n", + "\n", + "```python\n", + "names = ['Alice', 'Bob', 'Charlie']\n", + "scores = [85, 90, 95]\n", + "for name, score in zip(names, scores):\n", + " print(f\"{name} scored {score}\")\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **5. Dictionary Comprehensions**\n", + "\n", + "**Why:** Construct dictionaries concisely.\n", + "\n", + "```python\n", + "squares = {x: x*x for x in range(5)}\n", + "# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **6. Lambda Functions**\n", + "\n", + "**Why:** Quick, inline anonymous functions.\n", + "\n", + "```python\n", + "double = lambda x: x * 2\n", + "print(double(5)) # Output: 10\n", + "```\n", + "\n", + "Use in pandas:\n", + "\n", + "```python\n", + "df['log_salary'] = df['salary'].apply(lambda x: np.log(x))\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **7. Using `any()` and `all()`**\n", + "\n", + "**Why:** Clean and fast checks across iterable values.\n", + "\n", + "```python\n", + "nums = [1, 2, 3, 0]\n", + "print(all(nums)) # False (because of 0)\n", + "print(any(nums)) # True (non-zero elements exist)\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **8. Swapping Variables Without Temp**\n", + "\n", + "**Why:** Clean, pythonic way to swap.\n", + "\n", + "```python\n", + "a, b = 10, 20\n", + "a, b = b, a\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **9. F-Strings for String Formatting (Python 3.6+)**\n", + "\n", + "**Why:** Cleaner and faster string formatting.\n", + "\n", + "```python\n", + "name = \"Abhishek\"\n", + "score = 95\n", + "print(f\"{name} scored {score} in the test.\")\n", + "```\n", + "\n", + "---\n", + "\n", + "### 📌 **10. Using `Counter` from `collections`**\n", + "\n", + "**Why:** Count frequencies with ease.\n", + "\n", + "```python\n", + "from collections import Counter\n", + "\n", + "words = ['data', 'science', 'data', 'AI']\n", + "word_counts = Counter(words)\n", + "print(word_counts)\n", + "# Output: Counter({'data': 2, 'science': 1, 'AI': 1})\n", + "```\n", + "\n", + "---\n" + ] + } + ], + "metadata": { + "kaggle": { + "accelerator": "none", + "dataSources": [], + "dockerImageVersionId": 31040, + "isGpuEnabled": false, + "isInternetEnabled": true, + "language": "python", + "sourceType": "notebook" + }, + "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.11.11" + }, + "papermill": { + "default_parameters": {}, + "duration": 5.447827, + "end_time": "2025-07-02T13:57:56.012152", + "environment_variables": {}, + "exception": null, + "input_path": "__notebook__.ipynb", + "output_path": "__notebook__.ipynb", + "parameters": {}, + "start_time": "2025-07-02T13:57:50.564325", + "version": "2.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}