Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
534 changes: 534 additions & 0 deletions advanced_python/01_execution_model.ipynb

Large diffs are not rendered by default.

364 changes: 364 additions & 0 deletions advanced_python/02_memory_scopes.ipynb

Large diffs are not rendered by default.

874 changes: 874 additions & 0 deletions advanced_python/03_cpython_internals.ipynb

Large diffs are not rendered by default.

663 changes: 663 additions & 0 deletions advanced_python/04_advanced_iterators.ipynb

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions advanced_python/05_file_handling.ipynb

Large diffs are not rendered by default.

408 changes: 408 additions & 0 deletions advanced_python/06_system_modules.ipynb

Large diffs are not rendered by default.

338 changes: 338 additions & 0 deletions advanced_python/07_advanced_string_formatting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced String Formatting\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python offers incredibly powerful string interpolation tools. Here is a breakdown of all the ways you can format text, align outputs, and convert numerical representations directly inside f-strings."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 00_str_format.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1. multiplication method\n",
"print(f\"{'=' * 12}Your message{'=' * 12}\")\n",
"\n",
"# Output: ============Your Message============\n",
"\n",
"\n",
"# 2. center(width, fillchar): more readable approach\n",
"print(\"Your Message\".center(36, \"=\")) # Output: ============Your Message============\n",
"print(\"Your Message\".center(36, \"-\")) # Output: ------------Your Message------------\n",
"print(\"Your Message\".center(36, \"*\")) # Output: ************Your Message************\n",
"print(\"Your Message\".center(36, \"#\")) # Output: ############Your Message############\n",
"\n",
"\n",
"# 3. Working with variables and spaces\n",
"index = 0\n",
"label = \"Cat\"\n",
"pred = \"Cat\"\n",
"conf = 0.8\n",
"\n",
"print(f\"Image {index + 1}: True = {label:12s} | Predicted = {pred:12s} | Confidence = {conf:.4f}\")\n",
"\n",
"print(f\"Image {index + 1}: True = {label:12s} | Predicted = {pred:12s} | Confidence = {conf:.4f}\")\n",
"\n",
"\"\"\"\n",
"Image 1: True = Cat | Predicted = Cat | Confidence = 0.8000\n",
"Image 1: True = Cat | Predicted = Cat | Confidence = 0.8000\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 01_str_alignment.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = \"Ali\"\n",
"\n",
"print(f\"{name:10}\") # Left-aligned in width 10\n",
"print(f\"{name:<10}\") # Explicit left align\n",
"print(f\"{name:>10}\") # Right align\n",
"print(f\"{name:^10}\") # Center align\n",
"\n",
"\"\"\"\n",
"Ali \n",
"Ali \n",
" Ali\n",
" Ali\n",
"\"\"\"\n",
"\n",
"print(f\"{name:*<10}\") # Ali*******\n",
"print(f\"{name:*>10}\") # *******Ali\n",
"print(f\"{name:=^10}\") # ===Ali===="
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 02_integers.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 42\n",
"\n",
"# n:5 define no. of spaces\n",
"print(f\"{n:5}\") # Output: 42\n",
"print(f\"{n:05}\") # Output: 00042 zero-padded\n",
"print(f\"{n:+5}\") # Output: +42 Always show plus sign\n",
"print(f\"{n:+5}\") # Output: +42 Always show plus sign\n",
"\n",
"n = 1000000\n",
"print(f\"{n:,}\") # Output: 1,000,000 thousands separator\n",
"print(f\"{n:_}\") # Output: 1_000_000 thousands separator\n",
"\n",
"print(f\"{-42:+}\") # Output: -42"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 03_floating.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e = 2.71828\n",
"\n",
"print(f\"{e:.2f}\") # Output: 2.72 -- two decimal places\n",
"print(f\"{e:.4f}\") # Output: 2.7183 -- four decimal places\n",
"print(f\"{e:10.2f}\") # Output: 2.72 -- width 10, 2 decimals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 04_percentage.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rate = 0.853\n",
"\n",
"print(f\"{rate:.1%}\") # output: 85.3%"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 05_bin_oct_hex.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 178910\n",
"\n",
"print(f\"{n:b}\") # Output: 101011101011011110 -- binary\n",
"print(f\"{n:o}\") # Output: 535336 -- octal\n",
"print(f\"{n:x}\") # Output: 2bade -- hexa lowercase\n",
"print(f\"{n:X}\") # Output: 2BADE -- hexa Uppercase"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 06_sci_notation.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Scientific notations\n",
"\n",
"n = 12345678\n",
"\n",
"print(f\"{n:e}\") # 1.234568e+07 1.234568 Γ— 10^7\n",
"print(f\"{n:.2e}\") # 1.23e+07 1.23 x 10^7\n",
"\n",
"\n",
"# common ML examples\n",
"lr = 1e-3\n",
"print(f\"lr = {lr:.0e}\") # lr = 1e-03 1 x 10^-3\n",
"\n",
"loss = 2.34567e-7\n",
"print(f\"loss = {loss:.3e}\") # loss = 2.346e-07 2.346 x 10^-7\n",
"\n",
"\n",
"# Global formatter\n",
"\"\"\"\n",
"g automatically chooses between:\n",
"\n",
"fixed point (f)\n",
"scientific notation (e)\n",
"\n",
"depending on which looks better.\n",
"\"\"\"\n",
"\n",
"\n",
"x = 0.00001234\n",
"print(f\"{x:g}\") # 1.234e-05\n",
"\n",
"\n",
"x = 1234.567\n",
"print(f\"{x:g}\") # 1234.57\n",
"\n",
"# Significant Digits\n",
"x = 0.000012345678\n",
"\n",
"print(f\"{x:.3g}\") # 1.23e-05 # Show 3 or 5 significant digits, not decimal places.\n",
"print(f\"{x:.5g}\") # 1.234e-05"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 07_debug_fmt.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Debugging formatting\n",
"\n",
"name = \"Bedar\"\n",
"age = 21\n",
"\n",
"print(f\"{name=} | {age=}\") # Output: name='Bedar' | age=21"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 08_integer_d.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# d means decimal integer. Display this value as a base-10 integer.\n",
"\n",
"n = 42\n",
"\n",
"print(f\"{n:d}\") # Output: 42 -- Usually you don't need d by itself because Python already knows it's an integer.\n",
"\n",
"\n",
"n = 7\n",
"f\"{n:03d}\" # Output: 007\n",
"\"\"\"\n",
"0 -> pad with zeros\n",
"3 -> total width = 3\n",
"d -> decimal integer\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 09_date_fmt.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"now = datetime.now()\n",
"\n",
"print(f\"{now:%Y-%m-%d}\") # Output: 2026-06-25\n",
"print(f\"{now:%H-%M-%S}\") # Output: 21-52-01"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 10_table.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = \"Ali\"\n",
"score = 95\n",
"\n",
"print(f\"{'Name':<10}{'Score':>10}\")\n",
"print(f\"{name:<10}{score:>10}\")\n",
"\n",
"\"\"\"\n",
"Name Score\n",
"Ali 95\n",
"\"\"\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Loading