OnePythonTip

101 posts

OnePythonTip banner
OnePythonTip

OnePythonTip

@OnePythonTip

Daily bite-sized #Python & #DataScience gems. Maker of cool tees for devs. #MachineLearning #DataAnalytics #Programming #Coding #WebDeveloper

参加日 Temmuz 2023
22 フォロー中1 フォロワー
OnePythonTip
OnePythonTip@OnePythonTip·
hasattr checks if attribute exists. getattr gets attribute with optional default. Perfect for: - Avoiding AttributeError - Dynamic method calls - Config files - Data parsing One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
0
OnePythonTip
OnePythonTip@OnePythonTip·
💡 enumerate() works with ANY iterable. # Tuples for i, val in enumerate((10, 20)): print(i, val) # 0 10, 1 20 # Files with open("data.txt") as f: for i, line in enumerate(f, start=1): print(f"{i}: {line.strip()}") # line numbers! What do you use enumerate for?
English
0
0
0
3
OnePythonTip
OnePythonTip@OnePythonTip·
Stop using range(len()) to loop through lists. ❌ Ugly: for i in range(len(list)): print(i, list[i]) ✅ Clean: for i, item in enumerate(list): print(i, item) enumerate() gives you index AND value in one line. One tip a day. Follow for more. #Python #LearnToCode 🐍
OnePythonTip tweet media
English
1
0
0
2
OnePythonTip
OnePythonTip@OnePythonTip·
💡 setdefault vs defaultdict: setdefault: works on ANY dict, one-time default. defaultdict: needs import, default for ALL missing keys Use setdefault when you only need default occasionally. Use defaultdict when EVERY missing key needs default. Which one do you use?
English
0
0
0
5
OnePythonTip
OnePythonTip@OnePythonTip·
setdefault sets a default value if key is missing. Perfect for: - Grouping data - Building indexes - Caching One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
1
0
0
4
OnePythonTip
OnePythonTip@OnePythonTip·
Counter is a dictionary for counting things. Manual way: counts = {} for item in items: counts[item] = counts.get(item, 0) + 1 Counter way: from collections import Counter counts = Counter(items) One tip a day. Follow for more. #Python #DataScience 🐍
OnePythonTip tweet media
English
0
0
0
4
OnePythonTip
OnePythonTip@OnePythonTip·
The cycle: "I just need requests" *installs 47 libraries* 3 months later: "I don't remember what half of these do" But I MIGHT need them... 👕 This addiction is becoming a shirt. "import sanity" maybe. Link in bio. One tip a day. Follow for more. #Python #Coding
OnePythonTip tweet media
English
0
0
0
4
OnePythonTip
OnePythonTip@OnePythonTip·
Flatten nested lists with chain.from_iterable(). Nested = [[1,2], [3,4], [5,6]] chain.from_iterable (nested) → [1,2,3,4,5,6] Why better than manual loops: - Faster (C implementation) - Cleaner - Memory efficient One tip a day. Follow for more. #Python #DataScience 🐍
OnePythonTip tweet media
English
0
0
0
3
OnePythonTip
OnePythonTip@OnePythonTip·
💡 These exist because YOU voted and complained in replies. Every shirt comes from a real developer pain point. Next design ideas brewing: - "I'll comment later" - "import sanity" - "I am the bug" What's the next shirt I should make? #Python #Coding 👕
OnePythonTip tweet media
English
0
0
0
2
OnePythonTip
OnePythonTip@OnePythonTip·
partial creates a new function with some arguments pre-filled. With partial: square = partial(power, exponent=2) Perfect for: - Specializing general functions - Callbacks with fixed parameters - Reducing repetition One tip a day. Follow for more. #Python #DataScience 🐍
OnePythonTip tweet media
English
0
0
0
2
OnePythonTip
OnePythonTip@OnePythonTip·
The developer experience: Me: "I know exactly what I'm doing" My code: 💥 Me: "One small tweak" My code: 💥💥💥 Me: "I'll just rewrite it" My code: 💀 Me 3 hours later: "I wrote 3 lines" 👕 This is the most relatable shirt yet. Link in bio. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
2
OnePythonTip
OnePythonTip@OnePythonTip·
__name__ == "__main__" lets code run ONLY when you execute the file directly (not when imported). Use it for: - Tests - Demos - CLI interfaces One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
3
OnePythonTip
OnePythonTip@OnePythonTip·
💡 My "pro" moments: 1. Using enumerate() instead of range(len()) 2. List comprehensions 3. defaultdict for grouping 4. Writing my first decorator (felt like magic) What was YOUR moment? Be honest. #Python #LearnToCode 🐍
OnePythonTip tweet media
English
0
0
0
2
OnePythonTip
OnePythonTip@OnePythonTip·
@property lets you use methods like attributes. Without property: circle.get_radius() circle.set_radius(10) With property: circle.radius circle.radius = 10 Perfect for: Validation, computed values, read-only attributes. One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
0
OnePythonTip
OnePythonTip@OnePythonTip·
The cycle: Monday: "Works fine" Tuesday: "Need one more parameter" Wednesday: "I'll refactor later" 6 months later: 💀 Refactor NOW. Your future self will send flowers. 👕 This is 100% becoming a shirt. Link in bio. One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
0
OnePythonTip
OnePythonTip@OnePythonTip·
Complete error handling has four parts: try: # Risky code except: # Handle errors else: # Runs if NO error finally: # ALWAYS runs (cleanup) One tip a day. Follow for more. #Python #Coding 🐍
OnePythonTip tweet media
English
0
0
0
0
OnePythonTip
OnePythonTip@OnePythonTip·
f-strings can format numbers, dates, and alignment. Numbers: {value:.2f} # 2 decimals {value:,} # commas {value:.1%} # percentage Dates: {now:%Y-%m-%d} # 2026-06-14 Alignment: {name:<10} # left align One tip a day. Follow for more. #Python #DataScience 🐍
OnePythonTip tweet media
English
0
0
0
0
OnePythonTip
OnePythonTip@OnePythonTip·
The cycle every developer knows: 9pm: "One small fix" 9:15pm: "That broke something else" 11pm: "IT WORKS" 11:01pm: *new bug appears* 3am: "I am the bug" 👕 This spiral is becoming a shirt. Link in bio if you've been here. One tip a day. Follow for more. #Python #Coding 🐍👕
OnePythonTip tweet media
English
0
0
0
1
OnePythonTip
OnePythonTip@OnePythonTip·
sorted() with key lets you define custom sorting rules. Default: sorts by value Examples: words = ["cat", "elephant", "dog"] sorted(words, key=len) # ['cat', 'dog', 'elephant'] One tip a day. Follow for more. #Python #LearnToCode 🐍
OnePythonTip tweet media
English
0
0
0
2