Sometimes we don't know how many arguments a function needs to accept, but, Python has a way to handle that
We are allowed to collect an arbitrary number of arguments from the calling statement
NOTE:
You will now here the term TUPLE
We'll talk about it later, but it's essentially a list which you can't modify any of the items
It has its own methods (only 2 of them), but for now, know we can access all the items just like a list with an index
thing[index]
Arbitrary arguments allow you to pass a variable number of arguments to a function.
This is done by using the * operator before a parameter name in the function definition.
def greet_characters(*thecharacters):
When a function parameter is prefixed with an asterisk (*), Python collects all positional arguments passed to the function into a list-like object called a tuple.
You can then iterate over or access these arguments as needed.
Type this program in and let's see what we get...
It’s you passing multiple separate values, and Python bundles (packs) them into a tuple automatically.
If you pass "A", "B", "C" → Python packs them into ("A", "B", "C")
If you pass ["A", "B", "C"] → that is one argument (a list)
✅ Python packs positional arguments into a tuple when you use * in the parameter.
✅ Version 1: Using *args (multiple separate values)
✅ Version 2: Using a list (one argument that contains many)
greet_with_args("Mario", "Luigi", "Peach")
👉 3 separate arguments
greet_with_list(["Mario", "Luigi", "Peach"])
👉 1 argument (a list)
You want to let the caller type values naturally:
add(4, 9, 12)
The number of values isn’t known in advance
You just want to read/loop/count them
You already have the values collected in a list
You might edit/remove/sort them before using them
You’re passing big data around
Let's Try a Few Small Examples Together that will help us with:
1️⃣ Counting arbitrary arguments
2️⃣ Special-case detection
3️⃣ Decision-making based on how many arguments were passed
The Minions are reporting for duty, but we don’t know how many will show up. Our job is to handle any number of Minions and still keep track of them.
You will need to:
Accept a variable number of arguments in a function
Loop through all values that were passed in
Count how many items were received using len()
You can see the sample output screen here...
minion_roll_call("Bob", "Kevin", "Stuart")
Toys are coming out of the toy box, and some characters trigger special reactions. Your function must respond differently depending on who appears.
You will need to:
Accept a variable number of Toy Story character names as arguments
Loop through each character name one at a time
Check if the name is Buzz and print a Buzz-specific message
Check if the name is Woody and print a Woody-specific message
Print a generic message for any other character (such as Rex, Slinky, or Hamm)
Ensure every character passed into the function produces exactly one line of output
toy_story_check("Woody", "Buzz", "Rex")
The Avengers’ response depends on how many heroes arrive. Your function must make decisions based on the total number of arguments passed in, not which ones.
You will need to:
Accept a variable number of Avenger names as arguments
Count the total number of heroes passed into the function
Assign a threat/status level based on that count:
1 hero → Low threat / minimal response
2–3 heroes → Moderate threat / team response
4 or more heroes → High threat / full Avengers response
Print the threat/status message once, before listing hero names
Loop through all hero names and display them after the status message
avengers_threat_level("Iron Man")
avengers_threat_level("Iron Man", "Thor")
avengers_threat_level("Iron Man", "Thor", "Hulk", "Black Widow")
You must create a program with a function which takes in a random amount of dinosaur types
This function must print out a table of the dinosaur types, BUT
Print Specific Messages for certain types of dinosaurs
You will need to call your function with:
1 Dino
3 Dinos
5 Dinos
You will need for loops!!!
Bonus
Write a function which prompts the user to enter a list of dinosaurs
Use that list to select 1, 3, and 5 dinosaurs to pass to the function
You'll need to get creative for this one.
Sometimes a function needs one piece of information that is always required, and other information that can vary. That’s where mixing positional arguments and *args comes in.
Positional arguments are required and must always be provided.
Arbitrary arguments (*args) allow you to pass in any number of extra values.
Python reads arguments from left to right:
Required positional arguments come first
Arbitrary arguments come after and are grouped together
What we need to do...
Hulk always needs to know where he is smashing — that’s required.
But he might smash one thing… or many things — that part is flexible.
We need to write a function called hulk_smash which accepts both positional and arbitrary arguments!
Remember...
The function always expects a location first
Any additional values are treated as things Hulk smashes
Python automatically groups the extra values into a tuple
The function doesn’t care how many smash targets there are — it handles them all the same way
Hints:
Get the items and put into a list
send the list into the function with the *mylist
I know... I know... I literally just told you to create a list and then not pass the list. Why would I do that??
“If we’re using a list anyway… why not just pass a list?”
Short answer:
👉 Because *args and lists solve different problems.
🧠 What Problem Is *args Solving?
*args is not about storing data.
It’s about how a function is CALLED.
With a list:
You must already have everything collected and packaged
With *args:
You can pass values naturally, without preparing a list first.
Now the function can be called:
With user input
With hardcoded values
With unpacked lists
From other functions
Same function. More flexibility.
We collect values (often using a loop)
We store them in a list
We pass that list into the function using *
Python unpacks the list into separate arguments
The function packs them back into *args (a tuple)
That sounds weird — but it’s intentional and powerful.
✅ Use *args when:
You don’t know how many values will be passed in.
You only need to read the values (not modify them).
You want to quickly pass values without creating a list first.
✅ Use a List when:
You already have the values stored in a list.
You might need to modify the list inside the function.
You want to make it easier to pass in a large dataset.
Let's say you want to order a pizza.
You definitely have to tell them a size (sm, med, lg) - REQUIRED
You don't know how many toppings you want - ARBITRARY
Welcome to the Krusty Krab’s brand-new Pizza Delivery Service—proudly brought to you by SpongeBob SquarePants! SpongeBob is busy flipping Krabby Patties, and Squidward is too busy... not caring. So, it’s up to you to write a program that helps the Krusty Krab process pizza orders for the residents of Bikini Bottom. 🧽🍕
Your program will let customers:
Choose a pizza size (small, medium, or large) <-- REQUIRED
Add as many toppings as they’d like (think seaweed, kelp flakes, or extra cheesy coral!).
See a neat summary of their order before it's delivered.
But wait, there’s more!
For advanced programmers, Mr. Krabs has some extra challenges:
Add the option to specify the type of crust (thin, thick, or stuffed coral crust).
Calculate the total price of the pizza based on its size and number of toppings.
SpongeBob also loves a good receipt format, so make it look professional!
Sample Function Call: order_pizza("small", "seaweed")
Sample Function Call:
order_pizza("large", "extra cheese", "kelp flakes", "coral bites", crust="stuffed")
BONUS TASK SAMPLE:
order_pizza("large", "extra cheese", "coral bites", "jellyfish jelly", crust="thin")