How To Take Integer User Input In Python

In this article, we will show you how to take integer user input in Python. It is used to take integer data from users. You can input an integer number, two or more integer numbers, or even a list of integer numbers. To do that, you can use the input() function combined with other methods in Python. Let’s learn more about it with the explanation and example below.

Take Integer User Input In Python

To take integer user input in Python, you can perform the input() method to input user data and other functions to convert it to integer numbers. Let’s learn how to do that in the next title below.

Take an integer user input

To take an integer user input, you can perform the input() method and convert this data to the integer type by the int() function.

Look at the example below.

# Take an integer user input
intNumber = int(input('Input a number: '))

# Check the value and type of this data
print('Value: {}, Type: {}'.format(intNumber, type(intNumber)))

Output

Input a number: 2002
Value: 2002, Type: <class 'int'>

Take two or more integer user input numbers with the map() function

To take two or more integer user input numbers, you can perform the input() method combined with the map() and split() functions.

Look at the example below to learn more about this solution.

# Take three integer numbers
day, month, year = map(int, input('Input the day, month and year: ').split())

# Print out these data
print('Day: {}, Month: {}, Year: {}'.format(day, month, year))

Output

Input the day, month and year: 04 07 2002
Day: 4, Month: 7, Year: 2002

Take a list of integer user input numbers with the list comprehension

To take a list of integer user input numbers, you can perform the input() method combined with the split() function and the list comprehension.

Look at the example below to learn more about this solution.

# Input the list of integer numbers
numbers = [int(x) for x in input('Input the list of integer numbers: ').split()]

# Print out these data
print(*numbers)

Output

Input the list of integer numbers: 10 20 30 40 50 60 70 80 90
10 20 30 40 50 60 70 80 90

Summary

We have shown you how to take integer user input in Python. To do that, you can perform the input() method combined with the int() function. Besides, you can also take two or more integer numbers or even a list of integer numbers with the split() function combined with the map() function or the list comprehension. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *