Functions

Python, like many programming languages, has functions. A function is a block of code you can call to run that code.

Python's functions have a lot of "wait I didn't know that" features. Functions can define default argument values, functions can be called with keyword arguments, and functions can be written to accept any number of arguments.

18 articles · 1 hr 12 min read
16 screencasts · 55 min watch
How to call a function
2 min read Screencast available

To use a function in Python, write the function name followed by parentheses. If the function accepts arguments, pass the arguments inside the parentheses.

Read
Functions and Methods
4 min read Screencast available

Methods are functions that live on objects.

Read
Positional vs keyword arguments
3 min read Screencast available

When you're working with named arguments (a.k.a. keyword arguments) it's the argument name that matters. When you're working with positional arguments, it's the position matters (but not the name).

Read
How to make a function
4 min read Screencast available

To define a function in Python, use the def keyword with the function name and any accepted arguments (in parentheses). Functions have inputs (arguments) and an optional output (the return value).

Read
The difference between return and print
4 min read Screencast available

Both return and print "output" something, but one shows output to an end user and the other doesn't.

Read
Python's lambda functions
5 min read

What are lambda expressions and how are they used in Python?

Read
Accepting any number of arguments to a function
3 min read Screencast available

To make a function that accepts any number of arguments, you can use the * operator and then some variable name when defining your function. Some of Python's built-in functions work this way.

Read
Keyword-only function arguments
4 min read Screencast available

If you see a function that has an asterisk (*) on its own with a comma after it, every argument after that point is a keyword-only argument (an argument which can only be specified by its name).

Read
Accepting arbitrary keyword arguments
3 min read Screencast available

Ever seen **kwargs in a function definition? There's nothing special about the name "kwargs": it's the ** that's special. You can use Python's ** operator to define a function that accepts arbitrary keyword arguments.

Read
Is it a class or a function?
4 min read Screencast available

If a callable feels like a function, we often call it a function... even when it's not!

Read
Callables: Python's "functions" are sometimes classes
10 min read

Sometimes we call classes functions in Python. Why? And what's a "callable"?

Read
What is recursion?
6 min read Screencast available

Recursion is when a function calls itself. Loops are usually preferable to recursion, but recursion is excellent for traversing tree-like structures.

Read
The assignments hiding in your functions
3 min read Screencast available Premium

When defining a function, be careful about mutating the arguments passed into your function. Functions in Python are called by assignment, so function calls have similar gotchas to assignments.

Read
Function overloading
4 min read Screencast available Premium

Python doesn't have function overloading, but it is possible to achieve something similar.

Read
Unpacking iterables into function arguments
2 min read Screencast available Premium

In Python, you can unpack the items in an iterable into separate positional arguments in a function (sometimes called "variadic arguments"). This works even if you don't know how long the iterable is.

Read
Mutable default arguments
5 min read Screencast available

In Python, default argument values are defined only one time (when a function is defined).

Read
Positional-only function arguments
4 min read Screencast available Premium

As of Python 3.8, it's possible to declare positional-only arguments when defining a function. You'll (likely) rarely use this feature, but knowing it is handy for deciphering cryptic documentation.

Read
Storing attributes on functions
3 min read Screencast available Premium

Functions are objects in Python, and they can store attributes. Function attributes are usually used to store metadata related to a function.

Read

Continue exploring

Profile picture of Trey

Learn something new about Python

My name is Trey Hunner. I publish new Python articles and screencasts through Python Morsels. If you want to keep learning new things about Python, join Python Morsels!

Join Python Morsels ✨