How to take Multiple Input from User in Python29 Aug 2024 | 4 min read It is a common problem for the beginners. It may ask in the interview. Sometimes, the developers also need to take the multiple inputs in a single line. It can be easily done in the C/C++ using the scanf() method. However, Python provides the two methods that help us to take multiple values or input in one line.
In this tutorial, we will learn how to take multiple inputs in one line using various methods. Using split() MethodThe split() method is useful for getting multiple inputs from users. The syntax is given below. Syntax - Parameters -
The split() method is used to split the Python string, but we can use it to get the multiple values. Let's understand the following example. Example - Output: Enter three values: David Warner MCA Enter Your First Name: David Enter Your Last Name: Warner Enter Your Class: Warner Enter three values: 100 67 33 Total number of students: 100 Number of passed students : 67 Number of failed students : 33 Enter four values: 1 2 3 4 First number is 1, second number is 2 third is 3 and fourth is 4 Enter multiple values: 4 6 7 2 4 List of students: [4, 6, 7, 2, 4] Explanation - In the above code, we take the multiple inputs in a single line. The values are separated by the whitespace, you can use comma (,) or anything. Using List ComprehensionWe can also take values and convert them into the list using the map() method along with the split() method. Let's understand the following example. Example -2: Output: Enter multiple values: 4 6 7 2 4 List of values: [4, 6, 7, 2, 4] Explanation - We used whitespace as a separator in the above code and taken the input in one line and type casted it into a list. Take Matrix Input from UserMatrix is a rectangular array, or we can say rectangular arrangement of data or numbers. The matrix can take any value such as integer values, floating values, string, complex numbers, etc. The values are placed horizontally called rows, while vertical values are called 'columns'. If the matrix consists of r number of rows and c number of columns, the order of the matrix will be r x c. Example - Output: Enter the number of rows:3 Enter the number of columns:3 Enter the entries row-wise: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 The above can be implemented in the one line as below. Example - Output: Enter the number of rows:3 Enter the number of columns:3 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Using map() function and numpy LibraryThere is a popular library called Numpy which can use for any scientific computation. It provides the extensive support for multidimensional arrays. We will use this library for input matrix. Let's understand the following example. Example - Output: Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]] ConclusionIn this tutorial, we have shown the different ways to take multiple values from the user. It saves the number of code lines and is quite easy to use. We have also described the same as a matrix where we can create a user-define matrix. Next TopicPython zlib Library |
In the tutorial, we will learn about the Python's popular library for the data engineer. These libraries make data engineer life easy. As we know, Python is the most popular language for the machine learning. We will list down those popular libraries with the code. Top Six...
8 min read
Python is an object-oriented programming language. Everything is in Python treated as an object, including variable, function, list, tuple, dictionary, set, etc. Every object belongs to its class. For example - An integer variable belongs to integer class. An object is a real-life entity. An object...
2 min read
| Generate Secure Random Numbers In this tutorial, we will learn about one of the interesting Python modules, named as secret. We will also learn its methods and how it is different from the random module. It was released in Python 3.6 and is popularly known...
5 min read
Librosa is valuable Python music and sound investigation library that helps programming designers to fabricate applications for working with sound and music document designs utilizing Python. This Python bundle for music and sound examination is essentially utilized when we work with sound information, like in the...
4 min read
In this article, we are going to discuss what is os path curdir in Python, how to change the working directory, and how to retrieve the current directory using Python. The OS Python Module provides a portable way to communicate with the operating system. Tools for locating...
3 min read
Hi folks, if we need to learn Python and search for the best Coursera courses for Python then we have come to the perfect guide. There are numerous websites to get a Python Programming course but Coursera is quite possibly the famous rumored one. The positive point...
7 min read
When and How to Use StandardScaler? When the features of the given dataset fluctuate significantly within their ranges or are recorded in various units of measurement, StandardScaler enters the picture. The data are scaled to a variance of 1 after the mean is reduced to 0 via StandardScaler....
4 min read
In Data visualization, plots are the most effective ways of representing the data visually. If it is not plotted in a detailed form it can appear complicated. Python has Matplotlib, which is used for representing data in the plotting form. The user should optimize the size of...
3 min read
One method for learning about anything, like music, is to look for significant groupings or collections. While our friends may arrange music by decade, we may arrange music by genre, and our choice of groups aids in understanding the distinct elements. What is Clustering? One of the unsupervised...
11 min read
Create a Virtual Environment: In this tutorial, you will fabricate your undertaking structure. You can name the root envelope of your venture a way you like. For instance, you could name it rp_flask_api/. Make the organizer and explore into it: Syntax to Create a Folder in the Shell: $...
18 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India