python / beginner
Snippet
Default Arguments
You can assign default values to parameters in a function definition, making them optional when calling the function.
snippet.py
1
2
3
4
5
def greet(name="Guest"):print("Welcome, " + name)greet("Alice")greet()
Breakdown
1
def greet(name="Guest"):
Defines a function where 'name' defaults to 'Guest' if not provided.
2
greet("Alice")
Calls the function with 'Alice', overriding the default.
3
greet()
Calls the function without arguments, using the default 'Guest'.