Understanding 'def' in Python: A Comprehensive Guide

Benjamin Jul 11, 2026

In the realm of Python programming, defining a function is a fundamental aspect that enables code reusability and modularity. But what keyword do we use to define a function in Python? The answer is 'def'.

a table with words that are used to describe the keywords for each keyword
a table with words that are used to describe the keywords for each keyword

Let's delve into this topic, exploring the 'def' keyword, its syntax, and its role in creating functions in Python.

Minimalist Tech Workspace Ideas
Minimalist Tech Workspace Ideas

The 'def' Keyword

The 'def' keyword in Python is used to define a function. It's a reserved keyword, meaning it has a specific purpose in the language and cannot be used for other purposes.

Functions in Python | Types, Example
Functions in Python | Types, Example

When we use 'def', we're essentially telling Python to create a new function with a specific name and behavior.

Syntax of the 'def' Keyword

Functions
Functions

The basic syntax of the 'def' keyword is as follows:

def function_name(parameters):

Here, 'function_name' is the name you give to your function, and 'parameters' are the inputs that the function expects to receive.

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

For example, consider the following simple function that greets the user:

def greet(name):

print(f"Hello, {name}!")

IMPORTANT FUNCTIONS IN PYTHON
IMPORTANT FUNCTIONS IN PYTHON

Using the 'def' Keyword

To use a function defined with 'def', we simply call it by its name, followed by the arguments in parentheses:

Keywords in Python
Keywords in Python
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
two black and gold posters with different types of wires on them, one in the middle
two black and gold posters with different types of wires on them, one in the middle
the cover of collection data types in python, with an image of numbers and symbols
the cover of collection data types in python, with an image of numbers and symbols
User-Defined Functions in Python  Create Your Own Functions Easily📈🤩
User-Defined Functions in Python Create Your Own Functions Easily📈🤩
a table that has different types of words and phrases on it, all in purple
a table that has different types of words and phrases on it, all in purple
Master Python Functions – Write Smarter Code! 🐍✨
Master Python Functions – Write Smarter Code! 🐍✨
an important python functions chart with the words and numbers on it, including instructions to use them
an important python functions chart with the words and numbers on it, including instructions to use them
Learn Python Basics Quickly, Python Coding Essentials 2025, Python Functions Cheat Sheet, Python Functions Reference Guide, Python Programming Tips For Beginners, Python Algorithms, Python Code For Timelines, Python Programming Definition Infographic
Learn Python Basics Quickly, Python Coding Essentials 2025, Python Functions Cheat Sheet, Python Functions Reference Guide, Python Programming Tips For Beginners, Python Algorithms, Python Code For Timelines, Python Programming Definition Infographic
required functions
required functions
20 Python Functions You Should Know
20 Python Functions You Should Know
a poster with different types of text on it
a poster with different types of text on it
Python Cheat Sheet
Python Cheat Sheet
Imp💯 Python functions
Imp💯 Python functions
Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner
Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]
a screenshot of a computer screen with the text looping through dictionaries in python
a screenshot of a computer screen with the text looping through dictionaries in python
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Basic Functions in Python
Basic Functions in Python
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

greet("Alice")

This will output: Hello, Alice!

Functions in Python

Now that we know how to define a function using 'def', let's explore some aspects of functions in Python.

Functions can perform tasks, calculate values, or make decisions based on the inputs they receive. They can also be nested inside other functions, creating complex, modular code.

Returning Values from Functions

Functions can return a value using the 'return' statement. This value can then be used in expressions or assigned to variables:

def add_numbers(a, b):

return a + b

result = add_numbers(3, 5)

print(result) # Outputs: 8

Default Arguments and Keyword Arguments

Python allows us to define functions with default arguments and keyword arguments, providing flexibility in how we call our functions:

def greet(name="World"):

print(f"Hello, {name}!")

Calling greet("Alice") will output Hello, Alice!, while calling greet() will output Hello, World!.

In conclusion, the 'def' keyword is a powerful tool in Python, enabling us to create reusable, modular code. Understanding how to use 'def' is a key step in becoming proficient in Python programming. So, start exploring, defining, and using functions in your Python projects today!