For instance, in C we can do something like this:
One solution is to use raw_input() two times.
Another solution is to use split()
Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default. One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line
x, y = [int(x), int(y)]
# We can also use list comprehension
x, y = [int(x) for x in [x, y]]
Below is complete one line code to read two integer variables from standard input using split and list comprehension
Python3
x, y = [int(x) for x in input().split()]
|
Python3
x, y = map(int, input().split())
|
Instead of using the input function to read a line of input from the user and then processing the line to extract the values, you can use the sys.stdin.readline function to read a line of input and then use the split method and a list comprehension to extract and convert the values to the desired type.
Python3
import sys
line = [int(x) for x in sys.stdin.readline().split() ]
|
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
29 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...