Skip to main content
HomePython

Writing Functions in Python

4.3+
62 reviews
Intermediate

Learn to use best practices to write maintainable, reusable, complex functions with good documentation.

Start Course for Free
4 hours15 videos46 exercises90,777 learnersTrophyStatement of Accomplishment

Create Your Free Account

GoogleLinkedInFacebook

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.
Group

Training 2 or more people?

Try DataCamp for Business

Loved by learners at thousands of companies


Course Description

You've done your analysis, built your report, and trained a model. What's next? Well, if you want to deploy your model into production, your code will need to be more reliable than exploratory scripts in a Jupyter notebook. Writing Functions in Python will give you a strong foundation in writing complex and beautiful functions so that you can contribute research and engineering skills to your team. You'll learn useful tricks, like how to write context managers and decorators. You'll also learn best practices around how to write maintainable reusable functions with good documentation. They say that people who can do good research and write high-quality code are unicorns. Take this course and discover the magic!
For Business

Training 2 or more people?

Get your team access to the full DataCamp platform, including all the features.
DataCamp for BusinessFor a bespoke solution book a demo.

In the following Tracks

Certification Available

Associate Data Scientist in Python

Go To Track
  1. 1

    Best Practices

    Free

    The goal of this course is to transform you into a Python expert, and so the first chapter starts off with best practices when writing functions. You'll cover docstrings and why they matter and how to know when you need to turn a chunk of code into a function. You will also learn the details of how Python passes arguments to functions, as well as some common gotchas that can cause debugging headaches when calling functions.

    Play Chapter Now
    Docstrings
    50 xp
    Crafting a docstring
    100 xp
    Retrieving docstrings
    100 xp
    Docstrings to the rescue!
    50 xp
    DRY and "Do One Thing"
    50 xp
    Extract a function
    100 xp
    Split up a function
    100 xp
    Pass by assignment
    50 xp
    Mutable or immutable?
    50 xp
    Best practice for default arguments
    100 xp
  2. 2

    Context Managers

    If you've ever seen the "with" keyword in Python and wondered what its deal was, then this is the chapter for you! Context managers are a convenient way to provide connections in Python and guarantee that those connections get cleaned up when you are done using them. This chapter will show you how to use context managers, as well as how to write your own.

    Play Chapter Now
  3. 3

    Decorators

    Decorators are an extremely powerful concept in Python. They allow you to modify the behavior of a function without changing the code of the function itself. This chapter will lay the foundational concepts needed to thoroughly understand decorators (functions as objects, scope, and closures), and give you a good introduction into how decorators are used and defined. This deep dive into Python internals will set you up to be a superstar Pythonista.

    Play Chapter Now
  4. 4

    More on Decorators

    Now that you understand how decorators work under the hood, this chapter gives you a bunch of real-world examples of when and how you would write decorators in your own code. You will also learn advanced decorator concepts like how to preserve the metadata of your decorated functions and how to write decorators that take arguments.

    Play Chapter Now
For Business

Training 2 or more people?

Get your team access to the full DataCamp platform, including all the features.

In the following Tracks

Certification Available

Associate Data Scientist in Python

Go To Track

collaborators

Collaborator's avatar
Hillary Green-Lerman
Collaborator's avatar
Becca Robins

prerequisites

Python Toolbox
Shayne Miel HeadshotShayne Miel

Director of Software Engineering @ American Efficient

See More

Don’t just take our word for it

*4.3
from 62 reviews
69%
8%
18%
2%
3%
Sort by
  • Yuanpei G.
    18 days

    The content around context manager and decorators are confusing to me. I passed the practice without really knowing exactly what the code does..

  • Ileana R.
    2 months

    Amazing course!

  • Thuy N.
    4 months

    Difficult, but very helpful.

  • Sait O.
    6 months

    Provides In depth understanding of some key concepts.

  • Natalie R.
    6 months

    Wonderful course with a wonderful instructor. And there is a little bug. If you choose to re-watch the penultimate video, you will instead get the ultimate video, which is not the expected behavior. This happens for a number of courses, including this one. I observe the behavior when using the mobile app.

"The content around context manager and decorators are confusing to me. I passed the practice without really knowing exactly what the code does.."

Yuanpei G.

"Amazing course!"

Ileana R.

"Difficult, but very helpful."

Thuy N.

Join over 15 million learners and start Writing Functions in Python today!

Create Your Free Account

GoogleLinkedInFacebook

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.