Maruthaveeran
asked

Expert
Sudip Bhandari answered
Great question! In Python, def is a keyword used to define a function.
A function is a block of reusable code that performs a specific task—you define it once and can use it as many times as you like.
Example:
def greet(name):
print(f"Hello, {name}!")In the code:
deftells Python that you're defining a function.greetis the name of the function.(name)is a parameter the function takes.The indented line below it is the function body, which runs when the function is called.
To actually use the function, you'd call it like this:
greet("Alice") # Output: Hello, Alice!Why use functions?
They help you avoid repeating code.
They make your programs easier to read and maintain.
You can break a complex task into smaller parts.
Python
This question was asked as part of the Learn Python Basics course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
