In this tutorial, we will learn how to take space-separated integer input in Python 3. This kind of input is quite common while working with programming problems, especially when reading inputs for matrix or list problems.
Python provides multiple ways to handle this, and we will explore three different methods in this tutorial.
Method 1: Using list comprehension and split()
The simplest way of taking space-separated integer input in Python 3 is by using the list comprehension and split() methods together. Here are the steps:
1. Use the input() function to read a line of space-separated integers.
2. Use the split() method to split the input line into separate values.
3. With the help of list comprehension, convert the string values into integers.
1 |
input_line = input()<br>input_list = [int(x) for x in input_line.split()] |
This method reads a line of space-separated integers and converts them into a list of integers.
Method 2: Using map() and split()
Another way to take space-separated integer input is by using the map() and split() functions together. Here are the steps:
1. Use the input() function to read a line of space-separated integers.
2. Use the split() method to split the input line into separate values.
3. Use the map() function to convert the string values into integers and then convert the result into a list.
1 |
input_line = input()<br>input_list = list(map(int, input_line.split())) |
This method is similar to method 1 but uses map() to convert the string values into integers.
Method 3: Using * and split()
This method involves using the * (asterisk) operator along with the split() method. Here are the steps:
1. Use the input() function to read a line of space-separated integers.
2. Use the split() method to split the input line into separate values.
3. Use the * operator along with the split() method and pass it directly as arguments to a function or variables.
In this example, we’ll store the space-separated integers as variables (a, b, and c):
1 |
a, b, c = map(int, input().split()) |
Test the methods
To test these methods, we will write a simple program that takes a line of space-separated integers as input and calculates their sum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
def main(): input_line = input("Enter space-separated integers: ") # Method 1: Using list comprehension and split() input_list1 = [int(x) for x in input_line.split()] print(f"Method 1: Input list is: {input_list1}") print(f"Method 1: Sum is: {sum(input_list1)}") # Method 2: Using map() and split() input_list2 = list(map(int, input_line.split())) print(f"Method 2: Input list is: {input_list2}") print(f"Method 2: Sum is: {sum(input_list2)}") # Method 3: Using * and split() numbers = list(map(int, input_line.split())) print(f"Method 3: Numbers are: {numbers}") print(f"Method 3: Sum is: {sum(numbers)}") if __name__ == "__main__": main() |
Output
Enter space-separated integers: 1 2 3 4 5 Method 1: Input list is: [1, 2, 3, 4, 5] Method 1: Sum is: 15 Method 2: Input list is: [1, 2, 3, 4, 5] Method 2: Sum is: 15 Method 3: Numbers are: [1, 2, 3, 4, 5] Method 3: Sum is: 15
Conclusion
In this tutorial, we have learned three different methods to take space-separated integer input in Python 3. You can choose any of these methods according to your preference and the requirements of your task. Each method has its unique syntax, but all of them are efficient and easy to use for handling space-separated integer inputs.