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! 🚀