Writing Efficient Python Code
Learn to write efficient code that executes quickly and allocates resources skillfully to avoid unnecessary overhead.
Start Course for Free4 hours15 videos52 exercises
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.Training 2 or more people?Try DataCamp For Business
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!
For Business
Training 2 or more people?
Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and moreIn the following Tracks
Python Programming
Go To Track- 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 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
For Business
Training 2 or more people?
Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and moreIn the following Tracks
Python Programming
Go To TrackLogan Thomas
See MoreScientific Software Technical Trainer, Enthought
FAQs
Join over 14 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.