What do parameters in my specific function mean?

Im currently solving an algorithmic task with Python and I got this function, which I should finish:

def solution(nums: Union[List[int], Tuple[int, ...]]) -> int:

I don’t understand what does those thing mean in function. What is nums:? What does nums: Union[List[int], Tuple[int, ...]] mean at all. Could you please describe it? thx in advance

>Solution :

nums is the name of the parameter (this is the name that the argument value will be bound to inside your function), and Union[List[int], Tuple[int, ...]] is a type annotation saying that nums is expected to be either a list or a tuple of ints. (A better/simpler way to write this would probably be Sequence[int], although that’s not strictly equivalent, and if the union type was specified as part of the assignment you shouldn’t "correct" your instructor even though in real life you probably wouldn’t specify a type like that.)

The -> int means that the function is expected to return an int.

In other words, the function definition and type annotations are telling you to expect that somebody will call your function with some number of ints, like this:

result = solution([1, 2, 3, 4])

and that you should return an int value, which in the above example would be bound to result.

Leave a Reply