What is ‘def’ in Python?
In Python, the keyword “def” is used to define functions. By using the “def” keyword, you can create a function in the program and specify its name, parameters, and body. Functions can be called when needed to perform a specific task or return a specific value. Here is an example of defining a function using “def”:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
The code above defines a function called greet that takes an argument name and prints “Hello, ” followed by the string of the name on the screen. After the function is defined, we can use the statement greet(“Alice”) to call the function and pass the string “Alice” as a parameter. Running this program will output “Hello, Alice!”.