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
Channel created
Channel photo updated
🎉2
❤1
❤2
Channel name was changed to «Python»
Channel name was changed to «Python Learning»
Channel photo updated
Channel name was changed to «Python Programming»
Live stream started
Channel photo updated
🚀 Boost your file handling with pathlib!

Tired of fiddling with string concatenations for paths? pathlib lets you treat file paths like objects – safe, readable, and cross‑platform.

from pathlib import Path

# 📁 Build a path to "data/report.txt" inside the user's home folder
home = Path.home()
report_path = home / "data" / "report.txt"

# ✅ Check if it exists, read its content, and print the first line
if report_path.is_file():
first_line = report_path.read_text().splitlines()[0]
print(f"🗒️ First line: {first_line}")
else:
print("❌ File not found!")


Why you’ll love it:
- No more os.path.join headaches.
- Works the same on Windows, macOS, Linux.
- Handy methods like .is_file(), .read_text(), .write_text() keep code clean.

Give it a try and say goodbye to brittle path strings! 🎉
💡 Quick tip: the Walrus operator (:=) lets you assign and test a value in a single line. Great for reading input, looping over data, or any situation where you’d otherwise need two statements.

# Read numbers until a non‑numeric line appears
while (line := input("Enter a number (or anything else to stop): ")) .isdigit():
number = int(line)
print(f"✅ You entered {number}")

print("🚪 Done!")


What’s happening?
- line := input(...) captures the user’s input.
- The same expression is immediately checked with .isdigit().

No extra line = input(...) line, no duplicate calls—cleaner and faster to write. Give it a try in your next script! 🚀
💡 Silently ignore the errors you expect – without a try/except clutter

When you have a piece of code that may raise a known exception (e.g., trying to delete a file that might already be gone), wrapping it in a full try/except block can feel noisy.
contextlib.suppress does the same thing in a single line and keeps the flow readable.

from contextlib import suppress
import os

# Remove a temp file if it exists – ignore the FileNotFoundError otherwise
with suppress(FileNotFoundError):
os.remove('temp_data.json')


✅ Why you’ll love it
- No extra indentation levels – your main logic stays front‑and‑center.
- Explicit about which exception is being ignored, so it’s safe and self‑documenting.
- Works with any exception type, even custom ones.

Give it a try next time you need a “try‑and‑forget” cleanup! 🚀
❤1
🚀 One‑liner to count & rank items in a list

Ever needed a quick frequency report (e.g., most used words, top error codes) without writing a loop? 🎯 collections.Counter does it in a single line and gives you handy helpers like most_common.

from collections import Counter

data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
top = Counter(data).most_common(2) # → [('apple', 3), ('banana', 2)]

print(top)


Why it’s cool
- Fast – implemented in C, O(n) time.
- Readable – the intent is obvious at a glance.
- Flexible – most_common() can return all items or just the top N.

Next time you need a quick tally, reach for Counter and skip the manual dictionary gymnastics! 🌟