Python Practice › Function Programs › Program 18
Function with *args
Write a Python function that accepts any number of numbers with *args and prints their cubes.
Input
5, 6, 8, 7
Output
tuple (5, 6, 8, 7) 125 216 512 343
Approach
*argscollects the values into a tuple.
Solution
def cubes(*args): print(type(args).__name__, args) print(*(n ** 3 for n in args)) cubes(5, 6, 8, 7)Output
tuple (5, 6, 8, 7) 125 216 512 343
Learn the concept
New to this topic? Read the lesson, then come back.