Basic input and output techniques used in competitive programming in python

Prateek Surana
May 20, 2018 · 3 min read
Photo by Fabian Grohs on Unsplash

Input

1. Taking single input

2. Taking known number of inputs

1 2 3m,_,n=map(int,input().split())

3. Taking variable number of inputs

I love pythonl=list(input().split())
1 2 3 4 5 6 7l=list(map(int,input().split())

4. Taking fix and variable number of inputs

hello 1 2 3 4s,*l=input().split()
l=list(map(int,l))
hello 3 1 2 3_,n,*l=input().split()
n=int(input())
l=list(map(int,l))

Output

1. Outputting on different lines

result=[1,2,3,4,5]
for i in result:
print(i)
1
2
3
4
5

2. Outputting in the same line

result=[1,2,3,4,5]
for i in result:
print(i,end=' ')

3. Advanced outputting techniques

result=[15,23,32]
for i in range(len(result)):
print("Case #{}: {}".format(i+1,result[i]))
Case #1: 15
Case #2: 23
Case #3: 32
print("I love {}".format("python"))l=['python','is','the','best','language']
print("I love python because {}".format(' '.join(l)))
I love python
I love python because python is the best language

Prateek Surana

Written by

A young Jedi in a galaxy far away.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade