Python Programming
40 subscribers
5 photos
2 links
🐍 Python Programming | Code. Learn. Grow.

Welcome to Python Programming — your go-to Telegram community for mastering Python, from basics to advanced!
Download Telegram
Ever wanted a tiny personal assistant that lives right in your terminal? Imagine typing todo add "Buy milk" and seeing it saved instantly, then later running todo list to see everything waiting for you. You can build a single‑file command‑line To‑Do list in under an hour – no databases, no web servers, just pure Python.

Why this works

- argparse turns ordinary words you type into actions (add, list, done, delete). It reads sys.argv for you, so you don’t have to manually slice strings.
- json gives you a human‑readable file format. Python can turn a list of dictionaries into a text file with json.dump and back again with json.load. No extra libraries needed.
- pathlib lets you handle file paths in a cross‑platform way (Path.home() / ".my_todo.json"). It’s safer than concatenating strings with "/" or "\\“.
- datetime stamps each task so you can see when you created it – useful for “what’s overdue?”.

The big picture

1. Parse the command (add, list, done, remove) with argparse.
2. Load the existing JSON file (or start with an empty list if the file is missing).
3. Modify the list of tasks according to the command.
4. Write the updated list back to the same JSON file.
5. Print a friendly summary for the user.

Key concepts in plain language

- Dictionary: a “named box” ({'text': 'Buy milk', 'done': False}) that stores data with labels.
- List: an ordered collection of dictionaries – each entry is a task.
- Serialization: turning Python objects into a text format (JSON) so they can be saved to disk.
- Deserialization: reading that text back into Python objects.

A minimal skeleton (copy‑paste & run)

(continued below 👇)
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
from datetime import datetime

# Where we keep the data
DATA_FILE = Path.home() / ".my_todo.json"

def load_tasks():
if DATA_FILE.exists():
return json.load(DATA_FILE.open())
return [] # start empty

def save_tasks(tasks):
DATA_FILE.write_text(json.dumps(tasks, indent=2))

def add_task(text):
tasks = load_tasks()
tasks.append({
"text": text,
"done": False,
"created": datetime.now().isoformat()
})
save_tasks(tasks)
print(f'✅ Added: "{text}"')

def list_tasks():
tasks = load_tasks()
if not tasks:
print("🗒️ No tasks yet!")
return
for i, t in enumerate(tasks, 1):
status = "✔️" if t["done"] else "❌"
print(f"{i}. {status} {t['text']} (added {t['created'][:10]})")

def mark_done(idx):
tasks = load_tasks()
try:
tasks[idx]["done"] = True
save_tasks(tasks)
print(f'✅ Marked task #{idx+1} as done')
except IndexError:
print("⚠️ No such task number")

def remove_task(idx):
tasks = load_tasks()
try:
removed = tasks.pop(idx)
save_tasks(tasks)
print(f'🗑️ Removed: "{removed["text"]}"')
except IndexError:
print("⚠️ No such task number")

def main():
parser = argparse.ArgumentParser(prog="todo", description="Simple terminal to‑do list")
sub = parser.add_subparsers(dest="cmd")

add = sub.add_parser("add")
add.add_argument("text", help="Task description")

sub.add_parser("list")

done = sub.add_parser("done")
done.add_argument("num", type=int, help="Task number to mark done")

rm = sub.add_parser("remove")
rm.add_argument("num", type=int, help="Task number to delete")

args = parser.parse_args()

if args.cmd == "add":
add_task(args.text)
elif args.cmd == "list":
list_tasks()
elif args.cmd == "done":
mark_done(args.num - 1) # human numbers start at 1
elif args.cmd == "remove":
remove_task(args.num - 1)
else:
parser.print_help()

if __name__ == "__main__":
main()


How to run it

1. Save the script as todo.py and make it executable (chmod +x todo.py on Linux/macOS).
2. Run ./todo.py add "Read a chapter of a book" → task saved.
3. ./todo.py list → see all tasks with ✅/❌.
4. ./todo.py done 1 → mark the first task as done.
5. ./todo.py remove 2 → delete the second task.

That’s it – a fully functional, persistent to‑do list you built yourself! Tweak it by adding priorities, due dates, or even a tiny web UI later. The core ideas (argument parsing, JSON storage, pathlib paths) are building blocks you’ll reuse in countless Python projects.
❤1
Ever needed to walk through two (or more) lists at the same time?
Writing a loop with an index variable works, but there’s a cleaner, safer way: zip().

What zip() does
zip() takes any number of iterables (lists, strings, ranges…) and creates a new iterator that yields tuples.
Each tuple contains the items that share the same position in the original iterables.

names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]

for name, score in zip(names, scores):
print(f"{name} scored {score}")


Why it works

1. Parallel stepping – Internally zip asks each iterable for its next element, then bundles them together.
2. Stops at the shortest – If the iterables have different lengths, zip stops when the first one runs out, preventing an “index out of range” crash.
3. No explicit counter – You never have to manage i = 0; i += 1, so the code is less error‑prone and easier to read.

When to prefer zip over manual indexing

- Readability – for name, score in zip(...): reads like plain English.
- Safety – No chance of forgetting to increment the index or using the wrong list length.
- Flexibility – Works with any iterable, not just lists (e.g., strings, range(), generators).

Quick comparison

Manual index

for i in range(len(names)):
print(names[i], scores[i])


With zip

for name, score in zip(names, scores):
print(name, score)


Key take‑aways

- Use zip() whenever you need to pair items from multiple sequences.
- It automatically stops at the shortest sequence – perfect for avoiding accidental over‑runs.
- You can unpack the tuple directly in the for header, giving you meaningful variable names right away.

Give it a try! Replace any “loop‑by‑index” pattern with zip and watch your code become cleaner and more Pythonic.
❤1
Ever stared at a blank terminal and thought “I could use a spark of inspiration right now”? You can turn that feeling into a tiny, useful Python project that both brightens your day and builds a personal “quote‑archive” you can flip through later. In under an hour you’ll have a command‑line daily‑quote fetcher that pulls a random quote from the web, shows it instantly, and logs it with a timestamp in a plain‑text file.

What you’ll need

- requests – a friendly library that hides the low‑level details of talking to websites (it does the HTTP GET for you).
- json – Python’s built‑in way to turn the text you get back from the web into real Python objects (lists, dictionaries).
- datetime – lets you stamp each quote with the exact day and time it was fetched.
- random – picks a random element from a list (so you don’t always see the same quote).
- File I/O – using open(..., "a") to append new lines to a log file without overwriting anything.

Why these pieces fit together

1. Fetching data – requests.get(url) sends a GET request to the API endpoint. The server replies with a payload (usually JSON).
2. Turning text into data – response.json() parses that JSON string into ordinary Python structures (a list of dictionaries, each representing a quote).
3. Choosing one – random.choice(quotes) picks a random dictionary from the list, giving you a fresh quote each run.
4. Recording the moment – datetime.now().strftime("%Y-%m-%d %H:%M") formats the current date‑time as a readable string, so your log reads “2026‑09‑29 14:03 – …”.
5. Saving without wiping – opening a file with mode "a" means “add to the end”. Every new quote gets a new line, preserving the whole history.

Sketch of the script

import requests          # talk to the web
import random # pick a random item
from datetime import datetime # timestamp
import json # (optional) for pretty‑printing

# 1️⃣ The free quote API – it returns a big list of quote objects
API_URL = "https://type.fit/api/quotes"

def fetch_random_quote():
resp = requests.get(API_URL, timeout=10) # ask the server
resp.raise_for_status() # raise an error if something went wrong
quotes = resp.json() # turn JSON text into Python list
return random.choice(quotes) # pick one at random

def log_quote(quote, file_path="quotes_log.txt"):
ts = datetime.now().strftime("%Y-%m-%d %H:%M")
line = f"{ts} – {quote['text']} — {quote.get('author') or 'Unknown'}\n"
with open(file_path, "a", encoding="utf-8") as f: # open for appending
f.write(line)

def main():
q = fetch_random_quote()
# 2️⃣ Show it nicely in the terminal
author = q.get('author') or "Unknown"
print("\n💡 Quote of the moment:\n")
print(f"\"{q['text']}\"")
print(f" — {author}\n")
# 3️⃣ Save it for later browsing
log_quote(q)

if __name__ == "__main__":
main()


How it works, line by line

(continued below 👇)
- requests.get(..., timeout=10) contacts the API and waits at most 10 seconds – a safety net so your script never hangs forever.
- resp.raise_for_status() turns HTTP error codes (like 404 or 500) into Python exceptions, so you’ll know instantly if the service is down.
- resp.json() is a convenience: the requests library already decoded the raw text and handed you a ready‑to‑use Python list.
- random.choice(quotes) does exactly what its name says – no loops, no extra logic.
- datetime.now().strftime(...) converts the current moment into a human‑readable string; the pattern "%Y-%m-%d %H:%M" yields 2026-09-29 14:03.
- open(..., "a", encoding="utf-8") opens (or creates) quotes_log.txt in append mode, ensuring each run adds a fresh line instead of overwriting previous entries.

Take it further (optional, no extra code needed)

- Schedule it with the tiny schedule package so the script runs every morning automatically.
- Add a search function that lets you type a word and filters your log for matching quotes.
- Turn the log into a CSV file for easy import into spreadsheet tools.

That’s it – a handful of lines, a couple of built‑in concepts, and you end up with a daily boost of wisdom and a growing archive you can look back on any time. Give it a try, tweak the output, and watch how a few minutes of code can become a habit‑forming ritual. 🚀
Ever wondered why the numbers you type into a program sometimes behave like words?
When Python reads what you type with input(), it always treats it as text (a string). Even if you type 42, Python sees it as "42" – a sequence of characters, not a number you can add or compare mathematically.

Why does this matter?
Most operations you’ll do with numbers (addition, subtraction, comparisons) need real numeric types like int (whole numbers) or float (decimals). If you try to add two strings, Python will just glue them together:

>>> "2" + "3"
'23' # not 5!


So, after getting input, you usually convert that text to the right type.

Step‑by‑step guide

1. Ask the user for something – input() shows a prompt and waits.
2. Read the result – the result is always a string.
3. Turn the string into a number – use int() for whole numbers or float() for decimals.
4. (Optional) Guard against mistakes – people might type letters instead of numbers, so catching errors keeps the program from crashing.

Simple example: a mini calculator

# 1️⃣ Ask for the first number
first = input("Enter the first number: ")

# 2️⃣ Convert it to a float (works for both integers and decimals)
try:
first_num = float(first)
except ValueError:
print("❌ Oops! That's not a valid number.")
exit()

# 3️⃣ Ask for the second number
second = input("Enter the second number: ")

# 4️⃣ Convert it, with the same safety net
try:
second_num = float(second)
except ValueError:
print("❌ Oops! That's not a valid number.")
exit()

# 5️⃣ Do some math – let's add them
result = first_num + second_num

# 6️⃣ Show the answer
print(f"✅ The sum is: {result}")


What’s happening?

- input("Enter the first number: ") prints the prompt and pauses until you type something and press Enter.
- float(first) tries to turn the entered text into a number that can have a decimal part. If the text isn’t a valid number, Python raises a ValueError.
- The try … except block catches that error, prints a friendly message, and stops the program with exit() so it doesn’t continue with bad data.
- Finally, first_num + second_num performs real arithmetic because both variables are now numbers, not strings.

You could replace float with int if you only want whole numbers – int("3.7") would raise an error, while int("3") works fine.

Quick cheat‑sheet (bold for key terms)

- input(prompt) – shows prompt, returns string.
- int(text) – converts text to an integer (whole number).
- float(text) – converts text to a float (decimal number).
- try / except – protects your code from crashing when conversion fails.

Give it a try! Type the script into your editor, run it, and experiment by entering both numbers and non‑numbers to see how the error handling works. You’ve just turned raw user input into usable data – a foundational skill for every Python programmer. 🚀
❤1
Ever wanted a pocket‑size quiz app you can run straight from the terminal?
Build a single‑session Flashcard Trainer – a tiny program that reads a CSV file of question/answer pairs, shuffles them, asks you one at a time, and saves your score to a tiny JSON “progress” file. No GUI, no web requests, just pure Python that a beginner can finish in an hour.

How it works

- CSV file – you list your flashcards as question,answer. Python’s built‑in csv module turns each line into a dictionary, so you never have to split strings manually.
- Shuffle – random.shuffle randomises the order each run, keeping the practice fresh.
- Prompt & check – input() reads your answer, str.strip().lower() normalises it, then a simple equality test tells you if you’re right.
- Persist score – a tiny JSON file (progress.json) stores the total attempts and correct answers. The json module makes it easy to read/write that data without any database.
- Path handling – pathlib.Path gives you OS‑independent file paths (works on Windows, macOS, Linux) and lets you test “does the file exist?” with Path.is_file().

Why these pieces fit together

- csv.DictReader reads the header row once and gives you a dict per flashcard, so you can refer to row["question"] and row["answer"] by name – far clearer than juggling index positions.
- random.shuffle works in‑place; it doesn’t create a new list, which keeps memory usage tiny.
- json.dump/load serialize Python primitives (ints, dicts) to a human‑readable text file – you can open progress.json later and see how you’ve improved.
- Path abstracts away the difference between "flashcards.csv" and "./data/flashcards.csv"; you can safely join paths with the / operator (data_dir / "flashcards.csv").

A quick sketch

import csv
import json
import random
from pathlib import Path

# ---- configuration -------------------------------------------------
DATA_DIR = Path("./data")
CARD_FILE = DATA_DIR / "flashcards.csv"
PROG_FILE = DATA_DIR / "progress.json"

# ---- load or initialise progress -----------------------------------
if PROG_FILE.is_file():
with PROG_FILE.open() as f:
progress = json.load(f)
else:
progress = {"asked": 0, "correct": 0}

# ---- read flashcards ------------------------------------------------
cards = []
with CARD_FILE.open(newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
cards.append({"q": row["question"], "a": row["answer"]})

random.shuffle(cards) # random order each run

# ---- quiz loop ------------------------------------------------------
for card in cards:
print("\n❓", card["q"])
user = input("Your answer: ").strip().lower()
progress["asked"] += 1
if user == card["a"].strip().lower():
print("✅ Correct!")
progress["correct"] += 1
else:
print(f"❌ Wrong. The right answer is: {card['a']}")

# ---- show summary & save --------------------------------------------
print("\n=== Session Summary ===")
print(f"Asked: {progress['asked']}")
print(f"Correct: {progress['correct']}")
print(f"Score: {progress['correct'] / progress['asked']:.1%}")

with PROG_FILE.open("w", encoding="utf-8") as f:
json.dump(progress, f, indent=2)


What you’ll see

1. The script loads flashcards.csv.
2. It shuffles the cards so you never get the same order twice.
3. Each question appears, you type an answer, and the program instantly tells you if you’re right.
4. When you quit, a tiny JSON file updates – next time you run the trainer you’ll see your cumulative score grow.

Take it further (optional)

(continued below 👇)
- Add a timer (time.time()) to record how long each question took.
- Let the user pick a subset (e.g., only “biology” cards) by adding a “category” column in the CSV.
- Show a progress bar with tqdm for a visual cue of how many cards remain.

That’s it – a fully functional flashcard trainer you can run anywhere Python is installed. Grab a notebook, write a few cards, and start sharpening your knowledge right now!
Ever tried to turn a line of text like “apple,banana,cherry” into a list of separate words, or the other way around? That’s where split() and join() come in – two tiny helpers that make working with strings feel like magic.

Why they exist
A string in Python is just a sequence of characters. Often you need to break that long sequence into smaller pieces (like words) or stitch a list of pieces back together. Rather than writing loops and lots of index math, Python gives us split() to divide and join() to re‑combine.

How split() works
- Call it on a string: my_string.split(separator)
- It looks for the separator you give (default is any whitespace) and cuts the string at each occurrence.
- The result is a list – a new collection that holds each piece as its own string.

How join() works
- Call it on the separator you want between pieces: separator.join(iterable)
- The iterable (usually a list of strings) is walked through, and the separator is inserted between each element.
- The outcome is a single string.

Simple example – turning a CSV line into a list and back

# Original text, values separated by commas
csv_line = "apple,banana,cherry"

# Split the line into a list of fruits
fruits = csv_line.split(",") # ['apple', 'banana', 'cherry']
print("After split:", fruits)

# Maybe we want to sort them
fruits.sort()
print("Sorted:", fruits) # ['apple', 'banana', 'cherry']

# Join them back into a single string, this time using a pipe "|"
new_line = "|".join(fruits) # 'apple|banana|cherry'
print("After join:", new_line)


What’s happening under the hood?
1. split(",") scans the original string, finds each comma, and creates a fresh list where each segment between commas becomes an element.
2. join("|") starts with the first element ('apple'), then adds the separator ('|'), then the next element, and so on, building a new string without altering the original list.

A few handy tips
- No separator = any whitespace: "one two three".split() → ['one', 'two', 'three'] (multiple spaces are ignored).
- Limit splits: my_string.split(sep, maxsplit=2) stops after two cuts, leaving the rest untouched.
- Join only strings: All items in the list must be strings; if you have numbers, convert them first: " ".join(str(x) for x in numbers).

**Why you’ll love them**
- **Readability** – One line does what a whole loop would otherwise require.
- **Safety** – They never modify the original string; they always give you a new object, so you won’t accidentally lose data.
- **Flexibility** – Works with any separator: commas, spaces, tabs (
"\t"), newlines ("\n"), or even emojis! 🎉

Next time you see a long piece of text, remember: **
split()** to break it apart, **join()`** to glue it back together. 🎈
Ever wanted a tiny digital notebook that lives in a single file and works even when you’re offline?
You can build a CLI‑based notes manager in one sitting using only Python’s built‑in sqlite3 module – no external services, no heavy frameworks.

Why SQLite?
- It stores everything in a plain .db file, so you can move or back up your notes with a simple copy.
- The API is part of the standard library, so you don’t need to install anything extra.
- It gives you real‑SQL power (search, ordering, timestamps) without the overhead of a full server.

The sketch of the app

1. Open (or create) a database – sqlite3.connect('notes.db').
2. Create a table if it’s the first run:



CREATE TABLE IF NOT EXISTS note (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TEXT NOT NULL,
content TEXT NOT NULL
);


INTEGER PRIMARY KEY AUTOINCREMENT makes SQLite generate a unique row id automatically.

3. Add a note – grab the current time with datetime.now().isoformat() and insert with parameterised SQL (? placeholders). This protects you from accidental SQL injection even though the input is local.

4. List notes – a simple SELECT id, created, content FROM note ORDER BY id DESC gives the newest entries first.

5. Search – SELECT … FROM note WHERE content LIKE ? lets you find keywords (%keyword%).

6. Close – always call conn.close() when you’re done; it flushes buffers and releases the file lock.

What you’ll learn

- sqlite3 basics: connection, cursor, execute(), commit().
- Parameterized queries – why you should never concatenate strings into SQL.
- Working with timestamps using datetime.
- Simple menu loops and input() handling for a friendly CLI.

Below is a minimal, runnable snippet that contains the core logic. Flesh it out with nicer prompts, colour output, or even an export‑to‑CSV feature – all in one sitting!

import sqlite3
from datetime import datetime

DB = "notes.db"

def init_db():
conn = sqlite3.connect(DB)
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS note (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TEXT NOT NULL,
content TEXT NOT NULL
)""")
conn.commit()
return conn

def add_note(conn):
text = input("\n📝 Your note: ").strip()
if text:
cur = conn.cursor()
cur.execute(
"INSERT INTO note (created, content) VALUES (?, ?)",
(datetime.now().isoformat(timespec='seconds'), text)
)
conn.commit()
print("✅ Note saved!")

def list_notes(conn):
cur = conn.cursor()
cur.execute("SELECT id, created, content FROM note ORDER BY id DESC")
rows = cur.fetchall()
print("\n📚 Your notes:")
for nid, ts, txt in rows:
print(f"{nid:>3} | {ts} | {txt}")

def search_notes(conn):
term = input("\n🔍 Search keyword: ").strip()
cur = conn.cursor()
cur.execute("SELECT id, created, content FROM note WHERE content LIKE ?", (f"%{term}%",))
for nid, ts, txt in cur.fetchall():
print(f"{nid:>3} | {ts} | {txt}")

def main():
conn = init_db()
while True:
print("\n=== Simple Notes ===")
print("1️⃣ Add note")
print("2️⃣ List notes")
print("3️⃣ Search")
print("❌ Quit")
choice = input("Choose: ").strip()
if choice == "1":
add_note(conn)
elif choice == "2":
list_notes(conn)
elif choice == "3":
search_notes(conn)
elif choice.lower() in ("q", "quit", "4", "❌"):
break
conn.close()
print("👋 Bye!")

if __name__ == "__main__":
main()


(continued below 👇)
Try it now: copy the code into a file called notes.py, run python notes.py, and start typing away. Your notes live forever in notes.db, ready to be backed up or synced via cloud storage.

Happy coding, and enjoy your new pocket‑sized knowledge base!
Ever wondered why many Python scripts start with this line?

if __name__ == "__main__":
# code here


That tiny block is a safety gate. It tells Python, “Only run the code inside if this file is being executed directly, not when it’s imported elsewhere.”

Why does it work?
Every Python file has a built‑in variable called __name__.

- When you run the file yourself (e.g., python myscript.py), Python sets __name__ to the special string "__main__".
- When another file imports yours (import myscript), Python gives that module its actual filename as the name, like "myscript".

So the condition if __name__ == "__main__": is only True in the first case. Anything inside the block runs only when you launch the file directly.

Why use it?
- Avoid accidental execution of test code or demos when your module is imported.
- Separate reusable logic (functions, classes) from “script‑only” actions (like printing results or running a demo).
- Make your code friendlier for others who might want to reuse your functions without the extra output.

Simple example

def greet(name: str) -> str:
"""Return a friendly greeting."""
return f"Hello, {name}!"

# This part runs only when you execute this file directly.
if __name__ == "__main__":
# Try it out!
print(greet("Alice"))


- If you run python hello.py, you’ll see Hello, Alice!.
- If another script does import hello and then calls hello.greet("Bob"), nothing is printed automatically; only the function runs when you ask for it.

Quick checklist

- Write reusable code (functions, classes) at the top level.
- Put demo or test calls inside the if __name__ == "__main__": block.
- Leave the block empty if you don’t need a demo—no harm in having it.

That’s it! With this tiny guard, you keep your modules clean, reusable, and safe from unwanted side effects. Happy coding!
❤1
🚀 Mini Project: Personal URL Launcher
Turn a few minutes of coding into a handy command‑line tool that lets you type a short nickname (e.g. gh) and instantly opens the full website in your default browser. The shortcuts are saved in a tiny JSON file, so they persist between sessions – no need to remember long URLs any more!

Why it’s useful
- Speed – One‑word commands replace copying and pasting long links.
- Portability – The JSON file is just text; copy it to any machine and you have the same shortcuts.
- Learning boost – You’ll see how Python turns data structures into files (serialization), how the standard library can launch external programs, and how a tiny user interface can be built with plain input().

Key concepts you’ll practice

- json module – converts Python objects (lists, dicts) to a text format that can be stored on disk and later restored. Think of it as “saving the state” of your shortcuts.
- File I/O (open, read, write) – the way Python talks to files on your computer.
- webbrowser module – a built‑in tool that tells the operating system to open a URL in the default web browser. No extra packages needed.
- Simple data validation – checking that a nickname isn’t already taken and that a string looks like a URL (starts with http:// or https://).

How it works – in plain steps

1. Load existing shortcuts – When the script starts, it tries to read shortcuts.json. If the file doesn’t exist, it starts with an empty dictionary.
2. Main loop – The program repeatedly asks you what you want to do:
- add – give a nickname and a full URL, the script validates and saves it.
- open – type a nickname, the script looks it up and asks webbrowser to launch the link.
- list – shows all stored shortcuts with a quick glance at how many times each has been opened.
- quit – exits cleanly, writing any changes back to the JSON file.
3. Persist changes – After each add or open, the updated dictionary is written back to shortcuts.json so the next run sees the same data.

A quick peek at the code (just enough to spark your imagination – you’ll fill in the details!):

(continued below 👇)
import json, os, webbrowser

FILE = "shortcuts.json"

# 1️⃣ Load existing data or start fresh
if os.path.exists(FILE):
with open(FILE, "r") as f:
shortcuts = json.load(f)
else:
shortcuts = {} # {"gh": {"url": "https://github.com", "hits": 0}}

def save():
"""Write the current dictionary to disk."""
with open(FILE, "w") as f:
json.dump(shortcuts, f, indent=2)

def add():
name = input("🆔 Nickname: ").strip()
if name in shortcuts:
print("❗ That nickname is already taken.")
return
url = input("🔗 Full URL: ").strip()
if not (url.startswith("http://") or url.startswith("https://")):
print("❗ URLs should start with http:// or https://")
return
shortcuts[name] = {"url": url, "hits": 0}
save()
print(f"✅ Added {name} → {url}")

def open_link():
name = input("🔎 Which shortcut? ").strip()
entry = shortcuts.get(name)
if not entry:
print("❌ No such nickname.")
return
webbrowser.open(entry["url"])
entry["hits"] += 1
save()
print(f"🌐 Opening {entry['url']}")

def list_links():
if not shortcuts:
print("📂 No shortcuts saved yet.")
return
for nick, data in shortcuts.items():
print(f"🔹 {nick} → {data['url']} (opened {data['hits']}×)")

# 2️⃣ Main interactive loop
while True:
cmd = input("\n💡 Command (add/open/list/quit): ").strip().lower()
if cmd == "add":
add()
elif cmd == "open":
open_link()
elif cmd == "list":
list_links()
elif cmd in ("quit", "exit"):
print("👋 Bye!")
break
else:
print("❓ Unknown command – try add, open, list, or quit.")


What’s happening under the hood?

- json.dump / json.load translate the in‑memory dictionary into readable text and back again. This is why you can open shortcuts.json in any editor and see something like {"gh": {"url": "https://github.com", "hits": 0}}.
- webbrowser.open hands the URL to the operating system, which then decides which program (Chrome, Firefox, etc.) should handle it.
- Counting hits ("hits" field) shows how you can enrich a simple mapping with extra metadata without extra complexity.

Take it further (optional)

- Add a search command that finds nicknames containing a substring.
- Store a timestamp of the last use with datetime.now().isoformat().
- Turn the script into an executable (python -m pyinstaller …) so you can run it with just url-launcher from anywhere.

Give it a try – a few dozen lines, a little tinkering, and you’ll have a personal “bookmark manager” that lives right in your terminal. Happy coding!
❤1
Ever tried to hand a whole list or dictionary to a function without typing each value one by one? Python lets you unpack those containers so the function receives each element as an individual argument. 🎁

Why it works
When you call a function, Python expects separate values for each parameter. The * operator tells Python, “take this iterable (like a list or tuple) and spread its items out as separate positional arguments.”
The ** operator does the same for a dictionary, but it spreads key‑value pairs as separate keyword arguments (the name = value form).

Simple example

def greet(name, age):
# f‑string makes it easy to embed variables in text
print(f"👋 Hello {name}, you are {age} years old!")

# 1️⃣ Using a list (or tuple)
info = ["Alice", 30]
greet(*info) # same as greet("Alice", 30)

# 2️⃣ Using a dictionary
data = {"name": "Bob", "age": 25}
greet(**data) # same as greet(name="Bob", age=25)

# 3️⃣ Mixing both – you can still pass extra arguments directly
def full_greet(name, age, city="Unknown"):
print(f"🌍 {name}, {age} – from {city}")

details = ("Charlie", 22)
more = {"city": "Paris"}
full_greet(*details, **more) # Charlie, 22 – from Paris


What happens under the hood?
- *info turns ["Alice", 30] into two separate values, exactly what the function signature expects.
- **data turns {"name": "Bob", "age": 25} into name="Bob", age=25.
If the number of items doesn’t match the parameters, Python raises a TypeError. That’s why the names in a dictionary must match the parameter names exactly.

When to use it
- API responses often come as dictionaries; unpack them straight into your helper functions.
- Batch processing: you have a list of arguments for many calls – func(*args_list) keeps the code tidy.
- Default values: combine explicit arguments with **kwargs to let callers override only what they need.

Key take‑aways
- *iterable → positional unpacking
- **dict → keyword unpacking
- Names in a dict must match parameter names
- Great for cleaner, more flexible code

Give it a try: fetch some JSON, turn it into a dict, and call your function with **json_data. You’ll see how Python’s unpacking makes data flow feel effortless! 🚀
❤1
Hey Python explorers! 🌟 Ever wish you had a tiny, always‑ready to‑go to‑do list right in your terminal? Let’s build a Command‑line Todo Manager you can finish in one sitting. It’s perfect for beginners because it leans on only the standard library plus one optional, lightweight third‑party helper for nice tables.

What the app does

- Add a task with a short description and a priority (1 = high, 5 = low).
- Show all pending tasks, nicely ordered by priority and creation time.
- Mark a task as done, which moves it out of the active list.
- Persist everything between runs so you never lose a note.

Why it works

- shelve behaves like a dictionary that lives on disk. When you assign db[task_id] = task_dict, Python automatically pickles (serializes) the object and writes it to a file. The next time you open the shelf you get the same data back – no manual file handling needed.
- datetime gives us timestamps. By storing datetime.now() when a task is created we can later sort tasks chronologically, and the strftime method lets us turn those timestamps into human‑readable strings.
- Sorting is just a one‑liner: sorted(tasks, key=lambda t: (t['priority'], t['created'])). The key function tells Python how to compare items, so higher‑priority (lower number) tasks appear first, and ties are broken by the older entry.
- If you want a prettier table, tabulate (install with pip install tabulate) turns a list of dicts into an aligned grid without you writing any formatting code.

Sketch of the code

import shelve
from datetime import datetime

DB_NAME = 'todos.db' # shelf file on disk
DATE_FMT = '%Y-%m-%d %H:%M' # how dates appear to the user

def add_task(desc: str, priority: int = 3):
"""Create a new task and store it."""
with shelve.open(DB_NAME) as db:
task_id = str(int(time.time() * 1000)) # simple unique key
db[task_id] = {
'desc': desc,
'priority': priority,
'created': datetime.now(),
'done': False
}
print(f'✅ Task added with ID {task_id}')

def list_tasks():
"""Show all pending tasks, sorted by priority then date."""
with shelve.open(DB_NAME) as db:
tasks = [v | {'id': k} for k, v in db.items() if not v['done']]
tasks.sort(key=lambda t: (t['priority'], t['created']))

# optional pretty table
try:
from tabulate import tabulate
table = tabulate(
[[t['id'], t['desc'], t['priority'], t['created'].strftime(DATE_FMT)]
for t in tasks],
headers=['ID', 'Description', 'Prio', 'Created'],
tablefmt='simple')
print(table)
except ImportError:
for t in tasks:
print(f"{t['id']}: [{t['priority']}] {t['desc']} (added {t['created']:{DATE_FMT}})")

def complete_task(task_id: str):
"""Mark a task as done."""
with shelve.open(DB_NAME, writeback=True) as db:
if task_id in db:
db[task_id]['done'] = True
print(f'✔️ Task {task_id} completed!')
else:
print('❌ No such task.')

# ----------------------------------------------------------------------
# Quick interactive demo (run the file and follow the prompts)
if __name__ == '__main__':
import time
while True:
cmd = input('\n[t] Add [l] List [c] Complete [q] Quit > ').strip().lower()
if cmd == 't':
d = input('Description: ')
p = int(input('Priority (1‑5, 1=high): ') or 3)
add_task(d, p)
elif cmd == 'l':
list_tasks()
elif cmd == 'c':
tid = input('Task ID to finish: ')
complete_task(tid)
elif cmd == 'q':
break


How to run it

(continued below 👇)
1. Save the script as todo.py.
2. (Optional) pip install tabulate for a nice table view.
3. Run python todo.py and start adding tasks. Your list lives in todos.db and will be there next time you launch the program.

Why you’ll love it

- Instant payoff – add a task, see it appear, mark it done.
- Learn core tools – shelve for persistence, datetime for timestamps, list sorting, and a glimpse of third‑party packaging.
- Expandable – later you could add due dates, tags, or even a tiny web UI with Flask.

Give it a try, tweak the priority scale, or sprinkle in colors with colorama. In just one coffee break you’ll have a fully functional, persistent to‑do list that lives in the terminal! 🚀
❤1
What’s the difference between a “copy” and a “reference” in Python?
When you write new_list = old_list, you’re not making a fresh list – you’re just giving the same list another name. Both names point to the exact same object in memory. If you change the list through either name, the change shows up everywhere.

Why does this happen?
Python stores objects (like lists, dictionaries, strings) somewhere in memory and gives each object a unique ID. Variables are just labels that point to those IDs. Assigning a variable to another variable copies the label, not the underlying data.

original = [1, 2, 3]      # create a list
alias = original # alias points to the same list
alias.append(4) # modify through the alias
print(original) # 👉 [1, 2, 3, 4] – the original changed!
print(id(original), id(alias)) # 👉 same number, same object


How to make a true copy?
Use one of the following methods so the new variable gets its own list (its own ID).

Quick ways to copy a list

- Slice syntax – copy = original[:]
- list() constructor – copy = list(original)
- copy module – import copy; copy = copy.copy(original) (shallow)
- Comprehension – copy = [x for x in original]

original = [1, 2, 3]
shallow_copy = original[:] # slice copy
shallow_copy.append(5)

print("original :", original) # 👉 [1, 2, 3] – untouched
print("shallow_copy :", shallow_copy) # 👉 [1, 2, 3, 5]
print(id(original), id(shallow_copy)) # 👉 different numbers


When does a shallow copy still share data?
If the list contains other mutable objects (like another list), a shallow copy copies only the outer list’s references. The inner objects are still shared.

nested = [[1, 2], [3, 4]]
shallow = nested[:]
shallow[0].append(99) # modify inner list

print("nested :", nested) # 👉 [[1, 2, 99], [3, 4]] – changed!
print("shallow :", shallow) # 👉 same change


To completely separate nested structures, use a deep copy:

import copy
deep = copy.deepcopy(nested)
deep[0].append(100)

print("nested :", nested) # 👉 [[1, 2, 99], [3, 4]]
print("deep :", deep) # 👉 [[1, 2, 99, 100], [3, 4]]


Key take‑aways (bolded list)
- Reference = another name for the same object → changes affect all names.
- Shallow copy = new container, but inner items are still shared.
- Deep copy = completely independent copy, even for nested objects.

Tip for beginners: Whenever you’re about to modify a list that you also need in its original form later, make a copy first. It saves you from those puzzling bugs where data changes “by magic”.
❤1
Idea: Build a tiny command‑line journal that lets you add, list, and search personal entries, all saved in a single JSON file. You’ll get a useful “digital diary” you can open from any terminal, and you’ll practice file handling, data serialization, and working with dates.

Why it’s cool:
- Instantly useful – you can start recording thoughts, ideas, or daily logs right away.
- One‑file storage – JSON turns a Python dictionary into readable text, so you can peek at the file with any editor.
- No extra dependencies – only the standard library (json, datetime, os), perfect for beginners.

How it works, step by step

1. Data model – each entry is a dictionary with two keys:
- "timestamp" – a string like 2026‑10‑03 14:22 generated by datetime.now().strftime(...).
- "text" – the raw note you typed.
All entries live inside a list stored under the top‑level key "entries" in the JSON file.

2. Loading & saving – when the program starts it tries to read the JSON file (journal.json).
- If the file is missing or empty, we start with {"entries": []}.
- json.load() parses the text into a Python dict; json.dump() writes the updated dict back, with indent=2 for readability.

3. Adding an entry – we prompt the user with input("Your note: ").
- The current time is fetched with datetime.now().
- A new dict is appended to the "entries" list, then we immediately write the file so nothing is lost.

4. Listing entries – we loop over the stored list and print each timestamp followed by the note.
- Using enumerate() (still allowed) gives us a simple numeric index, but the core idea is just iterating a list.

5. Searching – the user types a keyword; we filter entries whose "text" contains that substring (case‑insensitive) using a list comprehension.
- This shows how to filter data without extra libraries.

6. Simple menu – a while True loop prints options ([A]dd, [L]ist, [S]earch, [Q]uit) and reacts to the first character of the input, keeping the program interactive without any GUI.

Key concepts you’ll see in the code

- File I/O (open, with statement) – guarantees the file is closed automatically.
- json module – turns Python objects into text and back, making persistence easy.
- datetime formatting – human‑readable timestamps, useful for sorting or later analysis.
- Functions – keep the code tidy (load_journal(), save_journal(), add_entry(), …).
- Simple error handling – try/except around json.load() to handle a brand‑new file.

Mini‑example (core part only)

(continued below 👇)
import json, os
from datetime import datetime

JOURNAL_FILE = "journal.json"

def load_journal():
if not os.path.exists(JOURNAL_FILE):
return {"entries": []}
with open(JOURNAL_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except json.JSONDecodeError:
return {"entries": []}

def save_journal(data):
with open(JOURNAL_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)

def add_entry():
note = input("🖊️ Your note: ").strip()
if not note:
print("❗ Nothing written, skipping.")
return
entry = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
"text": note
}
journal = load_journal()
journal["entries"].append(entry)
save_journal(journal)
print("✅ Entry saved!")

def list_entries():
journal = load_journal()
for i, e in enumerate(journal["entries"], 1):
print(f"{i}. [{e['timestamp']}] {e['text']}")

def search_entries():
term = input("🔍 Search keyword: ").lower()
journal = load_journal()
matches = [e for e in journal["entries"] if term in e["text"].lower()]
for e in matches:
print(f"[{e['timestamp']}] {e['text']}")

# ---- simple interactive loop ----
while True:
choice = input("\n[A]dd [L]ist [S]earch [Q]uit: ").strip().lower()
if choice == "a":
add_entry()
elif choice == "l":
list_entries()
elif choice == "s":
search_entries()
elif choice == "q":
print("👋 Bye!")
break
else:
print("❓ Unknown option.")


Run the script, type a to add a note, l to see everything, or s to hunt for a keyword. All data lives safely in journal.json, and you can open that file any time to back it up or migrate it.

Next steps (optional)
- Add a delete option (remove by index).
- Sort entries chronologically when listing.
- Export to a plain‑text markdown file for easy reading.

Give it a try – a few minutes of coding, and you’ll have a personal digital journal you control completely!
❤1