Python Input Guide for Competitive Coding
1. Introduction: Why Input Matters in Competitive Coding
When solving competitive programming problems, reading and processing input
efficiently is one of the first things a coder must learn. Problems usually come with very
specific input formats, and misunderstanding them can lead to wrong answers or errors,
even if your algorithm is correct.
Understanding how to take input properly:
1. Saves time during contests.
2. Helps avoid runtime and parsing errors.
3. Allows your code to handle different types of test cases correctly.
Also remember: input in Python is taken as a string of text, even when it looks like
numbers, brackets, or lists. You are responsible for converting it to the right structure
using methods like split(), map(), or int().
2. The input() Function in Python
Python provides a built-in function input() to capture input from the user or from the
standard input (stdin). We won’t be looking at or using stdin for this course. We’ll focus on
the beginner friendly concepts.
2.1 Syntax
value = input()
By default, input() returns a string. You must convert it to the desired type if needed.
2.2 Example: Taking a Name
name = input()
print("Hello,", name)
This works fine for simple inputs. But competitive programming usually involves multiple
values, lists, or structured data in a single line.
Important: Even if the input looks like numbers or lists, Python sees it as plain text. You
must break it apart and convert it.
3. Reading Common Input Types
3.1 Single Integer
n = int(input())
Example input: 5
The number 5 is entered as text, and we use int() to convert it.
3.2 List of Integers in One Line
arr = list(map(int, input().split()))
Example input: 1 2 3 4 5
This input format reads a line of a single space-separated string (i.e. "1 2 3 4 5") .This
string then gets split into words ( individual strings): ['1', '2', '3', '4', '5'] and then converted
to integers.
● input() → takes a line of input (i.e. "1 2 3 4 5")
● .split() → splits that string into ['1', '2', '3', '4', '5']
● map(int, ...) → converts each to an integer: 1,2,3,4,5
● list(...) → wraps it into a full list: [3, 5, 2, 9]
● Final result: arr = [3, 5, 2, 9]
3.3 List of Strings
names = input().split()
Example input: Alice Brian Claire David
Each word becomes a separate item in the list.
This reads a line of space-separated strings and stores them in a list called names.
● input() → gets something like "Alice Brian Claire David"
● .split() → breaks it into ["Alice", "Brian", "Claire", "David"]
Final result: names = ["Alice", "Brian", "Claire", "David"]
3.4 Multiple Inputs Assigned to Variables
a, b = map(int, input().split())
Example input: 10 20
This method splits the input and assigns 10 to a and 20 to b.
● input()
○ Reads a line of user input as a string
○ Example: "10 20"
● .split()
○ Splits the string at spaces
○ Result: ["10", "20"]
● map(int, ...)
○ Converts each item in the list to an integer
○ Result: map object → 10, 20
● a, b = ...
● Unpacks the two integers into variables
● Assigns:
● a = 10
● b = 20
3.5 Reading a Float
f = float(input())
Example input: 3.14
● input(): Receives a line of user input — in this case: 3.14
● float(...): Converts the string "3.14" into the floating-point number 3.14
● f = : Stores the resulting float value in the variable f
4. Why input() Isn’t Always Enough
The input() function alone works fine for most simple tasks. However, in competitive
coding:
1. You often need faster input (for huge inputs, sys.stdin.readline() may be used
but is considered advanced).
2. You often need to split, map, or transform the input to the right types.
3. Inputs may contain more than one variable per line or multiple lines, and
each must be handled properly.
We stick with input() and simple methods like .split() and map() in this course to keep
things beginner-friendly.
5. Common Input Mistakes (And Fixes)
Mistake Example Problem Fix
Forgetting to convert type age = input() Stores as Use int(input())
string
Not splitting a list nums = input() Just one Use .split()
long
string
Wrong number of variables a, b = map(int, ValueErro Check input
unpacked input().split()) with input r length
5
Using input() without loop Only reads once Misses Use for _ in
when multiple lines data range(n): loop
expected
6. Practical Input Patterns for Contests
6.1 Fixed-Size Input
This means you are told exactly how many numbers you need to read. It is called
“fixed-size” because the number of items is known ahead of time.
For example:
n = int(input())
arr = list(map(int, input().split()))
Input:
5
12345
Explanation:
· The first line (5) tells you how many elements are expected.
· The second line provides exactly 5 numbers.
· You use this number to verify the list is complete or control a loop.
Why it’s useful:
· Prevents errors like reading too much or too little.
· Helps in validating the data size in real time.
Common Questions:
· What if fewer numbers are entered? → You’ll get a ValueError or incomplete list.
· Can we still use split()? → Yes! But make sure the input length matches.
6.2 Unknown Number of Inputs in One Line
In some problems, you don’t know how many numbers will be given. You’re simply told:
“All numbers are in a single line.”
arr = list(map(int, input().split()))
Input:
13957
Explanation:
· The entire line is a space-separated string.
· split() breaks it into words.
· map(int, ...) converts each to an integer.
Why it’s useful:
· You don’t have to count or check the number of items.
· len(arr) can tell you how many items you got.
Common Questions:
· What if it’s empty? → You’ll get an empty list.
· What if there’s an extra space? → split() handles that.
6.3 Input Over Multiple Lines
Sometimes, the problem gives you a number of lines and expects input on each.
n = int(input())
lines = []
for _ in range(n):
lines.append(input())
Input:
3
apple
banana
mango
Explanation:
· The first line tells how many times to read.
· Loop collects one line per item.
Why it’s useful:
· Helps in string-based problems, lists, or multi-step input.
Common Questions:
· What if one line is empty? → It’ll still be stored as ''.
· What if input is shorter? → Loop waits until all n lines are entered.
6.4 Tuple Input (Two Variables)
This pattern is used when exactly two values are expected on one line.
x, y = map(int, input().split())
Input:
47
Explanation:
· Splits the line and assigns the first number to x, second to y.
Why it’s useful:
· Great for coordinate-based problems.
· Useful in geometry, age/score pairs, time values, and more.
Common Questions:
· What if more than 2 numbers are entered? → You get a ValueError.
· Can I use float() instead of int()? → Yes, if values aren’t integers.
7. Tips for Beginners
1. Always read the input format carefully in the problem.
2. Remember: input is just a string. Convert it as needed.
3. Use map() with input().split() for multiple values.
4. Use list(map(int, input().split())) when reading a list of numbers.
5. Check input length if you’re getting ValueErrors.
6. Practice reading different formats by writing input-only problems.
8. Summary
1. input() is your main tool for reading user data.
2. Use .split() and map() to convert and organize data.
3. Be cautious about data types and number of values expected.
4. Understand the difference between fixed-size and unknown-size inputs.
5. Input is always just a text string — it’s your job to shape it into data.
6. For this course, we will focus on 1D inputs only.
With regular practice, input handling will become second nature. Once you master this,
solving problems becomes a lot easier because you can focus on logic rather than
struggling with data entry.