Writing Efficient Python Code
Learn to time and profile Python code, find bottlenecks, and rewrite slow loops with built-ins, sets, NumPy arrays and pandas methods.
Slow Python code costs time and computational resources, and the fix usually starts with knowing where that time goes. Here you measure runtime and memory use to locate bottlenecks, then replace them with faster patterns built on Python’s built-in functions and modules, NumPy arrays and pandas. The exercises use themed data such as Pokémon names and types and baseball statistics, and the profiling lessons time each version of the code before and after a fix.
What You Will Achieve
- Time competing versions of a snippet with %timeit and choose the faster approach
- Read line_profiler and memory_profiler output to find the lines that slow code down or use the most memory
- Swap hand-written loops for built-in functions, Counter objects, set operations and vectorized NumPy arrays
- Choose the most performant way to iterate over or apply a function to a pandas DataFrame
Program Overview
Foundations for Efficient Python Code
Sets out what efficient code means in Python and practices the Standard Library’s built-in tools alongside NumPy arrays.
- The Zen of Python and what efficiency means
- Built-ins range(), enumerate() and map()
- Python’s Standard Library
- Working with NumPy arrays
Timing and Profiling Code
Gathers and compares runtimes, then profiles code line by line for time and memory to locate bottlenecks and replace them.
- Runtime comparisons with %timeit, including runs and loops
- Line profiling with %lprun
- Memory profiling with %mprun
- Fixing a bottleneck once it is found
Gaining Efficiencies with Sets, Loops and pandas
Applies built-in modules, set theory and better looping patterns, then turns to iterating over pandas DataFrames and applying functions to them.
- Efficient combining, counting and iterating
- Set theory for comparing and searching collections
- Eliminating loops and moving one-time calculations out of them
- pandas .iterrows(), .itertuples(), .apply() and underlying arrays in place of .iloc
Certificate Awarded (Paid)
Complete the course to earn it.
Job Outlook
These skills apply wherever Python code works with data, including data analysis, data engineering and software development work that relies on NumPy arrays and pandas DataFrames.
Good for: Python users who already write working data code and want it to run faster and use less memory.
Keep in mind: The pandas chapter is a brief introduction to DataFrame iteration and .apply(), and the profiling lessons rely on the %lprun and %mprun commands of line_profiler and memory_profiler.
Bottom line: Its strength is the habit it builds: measure Python code with %timeit and the profilers first, then rewrite the slow parts with built-ins, NumPy or pandas.
Frequently Asked Questions
Which profiling tools does the course use?
Runtime comparisons use %timeit, including how to set the number of runs and loops. Line-by-line runtime profiling uses %lprun from the line_profiler package, and memory profiling uses %mprun from the memory_profiler package.
What data do the exercises work with?
The practice sets are themed: a Festivus exercise, Star Wars profiling and hero BMI calculations, Pokémon names, types and Pokédexes for set theory and loops, and baseball statistics for run differentials and win percentage predictions in pandas.
Which pandas iteration methods are compared?
The pandas chapter works through .iterrows() and .itertuples(), shows .apply() as an alternative to looping, and closes with optimal pandas iterating, where .iloc lookups are replaced with the DataFrame's underlying arrays.
Are transcripts or a glossary available while studying?
Yes. The videos carry live transcripts that can be revealed from the bottom left of the player, and a course glossary sits in the resources section on the right of the course page.
User Reviews
Write your review
You must be logged in to post a review.
Reviews can only be posted by registered learners. Log in or create a free account, then you will return here to post.


There are no reviews yet.