Reading multiple values:
There can be many ways to take multiple in inputs from the user. Some of them are shown below.
x,y=int(input("Enter first number")),int(input("Enter second number"))
x,y=input("Enter two numbers").split()
x,y=map(int,input().split())
Note* x,y=int(input("Enter two numbers")).split() is wrong. This will show ValueError.
Taking different Values as input using eval() method:
x,y,z=[eval(x) for x in input().split()]
If we give input: 1 2 "mayank"
x and y type are int and z type will be string automatically. We don't need any type of conversion here because we are using eval() function for that.
Taking list as input from user:
l=[eval(x) for x in input().split()]
l=list(map(int,input().split()))
Taking tuple as input from user:
t=tuple([eval(x) for x in input().split()])
t=tuple(map(int,input().split()))
Taking set as input from user:
s=set([eval(x) for x in input().split()])
s=set(map(int,input().split()))
Taking dictionary as input from user:
d={int(input("enter key ")) : input("enter value ") for i in range(3)}
Here second input() method execute before the first input() method.
d={x: input() for x in range(3)}
Here 0 1 and 2 will becomes key and values will be enter by user.
There can be many ways to take multiple in inputs from the user. Some of them are shown below.
x,y=int(input("Enter first number")),int(input("Enter second number"))
x,y=input("Enter two numbers").split()
x,y=map(int,input().split())
Note* x,y=int(input("Enter two numbers")).split() is wrong. This will show ValueError.
Taking different Values as input using eval() method:
x,y,z=[eval(x) for x in input().split()]
If we give input: 1 2 "mayank"
x and y type are int and z type will be string automatically. We don't need any type of conversion here because we are using eval() function for that.
Taking list as input from user:
l=[eval(x) for x in input().split()]
l=list(map(int,input().split()))
Taking tuple as input from user:
t=tuple([eval(x) for x in input().split()])
t=tuple(map(int,input().split()))
Taking set as input from user:
s=set([eval(x) for x in input().split()])
s=set(map(int,input().split()))
Taking dictionary as input from user:
d={int(input("enter key ")) : input("enter value ") for i in range(3)}
Here second input() method execute before the first input() method.
d={x: input() for x in range(3)}
Here 0 1 and 2 will becomes key and values will be enter by user.
Be First to Post Comment !
Post a Comment