Yes, absolutely! Functions can have as many arguments as you need. You simply separate them with commas inside the parentheses.
Example of Multiple Arguments
Let's say you want to greet someone and also tell them what day it is. You would define the function like this:
# Define a function with two arguments: name and day
def greet_user(name, day):
print(f"Hello {name}, happy {day}!")
# Call the function with two values
greet_user("Carlos", "Monday")
greet_user("Wanda", "Friday")
Key things to remember:
- Order Matters: By default, Python matches the values you provide to the arguments in the order they appear. In the example above,
"Carlos"goes tonameand"Monday"goes today. - Matching Count: If you define a function with two arguments, you must provide exactly two values when you call it (otherwise, Python will show an error).
Try it yourself!
You can modify your arguments.py file to try this out:
- Change the function definition to:
def say_hello(name, city): - Change the print statement to:
print(f'Hello {name} from {city}') - Call it with two strings:
say_hello('Carlos', 'New York')
Would you like to see how to handle a situation where you don't know how many arguments you'll need?