Python Tip of the Day: Flexible Functions with args and kwargs
Flexible Functions with args
and kwargs
Author: Jeremy Morgan
Published: November 7, 2024
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.
Allow your functions to accept any number of arguments. Flexibility at its best!
# Function with *args and kwargs
def greet(*names, kwargs):
for name in names:
print(f"Hello, {name}!")
for key, value in kwargs.items():
print(f"{key} = {value}")
greet('Alice', 'Bob', age=30, location='USA')
# Output:
# Hello, Alice!
# Hello, Bob!
# age = 30
# location = USA
The Python Tip of the Day is a daily series published in the month of November. The tips are designed to help you become a better Python programmer. I post tips like this and more every single day on X. Let’s connect!
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.