About 11,400 results
Open links in new tab
  1. 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!

    Output:

    Hello, Alice!
    Hello, Bob!
    Copied!

    Here, 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 # Optional
    Copied!
    • function_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 + b

    result = add(5, 3)
    print(result) # 8
    Copied!

    This returns the sum of two numbers instead of just printing it.

    Default Arguments

    Feedback
  2. 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 function
    def my_function():
      print("Hello from a function")
    my_function()
  3. People also ask
    Loading
    Unable to load answer
  4. 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…

    Let's create our first function. Here are the different parts of the program: Here, we have created a simple function named greet() that prints Hello World!
    See more on programiz.com
  5. 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, …

  6. 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 …

  7. 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 …

  8. 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 …

  9. 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 …

  10. 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 …

  11. 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 …