|
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, Until 8 PM ET tonight, you can get the All-Access Pass for $99: Here's everything you need to know: Access all existing courses for one year ($700+ value) Includes new courses launched during your subscription Includes e-book version of Master Machine Learning (coming soon) Additional discounts available Lock in this price forever 30-day refund policy Get the Pass for $99 Questions? Please let me know! - Kevin
Hi Reader, I wanted to share with you three limited-time resources for improving your Python skills... 1οΈβ£ Algorithm Mastery Bootcamp π₯Ύ Are you looking for an intense, 12-day Python bootcamp? My friend Rodrigo GirΓ£o SerrΓ£o is running a new Algorithm Mastery Bootcamp, and it starts in just 5 days! In the bootcamp, you'll solve 24 real programming challenges and participate in daily live sessions to discuss and compare solutions. It's a great way to strengthen your problem-solving muscles πͺ I...
Hi Reader, Last week, I launched the All-Access Pass, which gives you access to ALL of Data School's courses for one year. Through Black Friday, you can buy the pass for $99, after which the price will increase. Here are the included courses: Build an AI chatbot with Python ($9) Create your first AI app in 60 minutes using LangChain & LangGraph! β‘ Build AI agents with Python ($99) Develop the skills to create AI apps that can think and act independently π€ Conda Essentials for Data Scientists...