Python Tutorial : Writing Efficient Python Code

Logan Thomas teaches how to write Python code that runs fast and uses fewer resources. The course covers timing, profiling, and optimization using the standard library, NumPy, and pandas for data science tasks like cleaning, deployment, and visualization.
Chapters
Hello and welcome. My name is Logan Thomas and I'll be your guide through this course about writing efficient code using Python. As a data scientist, the majority of your time should be spent gleaning actionable insights from data. Whether you're cleaning and curating a messy data set, deploying a machine learning model, or creating a sleek data visualization, the code you write should be a helpful tool to quickly get you where you need to go, not something that leaves you waiting around. In this course, you'll learn how to write cleaner, faster, and more efficient Python code. We'll explore how to time and profile your code in order to find potential bottlenecks. Then you'll practice eliminating these bottlenecks and other bad design patterns using Python's standard library, numpy, and Pandas. After completing this course, you'll have everything you need to start writing elegant and efficient Python code. But first, let's explore what is meant by efficient Python code. In the context of this course, efficient refers to code that satisfies two key concepts. First, efficient code is fast and has a small latency between execution and returning a result. Second, efficient code allocates resources skillfully and isn't subjected to unnecessary overhead. Although your definition of fast runtime and small memory usage may depend on the task at hand, the goal of writing efficient code is still to reduce performance, both latency and overhead. For the remainder of this course, we'll be exploring how to write Python code that runs quickly and has little memory overhead. We've defined what is meant by efficient code, but it's also important to note this course focuses on writing efficient code using Python. Python is a language that prides itself on code readability, and thus it comes with its own set of idioms and and best practices. Writing Python code the way it was intended is often referred to as Pythonic code. This means the code you write follows the best practices and guiding principles of Python. Pythonic code tends to be less verbose and easier to interpret. Although Python supports code that doesn't follow its guiding principles, this type of code tends to run slower. As an example, look at the non Pythonic code in this slide. Not only is this code more verbose than the Pythonic version, it takes longer to run. We'll take a closer look at why this is the case later on in the course, but for now, the main takeaway here is that Pythonic code is efficient code. In the previous slide, I discussed the importance of following Python's guiding principles and idioms. But what are these idioms, you may ask. Enter the Zen of Python. This is a list of a few idioms and best practices that summarize Python's design philosophy. Take a moment to familiarize yourself with these principles. Chances are, if you've been working with Python for some time, you are already familiar with most of them. If not, don't worry, we'll be touching on a few of these concepts throughout the course. Before moving on, we should check your Python knowledge level. There are a few things this course assumes you have a working knowledge of. You don't need to be an expert on the topics listed here, but you should definitely be familiar with them in order to get the most out of this course. Now that we've defined efficient and Pythonic code and touched on a few things you should already be familiar with, it's time to.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







