In this post, we are going to understand How to Take integer input single or multiple in python using the python in-built input() with examples.
How to take integer input in Python?
To read an integer number as input from the user in Python. The built-in input() function is used. The input() function always takes input as a string to read input as integer or number we need to typecast it with the help of the in-built int() function.
Synatx for input() function
- Prompt: A string message is displayed before input and control is given to the user.
1. Program to take integer input from User in python
In this below python program, we are asking users to input a number. We have printed the number to output and converted it into an integer by using the built-in function input, finally verifying the input number type using the type() function.
number = input("Please enter a number:") print(type(number)) print("you have enetered:",number) #convert entered number to integer number = int(number) print('type after conversion: ',type(number))
Please enter a number:13 <class 'str'> you have enetered: 13 type after conversion: <class 'int'>
Most Popular Related Post
2. How to takes two integer input in Python?
In this example, we are taking two or three number inputs from the users using list compression along with the input() method and typecasting the input using the int() function, and printing the result using the print() method.
# taking two integer input num, num1 = [int(ele) for ele in input("Enter two value: ").split()] print("First Num you entered is: ", num) print("Second Number you entered is: ", num1) print() #taking three integer input num1, num2,num3 = [int(ele) for ele in input("Enter three value: ").split()] print("First Number: ", num1) print("Second Number : ", num2) print("Third Number : ", num3) print()
Enter two value: 12 13 First Num you entered is: 12 Second Number you entered is: 13 Enter three value: 12 13 14 First Number: 12 Second Number : 13 Third Number : 14
3. How to takes multiple integer input as list.
In this example, we will understand how to take multiple integer inputs in one line of code. We will use the list comprehension with input() and split() method (split the input by space).
#python3 program to take array input in python input_list = [ int(num) for num in input("Please enter the numbers:").split()] print(type(input_list)) print("you have entered:", input_list)
Please enter the numbers:12 13 14 15 16 <class 'list'> you have entered: [12, 13, 14, 15, 16]
4. List Map() input() to take mutiple input range of number
In this example, We are using the map() method in list comprehension and split() function. To remove leading and trailing spaces we are using the strip() method.
numcount = int(input("Enter number of elements : ")) list = list(map(int,input("\nPlease Enter numbers : ").strip().split()))[:numcount] print("\nyou have enetered : ", list)
Enter number of elements : 7 Please Enter numbers : 12 13 15 17 18 19 20 you have enetered : [12, 13, 15, 17, 18, 19, 20]
5. How to take multiple input number as a list of lists.
In this example, First asking the user to enter the number of elements, taking user input as a list using the input() method. The input list(list_ele) appending to list (list_of_lists) to create a list of lists.
list_of_lists = [ ] num = int(input("Enter number of elements : ")) for i in range(0, num): list_ele = [input("enter First number:"), int(input("Enter Second number:"))] list_of_lists.append(list_ele) print(list_of_lists)
Enter number of elements : 2 enter First number:12 Enter Second number:13 enter First number:16 Enter Second number:15 [['12', 13], ['16', 15]]
6. How to take any number of inputs using while loop
In this code example, we are using the while loop to take any number of inputs from the user. The loop will terminate if the user enters the ‘done’.Appending each input element to the list.
list =  while True: value = input("Enter a number: ") if value == 'done' : break try: value = float(value) except: print('Invalid input:') continue try: list.append(int(value)) except: list.append(float(value)) print('you have entered:\n',list)
Enter a number: 12 Enter a number: 13.0 Enter a number: 14.5 Enter a number: 16 Enter a number: done you have entered: [12, 13, 14, 16]
In this post, we have learned multiple ways to Take integer inputs from the user. It could be a single or multiple integers in Python using the inbuilt function input() and int() to typecast the input. The split() method to split the input entered by the user.