Default Arguments in Python

Level 3113 keystrokes

This function has a default argument. If greeting is not provided, it defaults to 'Hello'.

Python code

# Define a function with a default argument
# greeting="Hello" means Hello is used if not specified
1def greet(name, greeting="Hello"):
# Return formatted greeting string
2 return f"{greeting}, {name}!"
3
# Call with only name - uses default greeting
4print(greet("Alice"))
# Call with both arguments - overrides default
5print(greet("Bob", "Hi"))

Output

Hello, Alice!
Hi, Bob!
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 10
  • (× 5
  • )× 5
  • ,× 3
  • {× 2
  • }× 2
  • :× 1
  • !× 1
  • =× 1

More Python examples at level 3

← Python typing practice