First Python Program (Hello World) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
print() function do in Python?Solution
Step 1: Understand print() purpose and match to options
Theprint()function is used to show messages or values on the screen. Displays text or values on the screen correctly describes this.Final Answer:
Displays text or values on the screen -> Option BQuick Check:
print() shows output = Displays text or values on the screen [OK]
- Thinking print() saves data
- Confusing print() with input()
- Assuming print() creates variables
Solution
Step 1: Identify correct print syntax in Python
Python requires text inside quotes using the print() function. print("Hello, World!") matches this correct syntax.Final Answer:
print("Hello, World!") -> Option DQuick Check:
Quotes + print() = Correct syntax [OK]
- Missing quotes around text
- Using echo or printf (not Python)
- Forgetting parentheses
print("Hello, World!")Solution
Step 1: Analyze print() output
The print() function shows text inside quotes without the quotes. The code outputs Hello, World! with no quotes or errors.Final Answer:
Hello, World! -> Option CQuick Check:
print("text") outputs text without quotes [OK]
- Expecting quotes in output
- Thinking code prints code itself
- Confusing output with error
print(Hello, World!)
Solution
Step 1: Identify the syntax error in print()
Text must be inside quotes as a string. Hello, World! lacks quotes, causing Missing quotes around text.Final Answer:
Missing quotes around text -> Option AQuick Check:
Text needs quotes in print() [OK]
- Forgetting quotes
- Removing parentheses
- Using wrong function name
Hello, World!and
Welcome to Python.Which code will do this correctly?
Solution
Step 1: Determine correct code for multiple lines
\n inside a string creates a newline. print("Hello, World!\nWelcome to Python.") uses \n correctly for two lines.Final Answer:
print("Hello, World!\nWelcome to Python.") -> Option AQuick Check:
Use \n inside string for new line [OK]
- Using multiple print without separation
- Wrong escape character
- Concatenating without space or newline
