def
KeywordPython 2.0+Beginner
Defines a new function or method
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
def greet(name): return f"Hello, {name}!" message = greet("Python") print(message) def add(a, b): return a + b print(add(3, 4))
def creates a named function. The return statement sends a value back to the caller.
Try in PlaygroundTags
languagesyntaxcorefunctionoop