First Python Program (Hello World) - Time & Space Complexity
Let's see how the time it takes to run a simple program changes as we run it.
We want to know how the program's steps grow when we run it.
Analyze the time complexity of the following code snippet.
print("Hello, World!")
This code prints a greeting message once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single print statement.
- How many times: Exactly once.
Since the program only prints once, the steps do not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps stays the same no matter the input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter what.
[X] Wrong: "Printing a message depends on input size, so it takes longer with bigger input."
[OK] Correct: The print runs once and does not change with input size, so time stays the same.
Understanding simple programs helps build a strong base for bigger problems later.
"What if we printed the message inside a loop that runs n times? How would the time complexity change?"