Writing multiple lines in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When writing multiple lines in a program, it is important to see how the time to run the code changes as we add more lines.
We want to know how the program's work grows when we write many lines one after another.
Analyze the time complexity of the following code snippet.
for i in range(n):
print(i)
for j in range(n):
print(j * 2)
This code prints numbers from 0 to n-1, then prints double those numbers in a second loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops that each run through n items.
- How many times: Each loop runs n times, one after the other.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 print actions |
| 100 | About 200 print actions |
| 1000 | About 2000 print actions |
Pattern observation: The total work grows roughly twice as fast as n because there are two loops each doing n steps.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input size grows, even with two loops one after another.
[X] Wrong: "Two loops mean the time complexity is squared, like O(n²)."
[OK] Correct: The loops run one after the other, not inside each other, so the total time just adds up, not multiplies.
Understanding how multiple steps add up helps you explain your code clearly and shows you know how programs grow with input size.
"What if the two loops were nested inside each other? How would the time complexity change?"
Practice
Solution
Step 1: Understand multi-line string syntax
Python allows strings to span multiple lines using triple quotes (either ''' or """).Step 2: Check each option
Use triple quotes like'''This is a\nmulti-line string'''correctly uses triple quotes to write a multi-line string. Other options misuse quotes or syntax.Final Answer:
Use triple quotes like '''This is a\nmulti-line string''' -> Option CQuick Check:
Triple quotes = multi-line string [OK]
- Using single or double quotes without triple quotes
- Trying to separate lines with commas inside quotes
- Forgetting to close triple quotes
Solution
Step 1: Understand how multi-line strings handle newlines
Triple-quoted strings keep line breaks as typed, so actual newlines appear without \n escape sequences.Step 2: Analyze each option
print('''Line 1 Line 2 Line 3''') uses triple quotes with actual newlines inside, so it prints three lines. print('''Line 1\nLine 2\nLine 3''') uses \n inside triple quotes, which prints literal \n, not new lines. Options B and D use single quotes with \n, which print new lines correctly, but are not multi-line strings.Final Answer:
print('''Line 1 Line 2 Line 3''') -> Option DQuick Check:
Triple quotes with real newlines print multiple lines [OK]
- Using \n inside triple quotes expecting line breaks
- Confusing escape sequences with actual newlines
- Using single quotes for multi-line strings
text = '''Hello World Python''' print(text)
Solution
Step 1: Understand triple-quoted string behavior
The triple quotes preserve the line breaks inside the string as actual newlines.Step 2: Predict print output
Printing the string will show three lines: Hello, World, and Python each on its own line.Final Answer:
Hello World Python -> Option AQuick Check:
Triple quotes print multi-line text as typed [OK]
- Expecting \n to print literally
- Confusing string representation with print output
- Syntax errors from missing quotes
text = '''Line 1 Line 2 Line 3' print(text)
Solution
Step 1: Check string delimiters
The string starts with triple single quotes ''' but ends with a single quote ', causing a syntax error.Step 2: Identify error type
Python expects matching triple quotes to close the string. Mismatched quotes cause SyntaxError.Final Answer:
Missing closing triple quotes causes SyntaxError -> Option AQuick Check:
Triple quotes must open and close properly [OK]
- Ending triple-quoted string with single quote
- Forgetting to close triple quotes
- Assuming print syntax error instead
Solution
Step 1: Understand requirement for preserving formatting
To keep multiple paragraphs and blank lines exactly as typed, the string must preserve line breaks and spaces.Step 2: Choose best method
Triple quotes allow writing multi-line strings naturally, including blank lines, without needing \n or concatenation.Final Answer:
Use triple quotes to write the message with blank lines inside -> Option BQuick Check:
Triple quotes preserve formatting best for long multi-line text [OK]
- Using concatenation makes code messy and error-prone
- Forgetting to add \n for new lines in single quotes
- Using multiple print statements loses single string storage
