What keyword is used to define a function in Python? The keyword is def. It introduces a function definition, followed by the function’s name, parentheses containing parameters, and a colon before the indented function body.
For example, def greet(name): creates a function named greet with one parameter. The indented statements beneath the header form the function body, and return can send a value back to the code that called it. Python uses indentation to delimit this block rather than curly braces, so indentation is part of the language’s syntax.
The keyword lambda also creates a function, but it is used for a restricted anonymous function expression containing a single expression. It is not the standard keyword used for a named, multi-line function definition. “function” and “func” are not Python keywords for this purpose, although other programming languages use similar terms.
A function may be defined at the top level, inside another function, or as a method within a class. Python’s def statement can also be preceded by decorators that modify or extend the function’s behavior.