|
Hi Reader, There's a gift for you somewhere in this email... just look for the 🎁 emoji! 👉 Tip #39: Six quick Python tricksHere's what I'll cover below:
Let's get started! 👇 1️⃣ Return the number of unique values Need to know the number of unique values in an iterable? Convert it to a set and check the length: A set is a collection of values (like a list), except it can't contain any duplicate values: You can use this trick with any iterable, including strings: 2️⃣ Count values with Counter Need to know how many times each letter appears? Use the Counter class: Counter objects act like dictionaries, which means that if we want to know how many times 'a' appears, we pass it the 'a' key: Counters have useful methods like most_common, which returns the specified number of most common values: 3️⃣ Better debugging with f-strings You've probably used f-strings for substitution: But f-strings can also help with debugging! Normally you might print out variables within a function to check that it's working as expected: But with self-documenting expressions (new in Python 3.8), the output is more useful: The secret is to end each replacement field with an equals sign! 4️⃣ Return multiple values from a function Let's say that your function needs to return multiple values: The function returns a tuple: But if you want those return values as separate objects, you can use multiple assignment (also known as tuple unpacking): 5️⃣ Count while looping Let's say that you need to loop through a list: Sometimes you need to keep track of the index while looping. Don't do this: Use enumerate instead, which keeps track of the index for you: It actually generates a tuple, which we're unpacking into i and word! 6️⃣ Create a dictionary with a comprehension List comprehensions are useful all over the place in Python: By slightly tweaking the syntax, you can write a dictionary comprehension instead: 📈 Going furtherMany of these tricks are taken from my online course, Python Essentials for Data Scientists:
🎁 Just for fun, I'm giving readers of Tuesday Tips a 25% discount on this course for the next week. Use code TUESDAY25 at checkout! 🎁 👋 Until next timeDid you like this week’s tip? Please send it to a friend or share this link on social. It really helps me out! 🙌 See you next Tuesday! - Kevin P.S. Uber's new "Save with Ads" option (parody) 😂 Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week! |
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, Data School's All-Access Pass is now on sale: Buy the pass for $99 With the All-Access Pass, you get access to ALL of my courses for a year, including every NEW course I launch during your subscription! That includes a secret new course I'll be launching on January 1 🤫 The $99 sale ENDS in one week, on Cyber Monday (November 30). By purchasing now, you'll lock in this discounted price for the lifetime of your subscription, which will get more valuable as I add more courses! Buy the...
Hi Reader, For 12 years, I've been teaching scikit-learn. It's the ideal tool for Machine Learning in Python, given its broad functionality, community support, and high-quality documentation. But there's a HUGE difference between documentation and an effective user manual. That's why I published Master Machine Learning with scikit-learn: I wanted to provide battle-tested guidance for achieving superior Machine Learning results. (Side note: Amazon US is currently offering a 34% discount on my...
Hi Reader, I hope you're having a great September! 🍂 Two quick announcements for you: 1️⃣ I'm working on two mystery courses right now: The first is an ML course, and the second has nothing to do with Data Science! 🤯 Can you guess what the second course is about? 2️⃣ On Monday, I'm raising the price of my Master Machine Learning book from $19 to $29. But until Monday, you can still snag a paperback copy for $19! The ebook will continue to be priced at $19, and you can still read the book...