Learning machine learning? Try my machine learning flashcards or Machine Learning with Python Cookbook.
Using Iterable As Function Arguments
Create Arguments
# Create a list with three values
scores = [1,2,3]
# Create a dictionary with a single key-value pair
scores_with_names = {'amy_score': "200"}
Create Function With Positional Arguments
# Create a function with three positional arguments
def show_team_scores(jack_score, amy_score, sarah_score):
# Print each of the positional arguments
print(jack_score, amy_score, sarah_score)
# Run the function with * and an iterable object (in this case a list)
show_team_scores(*scores)
1 2 3
Create Function Using Keyword Arguments
# Create a function with three keyword arguments with default values
def show_team_scores(jack_score=0, amy_score=0, sarah_score=0):
# Print each of the keyword arguments
print(jack_score, amy_score, sarah_score)
# Run the function with ** and a dictionary
show_team_scores(**scores_with_names)
0 200 0
Notice how jack_score
and sarah_score
used the default values, while amy_score
used the argument value we defined in scores_with_names
.