0
0
Pythonprogramming~5 mins

Positional arguments in Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Positional arguments
O(1)
Understanding Time Complexity

Let's see how the time it takes to run a function changes when we use positional arguments.

We want to know how the number of steps grows as we give more inputs.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

def sum_numbers(a, b, c):
    return a + b + c

result = sum_numbers(1, 2, 3)
print(result)

This code adds three numbers given as positional arguments and prints the result.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Adding three numbers once.
  • How many times: Exactly one time, no loops or repeats.
How Execution Grows With Input

Since the function always adds exactly three numbers, the work stays the same no matter what.

Input Size (n)Approx. Operations
32 additions
10Still 2 additions
100Still 2 additions

Pattern observation: The number of operations does not grow with input size here.

Final Time Complexity

Time Complexity: O(1)

This means the time to run the function stays the same no matter how many inputs you give, because it only handles a fixed number.

Common Mistake

[X] Wrong: "Adding more positional arguments makes the function slower in a big way."

[OK] Correct: The function only does a fixed number of steps, so adding more arguments to the call doesn't change how many steps the function runs.

Interview Connect

Understanding how fixed inputs affect time helps you explain simple function behavior clearly and confidently.

Self-Check

"What if the function used a loop to add a list of numbers passed as positional arguments? How would the time complexity change?"