Multiple ways of taking input in python
There are a multiple ways to take input.
In most of the coding problems,we have to take input in different formats.
Let's see some of the formats now.
To take integer input:
x=int(input())
Values are integers,if we print x value,x will be printed as integer value.
To take float input,that is decimal values like 5.5,0.5,-0.6 etc.
x=float(input())
we can also give integer values as input like 5,-5 etc.But if we print x,it will print
them in float values i.e. 5.0,-0.5,-0.6
To take string input,i.e. characters or sentences.
x=input()
inputs like p,python,python is best.
Take n number of integer inputs one after another.
n=int(input())
for i in range(n):
x=int(input())
Take n number of float inputs one after another.
n=int(input())
for i in range(n):
x=float(input())
Take n number of string inputs one after another.
n=int(input())
for i in range(n):
x=input()
Take unknown number of integer inputs at a time,in a single line separated by space.
x=list(map(int,input().split()))
or
x=list(map(int,input().split(' ')))
Here list is used to store all values in a single list called x.
Take unknown number of float inputs at a time,in a single line separated by space.
x=list(map(float,input().split()))
or
x=list(map(float,input().split(' ')))
Just replace int with float.
Take unknown number of string inputs at a time,in a single line separated by space.
x=list(map(str,input().split()))
or
x=list(map(str,input().split(' ')))
Just replace int with str.
Take n number of integer inputs at a time,in a single line separated by space.
n=int(input()
x=list(map(int,input().split() [0:n]))
Here,if we have given the number of inputs,we can use this.
otherwise go for unknown number of inputs..if user do not give n value specifically.
take two inputs in a single line separated by space.
x,y=map(int,input().split())
for three inputs:
x,y,z=map(int,input().split())
if user needs to take inputs in other formats like separated by dot or comma or any character,
just change the split() function as split(',') for comma
split('.') for dot and etc.
For more useful stuff on python,see my other posts,i have added better and user related stuff.
In most of the coding problems,we have to take input in different formats.
Let's see some of the formats now.
To take integer input:
x=int(input())
Values are integers,if we print x value,x will be printed as integer value.
To take float input,that is decimal values like 5.5,0.5,-0.6 etc.
x=float(input())
we can also give integer values as input like 5,-5 etc.But if we print x,it will print
them in float values i.e. 5.0,-0.5,-0.6
To take string input,i.e. characters or sentences.
x=input()
inputs like p,python,python is best.
Take n number of integer inputs one after another.
n=int(input())
for i in range(n):
x=int(input())
Take n number of float inputs one after another.
n=int(input())
for i in range(n):
x=float(input())
Take n number of string inputs one after another.
n=int(input())
for i in range(n):
x=input()
Take unknown number of integer inputs at a time,in a single line separated by space.
x=list(map(int,input().split()))
or
x=list(map(int,input().split(' ')))
Here list is used to store all values in a single list called x.
Take unknown number of float inputs at a time,in a single line separated by space.
x=list(map(float,input().split()))
or
x=list(map(float,input().split(' ')))
Just replace int with float.
Take unknown number of string inputs at a time,in a single line separated by space.
x=list(map(str,input().split()))
or
x=list(map(str,input().split(' ')))
Just replace int with str.
Take n number of integer inputs at a time,in a single line separated by space.
n=int(input()
x=list(map(int,input().split() [0:n]))
Here,if we have given the number of inputs,we can use this.
otherwise go for unknown number of inputs..if user do not give n value specifically.
take two inputs in a single line separated by space.
x,y=map(int,input().split())
for three inputs:
x,y,z=map(int,input().split())
if user needs to take inputs in other formats like separated by dot or comma or any character,
just change the split() function as split(',') for comma
split('.') for dot and etc.
For more useful stuff on python,see my other posts,i have added better and user related stuff.
Comments
Post a Comment