In this tutorial, we will discuss how to use the def keyword in Python. The def keyword is used to define a function in Python. Functions are a way of organizing and reusing code. Using functions can make your code more efficient and easier to maintain. We will learn how to define and call functions and how to work with the functions’ arguments and return values.
Step 1: Define a Function using ‘def’
To define a function in Python, you use the def keyword followed by the function name, a pair of parentheses, and a colon. The code block for the function should be indented, typically by four spaces. Here’s a simple example of defining a function that prints “Hello, World!”:
1 2 |
def hello_world(): print("Hello, World!") |
Step 2: Calling a Function
To call or use a defined function, just type the function name followed by a pair of parentheses. In this example, calling the function will print “Hello, World!” as defined in the function:
1 |
hello_world() |
When executed, the output will be:
Hello, World!
Step 3: Working with Arguments
Functions can receive input values called arguments. You can define the arguments in the function definition within the parentheses. To pass arguments when calling a function, place the values within the parentheses. Here’s an example of a function with an argument:
1 2 3 4 5 |
def greet(name): print(f"Hello, {name}!") greet("Alice") greet("Bob") |
When executed, the output will be:
Hello, Alice! Hello, Bob!
Step 4: Default Argument Values
You can set default values for arguments so that if a value is not provided when the function is called, the default value will be used. Here’s an example demonstrating default argument values:
1 2 3 4 5 |
def greet(name="World"): print(f"Hello, {name}!") greet() # No argument is provided, so the default "World" will be used. greet("Alice") |
When executed, the output will be:
Hello, World! Hello, Alice!
Step 5: Returning Values
Functions can return values using the return keyword. Returned values can be used and manipulated by the calling code. Here’s an example of a function that returns the square of a number:
1 2 3 4 5 |
def square(num): return num * num result = square(4) print(result) # Prints "16" |
When executed, the output will be:
16
Full Code
Here’s the full code combining all examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def hello_world(): print("Hello, World!") hello_world() def greet(name): print(f"Hello, {name}!") greet("Alice") greet("Bob") def greet_with_default(name="World"): print(f"Hello, {name}!") greet_with_default() greet_with_default("Alice") def square(num): return num * num result = square(4) print(result) |
When executed, the output will be:
Hello, World! Hello, Alice! Hello, Bob! Hello, World! Hello, Alice! 16
Conclusion
With this tutorial, you now know how to use the def keyword in Python to define functions, call them, work with arguments and their default values, and return values. Functions are essential in organizing and reusing code, making your programs easier to maintain and more efficient. Keep practicing and have fun experimenting with creating your own functions!