Writing Efficient Python Code
4.7+
34 reviewsIntermediate
Learn to write efficient code that executes quickly and allocates resources skillfully to avoid unnecessary overhead.
Start Course for Free4 Hours15 Videos53 Exercises96,546 Learners
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.Loved by learners at thousands of companies
Course Description
As a Data Scientist, the majority of your time should be spent gleaning actionable insights from data -- not waiting for your code to finish running. Writing efficient Python code can help reduce runtime and save computational resources, ultimately freeing you up to do the things you love as a Data Scientist. In this course, you'll learn how to use Python's built-in data structures, functions, and modules to write cleaner, faster, and more efficient code. We'll explore how to time and profile code in order to find 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 the necessary tools to start writing efficient Python code!
- 1
Foundations for efficiencies
FreeIn this chapter, you'll learn what it means to write efficient Python code. You'll explore Python's Standard Library, learn about NumPy arrays, and practice using some of Python's built-in tools. This chapter builds a foundation for the concepts covered ahead.
Welcome!50 xpPop quiz: what is efficient50 xpA taste of things to come100 xpZen of Python50 xpBuilding with built-ins50 xpBuilt-in practice: range()100 xpBuilt-in practice: enumerate()100 xpBuilt-in practice: map()100 xpThe power of NumPy arrays50 xpPractice with NumPy arrays100 xpBringing it all together: Festivus!100 xp - 2
Timing and profiling code
In this chapter, you will learn how to gather and compare runtimes between different coding approaches. You'll practice using the line_profiler and memory_profiler packages to profile your code base and spot bottlenecks. Then, you'll put your learnings to practice by replacing these bottlenecks with efficient Python code.
Examining runtime50 xpUsing %timeit: your turn!100 xpUsing %timeit: specifying number of runs and loops50 xpUsing %timeit: formal name or literal syntax100 xpUsing cell magic mode (%%timeit)50 xpCode profiling for runtime50 xpPop quiz: steps for using %lprun50 xpUsing %lprun: spot bottlenecks50 xpUsing %lprun: fix the bottleneck50 xpCode profiling for memory usage50 xpPop quiz: steps for using %mprun50 xpUsing %mprun: Hero BMI50 xpUsing %mprun: Hero BMI 2.050 xpBringing it all together: Star Wars profiling100 xp - 3
Gaining efficiencies
This chapter covers more complex efficiency tips and tricks. You'll learn a few useful built-in modules for writing efficient code and practice using set theory. You'll then learn about looping patterns in Python and how to make them more efficient.
Efficiently combining, counting, and iterating50 xpCombining Pokémon names and types100 xpCounting Pokémon from a sample100 xpCombinations of Pokémon100 xpSet theory50 xpComparing Pokédexes100 xpSearching for Pokémon100 xpGathering unique Pokémon100 xpEliminating loops50 xpGathering Pokémon without a loop100 xpPokémon totals and averages without a loop100 xpWriting better loops50 xpOne-time calculation loop100 xpHolistic conversion loop100 xpBringing it all together: Pokémon z-scores100 xp - 4
Basic pandas optimizations
This chapter offers a brief introduction on how to efficiently work with pandas DataFrames. You'll learn the various options you have for iterating over a DataFrame. Then, you'll learn how to efficiently apply functions to data stored in a DataFrame.
Intro to pandas DataFrame iteration50 xpIterating with .iterrows()100 xpRun differentials with .iterrows()100 xpAnother iterator method: .itertuples()50 xpIterating with .itertuples()100 xpRun differentials with .itertuples()100 xppandas alternative to looping50 xpAnalyzing baseball stats with .apply()100 xpSettle a debate with .apply()100 xpOptimal pandas iterating50 xpReplacing .iloc with underlying arrays100 xpBringing it all together: Predict win percentage100 xpCongratulations!50 xp
Datasets
Baseball statisticsCollaborators


Logan Thomas
See MoreScientific Software Technical Trainer, Enthought
Logan is a member of the Technical Training team at Enthought – an organization that provides digital transformation, software consulting, and training services to individuals and companies worldwide. As a Scientific Software Technical Trainer, he focuses on helping students write robust, scalable, and efficient Python code. As a self-proclaimed Python enthusiast, Logan enjoys attending meetups and conferences in the Austin, Texas area to share his knowledge with others.
Don’t just take our word for it
*4.7from 34 reviews
85%
6%
6%
3%
0%
Sort by
- Oleksandr S.about 15 hours
One of the most useful and nicely organized courses at Datacamp. Tons of useful information.
- Indrajeet P.about 20 hours
A lot of helpful tips!
- Austin M.about 1 month
It was fun and educational. Didn’t encounter any curriculum errors until towards the end.
- Lay P.about 1 month
clear and concise
- Nady I.about 2 months
As for me, it is every clear explanation, I’ve received useful information. Thank you
Loading ...
"One of the most useful and nicely organized courses at Datacamp. Tons of useful information."
Oleksandr S.
"A lot of helpful tips!"
Indrajeet P.
"It was fun and educational. Didn’t encounter any curriculum errors until towards the end."
Austin M.
FAQs
Join over 11 million learners and start Writing Efficient Python Code today!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.