Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Uncategorized»How to take Multiple Input from User in Python
    Uncategorized

    How to take Multiple Input from User in Python

    March 14, 2023No Comments7 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    How to Accept Multiple User Inputs in PythonThat is a typical issue among novices. It may inquire during the interview. Sometimes, developers must also accept numerous inputs on a single line. That is straightforward in C/C++ using the scanf() technique. Python has two techniques that enable us to accept numerous values or input on a single line.

    • Using split() method
    • Using List Comprehension

    In this article, we will learn how to accept numerous inputs in a single line using different techniques.Method split() UseThe split() technique is helpful for collecting various user inputs. The syntax is provided in the following section.Syntax –

    1. input().split(separator, maxsplit)  

    Parameters –

    • The separator parameter breaks the input by the specified separator. By default, whitespace is the specified separator.

    The split() function is often used to divide a Python string, but it may also be used to get several values.Let’s analyse the following illustration.Example –

    1. # taking two inputs at a time  
    2. a, b, c = input(“Enter three values: “).split()  
    3. print(“Enter Your First Name: “, a)  
    4. print(“Enter Your Last Name: “, b)  
    5. print(“Enter Your Class: “, c)  
    6. print()  
    7.    
    8. # taking three inputs at a time  
    9. x, y, z = input(“Enter three values: “).split()  
    10. print(“Total number of students: “, x)  
    11. print(“Number of passed student : “, y)  
    12. print(“Number of failed student : “, z)  
    13. print()  
    14.    
    15. # taking four inputs at a time  
    16. a, b, c, d = input(“Enter four values: “).split()  
    17. print(“First number is {}, second number is {} third is {} and fourth is {}”.format(a, b, c, d))  
    18. print()  

    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, several inputs are collected on a single line. The values are separated by whitespace; commas (,) or anything else may be used.Using List UnderstandingUsing the map() and split() methods, we can also turn values into a list from a set of values. Let’s analyse the following illustration.Example -2:

    1. # Taking multiple inputs in a single line  
    2. # and type casting using list() function  
    3. x = list(map(int, input(“Enter multiple values: “).split()))  
    4. print(“List of students: “, x)  

    Output:

    Enter multiple values: 4 6 7 2 4
    List of values:  [4, 6, 7, 2, 4]
    

    Explanation: In the preceding code, we utilised whitespace as a separator and typecast the input on one line as a list.Take Matrix User Matrix input is a rectangle array, or rectangular collection of data or numbers. The matrix may accommodate any value, including integers, floating-point numbers, strings, complex numbers, etc. The horizontal values are referred to as “rows,” while the vertical values are referred to as “columns.” If the matrix has r rows and c columns, the order of the matrix will be r x c.Example –

    1. row = int(input(“Enter the number of rows:”))  
    2. column = int(input(“Enter the number of columns:”))  
    3.   
    4. # Initialize matrix  
    5. matrix = []  
    6. print(“Enter the entries row-wise:”)  
    7.   
    8. # For user input  
    9. for i in range(row):         # A for loop for row entries  
    10.     a =[]  
    11.     for j in range(column):  # A for loop for column entries  
    12.         a.append(int(input()))  
    13.     matrix.append(a)  
    14.   
    15. # For printing the matrix  
    16. for i in range(row):  
    17.     for j in range(column):  
    18.         print(matrix[i][j], end = ” “)  
    19.     print()  

    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 preceding may be accomplished in a single line as shown below:Example –

    1. row = int(input(“Enter the number of rows:”))  
    2. column = int(input(“Enter the number of columns:”))  
    3. matrix = [[int(input()) for x in range (column)] for y in range(row)]  
    4. print(matrix)  

    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() and numpy LibraryNumpy is a popular package that may be used for any scientific calculation. It offers significant support for arrays with many dimensions. This library will be used for the input matrix. Let’s analyse the following illustration.Example –

    1. import numpy as np  
    2.     
    3. row = int(input(“Enter the number of rows:”))  
    4. column = int(input(“Enter the number of columns:”))  
    5.     
    6.     
    7. print(“Enter the entries in a single line (separated by space): “)  
    8.     
    9. # User input of entries in a   
    10. # single line separated by space  
    11. entries = list(map(int, input().split()))  
    12.     
    13. # For printing the matrix  
    14. matrix = np.array(entries).reshape(row, column)  
    15. print(matrix)  

    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]]
    

    ConclusionThis article demonstrates the various methods for collecting numerous values from the user. It reduces the amount of lines of code and is fairly simple to use. We have also expressed the same as a matrix where a user-defined matrix may be created.

    How to take Multiple Input from User in Python Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleLocal Variable in Python
    Next Article Amazing hacks of Python

    Related Posts

    python

    Plot With Pandas Python Data Visualization Basics

    March 27, 2023
    python

    Defining and Calling Python Functions

    March 27, 2023
    python

    BreadthFirst Search in Python

    March 27, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.