This can be a bit confusing to grasp at first.
Time complexity, broadly speaking, talks about how the execution time of a particular code/function/algorithm grows depending on the size of the data (N).
And this is the important distinction point -
If the order is O(N) that means that the execution time grows linearly as N increases.
But, what if
N is the same value? What if it’s the same value every time the code is executed?
Then the execution time for that code will always be the same. Because if the input to that code,
N is not changing, then the execution time for that code [mostly] won’t change either.
This is what they explain in Step 2 of this Mission as well.
sum_1000() does one thing. Computes the sum of the first 1000 integers.
Every time you call
sum_1000() it will do that exact same thing. So, it’s execution time is not changing with the number of integers here. Because the number of integers is always 1000.
If you modified the function to something like -
total = 0
for i in range(N):
total += i
The time complexity of the above would be O(N), because N could be any value. And with higher/larger N, the time complexity would be higher.
It’s the same for -
list2 = [i for i in range(3)]
3 is not changing. Any time that particular piece of code is run, you will get [almost] the same execution time. If instead of that
3 we had an
N, then we could say that the time complexity is O(N) because
N would be a variable whose value could be different.