print() function basics in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time to run a program changes when we use the print() function.
We want to know how the number of print statements affects the time it takes to run.
Analyze the time complexity of the following code snippet.
for i in range(n):
print(i)
This code prints numbers from 0 up to n-1, one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print() function inside the loop.
- How many times: It runs once for each number from 0 to n-1, so n times.
As n gets bigger, the number of print calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The number of print calls grows directly with n.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of print statements increases.
[X] Wrong: "print() runs instantly and does not affect time as n grows."
[OK] Correct: Each print takes some time, so more prints mean more total time.
Understanding how simple commands like print() add up helps you think clearly about program speed in real tasks.
"What if we printed only every other number instead of every number? How would the time complexity change?"
Practice
print() function do in Python?Solution
Step 1: Understand the purpose of
Theprint()print()function is used to show messages or values on the screen, making it easy to see output from the program.Step 2: Compare with other options
Saving data, reading input, or creating variables are done by other functions, notprint().Final Answer:
It displays messages or values on the screen. -> Option AQuick Check:
print()shows output [OK]
- Confusing print() with input()
- Thinking print() saves data
- Assuming print() creates variables
Solution
Step 1: Understand how print separates items
Using commas inprint()separates items with spaces by default, soprint('Hello', 'World')prints "Hello World".Step 2: Check other options
print('Hello' 'World') joins strings without space; print('Hello' + 'World') concatenates without space; print('Hello'; 'World') uses invalid syntax.Final Answer:
print('Hello', 'World') -> Option CQuick Check:
Comma adds space in print() [OK]
- Forgetting commas between items
- Using semicolon inside print()
- Concatenating without spaces
print('A', 'B', 'C', sep='-')Solution
Step 1: Understand the sep parameter in print()
Thesepparameter changes the separator between printed items. Here, it is set to '-'.Step 2: Apply sep to the items
Items 'A', 'B', 'C' will be joined with '-' between them, resulting in 'A-B-C'.Final Answer:
A-B-C -> Option BQuick Check:
sep='-' joins items with dash [OK]
- Ignoring sep and expecting spaces
- Confusing sep with end
- Missing quotes around separator
print('Hello' end='!')Solution
Step 1: Check syntax of print() arguments
Arguments inprint()must be separated by commas. Here, 'Hello' andend='!'are missing a comma.Step 2: Understand correct usage
Correct syntax isprint('Hello', end='!')with a comma separating the string and the keyword argument.Final Answer:
Missing comma between arguments -> Option AQuick Check:
Separate print args with commas [OK]
- Forgetting commas between arguments
- Thinking end is not allowed
- Misplacing quotes
Solution
Step 1: Use sep to set commas between numbers
Settingsep=','prints numbers separated by commas: '1,2,3'.Step 2: Use end to replace newline with a period
Settingend='.'prints a period instead of moving to a new line.Step 3: Check argument order
Argument order does not matter, but bothsepandendmust be included in the same print call.Final Answer:
print(1, 2, 3, sep=',', end='.') -> Option DQuick Check:
sep=',' and end='.' print 1,2,3. [OK]
- Swapping sep and end values
- Using two print statements instead of one
- Forgetting to set end to '.'
