In Python programming, sometimes you may need to take long integer inputs from the user during program execution. In this tutorial, you will learn the various methods to take long integer inputs in Python 3. By the end of this tutorial, you will become familiar with:
- Taking long integer input using built-in input() and int() functions.
- Reading long integer input from a file.
- Taking long integer input as a command-line argument.
Method 1: Using input() and int() Functions
The simplest method to take a long integer input in Python 3 is by using the built-in input() function, which reads a string from the user, and then converts the string to a long integer using the int() function. Here’s a step-by-step explanation:
- Use the input() function to get the user input as a string.
- Convert the string to a long integer using the int() function.
- Assign the long integer value to a variable.
Here’s an example code that demonstrates how to perform the steps above:
1 2 |
long_integer = int(input("Enter a long integer: ")) print("The long integer you entered is:", long_integer) |
1 2 |
long_integer = int(input("Enter a long integer: ")) print("The long integer you entered is:", long_integer) |
Output:
Enter a long integer: 1234567890987654321 The long integer you entered is: 1234567890987654321
Method 2: Reading Long Integer Input from a File
Another method to get a long integer input is by reading it from a file. This could be useful, especially when you have multiple long integer values to input. Here are the steps to read a long integer input from a file:
- Open the file containing the long integer input in reading mode.
- Read the content of the file using the readline() or read() function.
- Convert the string content to a long integer using the int() function.
- Assign the long integer value to a variable.
Here’s an example file named long_input.txt containing a long integer value:
1234567890987654321
And here’s an example Python code to demonstrate the steps above:
1 2 3 4 |
filename = 'long_input.txt' with open(filename, 'r') as file: long_integer = int(file.read().strip()) print("The long integer from the file is:", long_integer) |
1 2 3 4 |
filename = 'long_input.txt' with open(filename, 'r') as file: long_integer = int(file.read().strip()) print("The long integer from the file is:", long_integer) |
Output:
The long integer from the file is: 1234567890987654321
Method 3: Taking Long Integer Input as a Command-line Argument
You can also take the long integer input as a command-line argument using Python’s built-in sys.argv. Here’s how to do it:
- Import the sys module.
- Retrieve the command-line argument using sys.argv[index].
- Convert the string argument to a long integer using the int() function.
- Assign the long integer value to a variable.
Here’s an example Python code that demonstrates how to perform the steps above:
1 2 3 4 5 6 7 8 |
import sys if len(sys.argv) > 1: long_integer = int(sys.argv[1]) else: long_integer = 0 print("The command-line long integer input is:", long_integer) |
To execute the script with the command-line argument, run the following command in the terminal or command prompt:
shell
python script.py 1234567890987654321
Output:
The command-line long integer input is: 1234567890987654321
Conclusion
In this tutorial, you have learned three different methods to take long integer inputs in Python 3. These methods include using the built-in input() and int() functions, reading from a file, and using command-line arguments. Select the appropriate method based on your program’s requirements and convenience in handling long integer inputs.