Taking Multiple Input
Each programming language has 8 standard input-output systems, so the Python programming language is no exception. The input (0) function is used to take user input in a very normal way. In this tutorial, we will learn how to input()
Basic Input
Python basic input method.
# input
input1 = input()
# output
print(input1)
Output for the input “EnableGeek” is:
EnableGeek
Or if you need to take two different inputs separately to find the sum of two numbers, you have to do it like this:
# For Integer Numbers
number1 = int(input())
number2 = int(input())
print(number1+number2)
# For Floating Numbers
number1 = float(input())
number2 = float(input())
print(number1+number2)
Output: for the input: 23, 24, 23.4, 24.4
47
47.8
Using Separator method
That method helps to take multiple inputs at once. The inputs are separated by specific separators. If no separator is provided in advance then any empty space will be treated as a separator. The function is split() through which multiple inputs can be taken.
Example:
# input().split(separator, maxsplit) --> This is the syntax
# taking two inputs at a time
P, Q = input("Enter two values: ").split()
print("Amount of items: ", P)
print("Amount of money: ", Q)
print()
# taking three inputs at a time
P, Q, R = input("Enter three values: ").split()
print("Total number of items: ", P)
print("Number of books is : ", Q)
print("Number of pencil is : ", R)
print()
Output:
Enter two values: 38 12
Amount of items: 38
Amount of money: 12
Enter three values: 10 15 20
Total number of items: 10
Number of books is : 15
Number of pencil is : 20
With this function, we can take any list and input it. The most important data structure in Python programming language is the list where many items are kept in the same variable following a certain procedure.
Example:
# taking two inputs at a time
P, Q = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(P, Q))
print()
# taking multiple inputs at a time as a list
P = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", P)
Output:
Enter two values: 38 10
First number is 38 and second number is 10
Enter multiple values: 6 3 9 282 40 98 233 4
List of students: [6, 3, 9, 282, 40, 98, 233, 4]
Using List comprehension
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
# taking two input at a time
P, Q = [int(num) for num in input("Enter two values: ").split()]
print("First item: ", P)
print("Second item: ", Q)
print()
# taking three input at a time
P, Q, R = [int(num) for num in input("Enter three values: ").split()]
print("Item one: ", P)
print("Item two: ", Q)
print("Item three: ", R)
print()
x_list = [int(item) for item in input("Enter multiple values: ").split()]
print("Number of list is: ", x_list)
Output:
Enter two values: 100 200
First item: 100
Second item: 200
Enter three values: 23 24 25
Item one: 23
Item two: 24
Item three: 25
Enter multiple values: 76 34 54 12 98
Number of list is: [76, 34, 54, 12, 98]
Frequency Check
Suppose you are given a list of multiple integer numbers. The numbers on the list are random, and not organized. But there are some numbers more than once in that list.
Your job is to find out how many times a number has been on the list.
How to solve it? Let’s solve this problem:
Example:
# This function check multiple numbers in a list
def check_freq(x):
freq = {}
for c in set(x):
freq = x.count(c)
return freq
x_list = [int(item) for item in input("Enter multiple values: ").split()]
x_dictionary = check_freq(x_list)
# Print as key-value pair the items as dictionary
print(x_dictionary)
Output
Enter multiple values: 34 34 34 56 56 56 56 56 78 67 90
{34: 3, 56: 5, 78: 1, 67: 1, 90: 1}
Here a function has been created at the beginning. That will take a list as an argument and return a dictionary. The dictionary will show how many times each number has been added as a key-value pair.