Python Empty Collections — Common Mistake [] # List () # Tuple {} # Dictionary (NOT a set) set() # Set Trick to Remember: 👉 {} = key:value → dictionary 👉 set() = only way to make empty set Example type({}) # dict type(set()) # set If you remember just this: “Curly braces empty = dict, not set” You’ll never make this mistake again. Source: https://lnkd.in/d8sbr7gc #Python
Python Empty Collections: Dictionary vs Set
More Relevant Posts
-
🚀 #100DaysOfPython – Day 4: map(), filter(), reduce() These are powerful functional tools in Python 👇 👉 map() – transform nums = [1, 2, 3] squares = list(map(lambda x: x*x, nums)) 👉 filter() – select evens = list(filter(lambda x: x % 2 == 0, nums)) 👉 reduce() – accumulate from functools import reduce total = reduce(lambda a, b: a + b, nums) 💡 But in real-world Python? List comprehensions are often preferred for readability. 🔍 My takeaway: Understand these concepts—but choose readability over cleverness. #Python #FunctionalProgramming #100DaysOfCode
To view or add a comment, sign in
-
Python Strings & Formatting String Formatting f-strings (Python 3.6+, recommended – cleanest): print(f"My name is {name} and I am {age}") # My name is Joy and I am 30 print(f"Next year, I will be {age + 1}") # Next year, I will be 31 String Methods phrase = "Hello World" print(phrase.lower()) # hello world print(phrase.upper()) # HELLO WORLD print(phrase.count('l')) # 3 print(phrase.find('World')) # 6 print(phrase.replace('World', 'Universe')) # Hello Universe Key Takeaways: - Multiple ways to format strings - f-strings = cleanest & recommended - Strings are immutable - .find() gives index, .count() counts chars #Python
To view or add a comment, sign in
-
-
This is a post about interesting behavour of "del" in Python. Do you think that "del x" calls x.__del__()? Well... >>> class Foo: ... def __del__(self): ... print("Deleting") >>> bar = Foo() >>> baz = bar >>> del bar Nothing is printed. Why? Now try >>> del baz Deleting The problem here is that bar and baz are the same object, and what "del" does is - removes the name (done) - decrements reference count for object (done, after "del bar" it is 1, baz still refers to the same object) - calls __del__, *if reference count is 0* (NOT done after "del bar", as the reference count is 1) When "del baz" is called, reference count finally reaches 0, and __del__ is called. While this kinda makes sense, it can be surprising and non-intuitive, if you don't really understand what is happening behind the scene. #python #gotcha
To view or add a comment, sign in
-
🐍 Python Interview Question 📌 What is the difference between range() and xrange()? 🔹 range() ✔ In Python 3, range() returns a lazy sequence object ✔ Generates numbers only when needed ✔ Memory efficient for large loops 🔹 xrange() ✔ Available only in Python 2 ✔ Returns an iterator-like object instead of a full list ✔ Designed for better memory efficiency in Python 2 🔹 Important Note: ✔ In Python 3, xrange() was removed ✔ range() now behaves like Python 2 xrange() 💡 In Short: Use range() in Python 3 — it already provides the memory-efficient behavior of old xrange() ⚡ 👉For Python Course Details Visit : https://lnkd.in/gf23u2Rh . #Python #PythonProgramming #Range #Xrange #CodingInterview #InterviewPreparation #TechLearning #AshokIT
To view or add a comment, sign in
-
-
If you work with Python, have you ever wondered: • What are magic methods? • Are they the same as special methods? • What about “dunder methods”? First thing is, magic methods are not really magic.They are just special methods defined by the Python Data Model. And, guess what, magic methods, special methods and dunder methods are all the same thing. Amazing, right? Let’s look at a simple example: 𝗰𝗹𝗮𝘀𝘀 𝗠𝘆𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻: 𝗱𝗲𝗳 __𝗹𝗲𝗻__(𝘀𝗲𝗹𝗳): 𝗿𝗲𝘁𝘂𝗿𝗻 𝟰𝟮 Now, when you call: 𝗹𝗲𝗻(𝗼𝗯𝗷) You’re NOT calling your method directly, Python is. This is the key insight and it is very important as we are going to see on another post. Takeaway: “Magic methods” are not magic. They are contracts with the Python interpreter. Implementing such methods are going to open a lot of new doors and it is a very pythonic whay of implementing your code. #python #magicmethods #dundermethods #specialmethods
To view or add a comment, sign in
-
👉 We all use quotes in Python… But do you know when to use: ' vs " vs '''? Most beginners just use them randomly. Here’s the simple rule 👇 # Single quotes → simple text name = 'Ali' # Double quotes → when text has ' msg = "It's a good day" # Triple quotes → multi-line / docstrings text = '''This is multi-line text''' That’s it. No confusion. No overthinking. --- 💡 Good code is not just about working… It’s about being clear and readable. --- Do you follow this… or just use quotes randomly? #Python #LearnPython #CodingBasics #ProgrammingConcepts #PythonTips #CodeClarity #CleanCode #LearnWithMe #strings
To view or add a comment, sign in
-
-
⚡ Python Tip: list.append(x) vs list.extend(x) 👉 append(): Adds one element [1,2].append([3,4]) → [1,2,[3,4]] 👉 extend(): Adds elements individually [1,2].extend([3,4]) → [1,2,3,4] 📌 Small difference. Big impact. #Python #CodingTips #LearnPython
To view or add a comment, sign in
-
🐍 Quick Python Quiz! 📌 Question 1: Which Python collection allows duplicates? A) set (😂) B) dict (🔥) C) list (❤️) D) frozenset (👍) ----- 📌 * Question 2: Which of these is immutable in Python? A) list (👍) B) set (🔥) C) tuple (😂) D) dict (❤) ------- 📌 * Question 3: What is the key difference between set and list? A) set is ordered (👍) B) list removes duplicates (😂) C) set has no duplicates (❤) D) list is immutable (🔥) ------- #Python #PythonQuiz #Coding #Programming #LearnPython #Tech #Developer #CodingLife #PythonBasics #InterviewPrep #ITJobs #AshokIT Follow @ashokit_official for more updates 🚀
To view or add a comment, sign in
-
🚀 #100DaysOfPython – Day 1: List Comprehension Starting my Python journey by revisiting one of the most elegant features in Python – List Comprehension. 👉 It provides a concise way to create lists. Instead of writing: squares = [] for i in range(5): squares.append(i*i) You can simply write: squares = [i*i for i in range(5)] ✨ Cleaner ✨ More readable ✨ More Pythonic 💡 You can also add conditions: even_squares = [i*i for i in range(10) if i % 2 == 0] 📌 Why it matters? - Reduces lines of code - Improves readability (when used correctly) - Widely used in real-world Python codebases 🔍 My takeaway: List comprehensions are powerful, but overusing them can hurt readability. Keep them simple! #Python #CodingJourney #LearnPython #100DaysOfCode #WomenInTech
To view or add a comment, sign in
-
Python & p-values: beyond the code 📊 Running statistical tests in Python is straightforward. Interpreting the results is where real value lies. A p-value may show statistical significance, but decision-makers care about what it means in practice. Using Python: from scipy import stats stat, p = stats.ttest_ind(group1, group2) • p ≤ 0.05 → Evidence of a difference • p > 0.05 → No strong evidence Key insight: Python provides the result, but your value as an analyst comes from turning that result into clear, actionable insight. Save this for your next analysis.
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development