How to take integer input in Python?


The acquisition of integer input holds immense significance in various programming tasks, and the Python programming language offers a plethora of techniques to achieve this goal. This article embarks on an insightful journey to explore diverse methodologies for acquiring integer input in Python, focusing on the following strategies:

  • Unveiling the Potential of the `input()` Function and `int()` Type Conversion

  • Harnessing the Versatility of the `map()` Function

  • Unearthing Integer Input from File Sources

  • Attaining Integer Input via Command Line Arguments

Method 1: Unveiling the Potential of the `input()` Function and `int()` Type Conversion

The input() function emerges as a prime method for acquiring user input in Python. It enables the retrieval of a line of input, typically from the user's keyboard, yielding a string. To convert this string input into an integer, the int() function comes into play.

Example

In the following example we will:

1. Obtain a single integer input from the user, store it in the variable 'num', and print the entered value.

2. Capture multiple integer inputs separated by spaces, store them in the list 'nums', and print the entered values.

Example

# Obtaining a single integer input
num = int(input("Please enter an integer: "))
print("You entered:", num)

# Capturing multiple integer inputs separated by spaces
nums = list(map(int, input("Please enter multiple integers 
separated by spaces: ").split()))
print("You entered:", nums)

Output

Please enter an integer: 5
You entered: 5

Please enter multiple integers separated by spaces: 1 2 3 4 5
You entered: [1, 2, 3, 4, 5]

Method 2: Harnessing the Versatility of the `map()` Function

The `map()` function offers a versatile approach to applying a function to multiple items within an iterable, be it a list or a string. In the context of acquiring integer input, `map()` proves to be an invaluable tool for applying the `int()` function to each item within a string consisting of space-separated integers.

Example

In the following example we will:

1. Request multiple integers separated by spaces from the user and split them into a list.

2. Convert the list elements to integers using map(), store them in 'nums', and print the entered values.

Example

# Capturing multiple integer inputs separated by spaces
nums = list(map(int, input("Please enter multiple integers 
separated by spaces: ").split()))
print("You entered:", nums)

Output

Please enter multiple integers separated by spaces: 10 20 30 40
You entered: [10, 20, 30, 40]

Method 3: Unearthing Integer Input from File Sources

In certain scenarios, acquiring integer input from a file becomes a necessity rather than relying solely on user input.This may be fulfilled by utilizing the built-in open() function to get to the record and utilizing the examined() or readline() strategies to recover its contents. Once the contents are received, the int() function can be utilized to change over the string representations into integers.

Example

In the following example we will:

Consider a file named input.txt containing the following data:

5 1 2 3 4 5

The subsequent code snippet retrieves the integer input from the file:

1. Open the file "input.txt" for reading and retrieve a single integer input, storing it in 'num' and printing the value.

2. Read the next line, split it by spaces, convert the elements to integers, store them in 'nums', and print the list of integers.

Example

# Opening the file for reading
with open("input.txt", "r") as file:
    # Retrieving a single integer input
    num = int(file.readline().strip())
    print("First integer in the file:", num)

    # Capturing multiple integer inputs separated by spaces
    nums = list(map(int, file.readline().strip().split()))
    print("List of integers in the file:", nums)

Output

First integer in the file: 5
List of integers in the file: [1, 2, 3, 4, 5]

Method 4: Attaining Integer Input via Command Line Arguments

Command line arguments offer an alternative avenue for providing integer input to a Python script. The sys.argv list contains the command line arguments passed to the script, with the script name itself involving the primary position (sys.argv[0]). By leveraging the int() function, the string arguments can be changed over into integers.

Example

In the following example we will create a Python script named `integer_input.py` with the following steps:

1. Import the sys library to access command line arguments.

2. Convert the arguments to integers, store them in 'args' list, and print the entered values.

Example

import sys

# Attaining integer input from command line arguments
args = [int(arg) for arg in sys.argv[1:]]
print("You entered:", args)

Run the script from the command line with a series of integer arguments:

$ python integer_input.py 10 20 30 40

Output

You entered: [10, 20, 30, 40]

Conclusion

In this comprehensive exploration, we have ventured into diverse methodologies for acquiring integer input in Python. We have uncovered the potential of the `input()` function in conjunction with `int()` type conversion, harnessed the versatility of the `map()` function, unearthed integer input from file sources, and attained integer input via command line arguments. Armed with this knowledge, you can seamlessly acquire integer input in Python, tailoring your approach to suit the requirements of your programming tasks.

Updated on: 28-Aug-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements