Taking input using input() in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use input() in Python, we want to know how the time to get data grows as the input size changes.
We ask: How does the program's waiting time change when the user types more or less data?
Analyze the time complexity of the following code snippet.
user_input = input("Enter some text: ")
print(f"You typed: {user_input}")
This code waits for the user to type something and then prints it back.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading characters one by one from the user until Enter is pressed.
- How many times: Once for each character typed by the user.
As the user types more characters, the time to read input grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character reads |
| 100 | About 100 character reads |
| 1000 | About 1000 character reads |
Pattern observation: The time grows directly with how many characters are typed.
Time Complexity: O(n)
This means the time to get input grows in a straight line as the user types more characters.
[X] Wrong: "Taking input with input() always takes the same time no matter what."
[OK] Correct: Actually, the time depends on how many characters the user types, so more input means more time.
Understanding how input time grows helps you think about program speed and user experience in real projects.
"What if we read input using a loop that reads one character at a time instead of using input()? How would the time complexity change?"
Practice
input() function do in Python?Solution
Step 1: Understand the purpose of
Theinput()input()function waits for the user to type something and press Enter.Step 2: Identify what
It always returns the typed data as a string (text), not numbers or other types.input()returnsFinal Answer:
It asks the user to type something and returns it as text. -> Option CQuick Check:
input()returns text [OK]
- Thinking input() prints text
- Assuming input() stops the program
- Believing input() converts text to numbers automatically
input()?Solution
Step 1: Check the syntax of
The prompt must be inside parentheses and quotes, likeinput()with promptinput('prompt').Step 2: Identify the correct option
Only name = input('Enter your name: ') uses parentheses and quotes correctly to show the prompt.Final Answer:
name = input('Enter your name: ') -> Option BQuick Check:
Prompt text inside parentheses and quotes [OK]
- Missing parentheses around prompt
- Using brackets or braces instead of parentheses
- Not putting prompt text inside quotes
25 when asked?
age = input('Enter your age: ')
print(age + 5)Solution
Step 1: Understand input() returns a string
The variableagewill be the string '25', not the number 25.Step 2: Analyze the print statement
age + 5attempts to add a string and an integer. In Python, this causes a TypeError, but if the code wasprint(age + '5'), it would concatenate to '255'. Since the code isprint(age + 5), it raises a TypeError.Final Answer:
TypeError -> Option DQuick Check:
String + int causes TypeError [OK]
- Assuming input() returns number
- Trying to add int to string without conversion
- Expecting automatic type conversion
number = input('Enter a number: ')
result = number * 2
print(result)Solution
Step 1: Identify the type of
Thenumberinput()function returns a string, sonumberis text.Step 2: Understand what
Multiplying a string by 2 repeats it twice, so if user types '3', result is '33', not 6.number * 2does for stringsFinal Answer:
number is a string, multiplying repeats text instead of doubling number -> Option AQuick Check:
String * 2 repeats text, not math multiply [OK]
- Assuming input returns number
- Expecting string * 2 to double value
- Ignoring type conversion
Solution
Step 1: Convert inputs to numbers
To add numbers, convert both inputs from strings to integers usingint().Step 2: Add the converted numbers
Only a = int(input('First number: ')) b = int(input('Second number: ')) print(a + b) converts both inputs before adding, so it sums correctly.Final Answer:
a = int(input('First number: ')) b = int(input('Second number: ')) print(a + b) -> Option AQuick Check:
Convert both inputs to int before adding [OK]
- Adding strings instead of numbers
- Converting only one input
- Forgetting to convert inputs
