Python is a popular, easy-to-learn programming language that allows developers to create a wide range of applications. One of the fundamental operations that every programmer should know is how to read input from the user, as this enables your program to interact with the user and perform tasks based on their input.
In this tutorial, we will cover different methods of reading input in Python, such as using the input()
function, reading from files, and using command-line arguments.
Step 1: Using the input() Function
The most straightforward way to read user input is to use the built-in input()
function. This function accepts an optional string parameter, which is used as a prompt to display to the user. When called, input()
waits for the user to enter text and then returns that text as a string.
Here’s an example of how to use the input()
function:
1 2 |
name = input("Enter your name: ") print("Hello, " + name) |
The input()
function always returns a string, so if you need to request a numerical value from the user, you will need to convert the received input to the appropriate type. For example:
1 2 |
age = int(input("Enter your age: ")) print("You will be", age + 1, "years old next year.") |
In this example, we use the int()
function to convert the user’s input into an integer. You can also use float()
for floating-point numbers.
Step 2: Reading Data from a File
Another common way to read input in Python is by reading data from a file. To do this, you will need to use the open()
function to open the file and create a file object. Once the file is opened, you can use methods such as read()
, readline()
, and readlines()
to read the contents of the file.
Here’s a simple example of how to read data from a text file called “example.txt”:
1 2 3 4 |
file = open("example.txt", "r") content = file.read() print(content) file.close() |
In this example, the "r"
parameter passed to the open()
function tells Python to open the file in read mode. The read()
method is called on the file object to read the contents of the file as a single string. Finally, we close the file using the close()
method.
A more Pythonic way to read a file is by using the with
statement, which takes care of closing the file for you:
1 2 3 |
with open("example.txt", "r") as file: content = file.read() print(content) |
Step 3: Parsing Command-Line Arguments
Sometimes, you may want to read input from the command line when your Python script is executed. To do this, you can use the sys.argv
list, which contains the command-line arguments passed to the script.
For instance, here’s an example of a Python script that prints the sum of two numbers provided as command-line arguments:
1 2 3 4 5 6 7 |
import sys num1 = int(sys.argv[1]) num2 = int(sys.argv[2]) sum = num1 + num2 print("The sum is:", sum) |
In this example, we import the sys
module to access the argv
list, which contains the script name and any command-line arguments. We ignore the script name itself (contained in sys.argv[0]
) and convert the arguments to integers before performing the addition.
To execute this script, save it as “add_numbers.py” and run it from the command line:
1 |
$ python add_numbers.py 3 5 |
This will output:
The sum is: 8
Conclusion
In this tutorial, we have covered three methods to read input in Python: the input()
function for user prompts, reading data from files, and parsing command-line arguments using the sys.argv
list. Now you should be able to incorporate different types of input into your Python programs, allowing for more interactive and dynamic applications.