What Does -> Mean in Python Function Definitions?

What Does -> Mean in Python Function Definitions?

by Dorothy Phoenix 0 Comments basics python

In Python, the arrow symbol (->) appears in function definitions as a notation to indicate the expected return type. This notation is optional, but when you include it, you clarify what data type a function should return:

Python
>>> def get_number_of_titles(titles: list[str]) -> int:
...     return len(titles)
...

You may have observed that not all Python code includes this particular syntax. What does the arrow notation mean? In this tutorial, you’ll learn what it is, how to use it, and why its usage sometimes seems so inconsistent.

Take the Quiz: Test your knowledge with our interactive “What Does -> Mean in Python Function Definitions?” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

What Does -> Mean in Python Function Definitions?

Test your understanding of Python return type hints and learn how to use the -> arrow, annotate containers, and check code with static tools.

In Short: The -> Notation Indicates a Function’s Return Type in Python

In Python, every value stored in a variable has a type. Because Python is dynamically typed, variables themselves don’t have fixed types—they can hold values of any type at different times. This means the same variable might store an integer at one moment and a string the next. In contrast, statically typed languages like C++ or Java require explicit type declarations, and variables are bound to a specific type throughout their lifetime.

You can see an example of a dynamically typed Python variable in the following code example:

Python
>>> my_number = 32
>>> my_number = "32"

You start by declaring a variable called my_number, and setting it to the integer value 32. You then change the variable’s value to the string value "32". When you run this code in a Python environment, you don’t encounter any problems with the value change.

Dynamic typing also means you might not always know what data type a Python function will return, if it returns anything at all. Still, it’s often useful to know the return type. To address this, Python 3.5 introduced optional type hints, which allow developers to specify return types. To add a type hint, you place a -> after a function’s parameter list and write the expected return type before the colon.

You can also add type hints to function parameters. To do this, place a colon after the parameter’s name, followed by the expected type—for example, int or str.

Basic Type Hint Examples

To further explore type hints, suppose that you’re creating a Python application to manage inventory for a video game store. The program stores a list of game titles, tracks how many copies are in stock, and suggests new games for customers to try.

You’ve already seen the type hint syntax in the code example introduced earlier, which returns the number of titles in a list of games that the game store carries:

Python
>>> def get_number_of_titles(titles: list[str]) -> int:
...     return len(titles)
...
>>> games = ["Dragon Quest", "Final Fantasy", "Age of Empires"]
>>> print("Number of titles:", get_number_of_titles(games))
Number of titles: 3

Here you see a type hint. You define a function called get_number_of_titles(), which takes a list of game titles as input. Next, you add a type hint for the titles parameter, indicating that the function takes a list of strings. Finally, you also add another type hint for the return type, specifying that the function returns an int value.

The function returns the length of the list, which is an integer. You test this out in the next line, where you create a variable that stores a list of three game titles. When you invoke the function on the list, you verify that the output is 3.

Note that in a real-world application, creating a separate function just to return a list’s length might be redundant. However, for instructional purposes, the function shown in the example is a straightforward way to demonstrate the type hint concept.

You can use type hints with any Python type. You’ve already seen an example with an int return type, but consider another example with a str return type. Suppose you want to recommend a random game for a customer to try. You could do so with the following short example function:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Dorothy Phoenix

Dorothy is an independent video game developer, software engineer, technical writer, and tutor.

» More about Dorothy

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: basics python