Step-by-Step Explanation
1. Importing List from typing
from typing import List
The typing module provides type hints to improve code readability and maintainability.
List[int] indicates that nums should be a list of integers.
2. Defining the Function
def add_numbers(nums: List[int]) -> int:
Function name: add_numbers
Parameter:
nums: List[int] → A list containing integers.
Return type: int → The function is expected to return an integer.
3. Summing the Numbers
return sum(nums)
sum(nums) calculates the sum of all integers in the list.
4. Calling the Function
print(add_numbers([1, 2, 3]))
[1, 2, 3] is passed to add_numbers.
sum([1, 2, 3]) evaluates to 1 + 2 + 3 = 6.
print(6) outputs:
6
Final Output
6
0 Comments:
Post a Comment