- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
In Python, the def keyword is used to define a reusable block of code called a function. Functions help organize code, avoid repetition, and make programs more modular.
Example: Basic Function Definition and Call
def greet(name):print(f"Hello, {name}!")greet("Alice")greet("Bob")Copied!✕CopyOutput:
Hello, Alice!Hello, Bob!Copied!✕CopyHere, def greet(name): defines a function named greet with one parameter name. The function is executed only when called.
Basic Syntax
def function_name(parameters):"""Optional docstring describing the function."""# Function body (indented)return value # OptionalCopied!✕Copyfunction_name: Must follow Python naming rules.
parameters: Optional; can be empty or multiple.
return: Sends a value back to the caller; defaults to None if omitted.
Example with Return Value
def add(a, b):return a + bresult = add(5, 3)print(result) # 8Copied!✕CopyThis returns the sum of two numbers instead of just printing it.
Default Arguments
Python def Keyword - GeeksforGeeks
Jan 13, 2026 · The def keyword in Python is used to define a function. Functions are logical blocks of code that can be reused multiple times. For example:
See results only from geeksforgeeks.orgSign In
The def keyword in Python is used to define a function. Functions are logical blocks of code that can be reused multiple times. For example:
Python def Keyword - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Code sample
# Create and execute a functiondef my_function():print("Hello from a function")my_function()- People also ask
Python Functions (With Examples) - Programiz
Learn how to create and use functions in Python, including user-defined and standard library functions. Functions are blocks of code that perform a specific task and can tak…
How To Define A Function In Python?
Feb 10, 2025 · Learn how to use the def keyword to create a function in Python, and how to pass parameters, return values, and call functions. See examples of calculating the area of a rectangle, …
What Does ‘def’ Mean in Python?: Definition with Examples
Apr 17, 2025 · Using def function in Python improves code organization, making Python programs more structured and easier to debug and scale. Functions help …
def | Python Keywords – Real Python
In Python, the def keyword defines a function, which is a reusable block of code that can accept data input, perform a specific computation or task, and return a result …
What is Def in Python? Defining Functions Explained
This involves the use of the def keyword, a critical component in Python programming. Understanding what is def in Python and how to utilize it …
Understanding `def` in Python: Functions Unveiled - CodeRivers
Feb 7, 2025 · In Python, the def keyword is used to define a function. A function is a block of organized, reusable code that performs a specific task. When you use def, you are essentially creating a new …
Python Function: The Basics Of Code Reuse
Oct 19, 2025 · Learn how to define, call, and use functions in Python with the def keyword. Functions are named sections of code that perform a specific task and …
Python def Keyword - Online Tutorials Library
When we define a function using def keyword inside a class it is known as method. To execute the function, which was created using def keyword we just need to call using function-name. Following is …