In this tutorial, we’ll learn how to easily get input in Python. We’ll going through multiple examples to properly understand how to get input in Python.
Outline
- Introduction: Get Input In Python
- Python input() Function
- Example 1: Get Name of User
- Example 2: Get Name, Age and Gender by using ID
- Example 3: Input two Numbers and add them
- Example 4: Get Name, ID, Age and Gender of User and store in Python Dictionary
- Conclusion
Introduction: Get Input In Python
As the name suggests, it means allowing the user to input some data. This data can be stored or can used to perform some actions.
Let’s understand practically how to get input in Python by using multiple examples so you can have a better idea of its implementation.
Python input() Function
This function is used to get input in Python. Its syntax is given below:
input('Optional string')
- This function takes an optional string.
- The input that the user provides is returned by this function.
- We can store the returned data in a variable.
- The input will be in a string format.
Example 1: Get Name of User
# input method is used to get input in Python returnedData= input('Enter your name?') print(f'Your name is {returnedData}')
Output
Enter your name? Zeeshan Your name is Zeeshan
We’ve used the input function to take input from user and stored it in a variable. Finally, we’ve printed the data using Python print function. This is a simple method to take name of the user, store it and prints it.
Example 2: Get Name, Age and Gender by using ID
This program will get an ID from user. After that, it’ll check this ID for a match. When it get’s matched, then it’ll print the data of this ID.
dictValues=[{'name':'Yasir','age':26,'gender':'Male','idNumber':3840,{'name':'Zeeshan','age':24,'gender':'Male','idNumber':3456}] idNum= input('Enter your ID number?') for val in dictValues: if(str(val['idNumber'])==idNum): print('Details Found') print('Name: '+val['name']) print('Age: '+str(val['age'])) print('Gender: '+val['gender'])
Output
Enter your ID number? 3456 Details Found Name: Zeeshan Age: 24 Gender: Male
- In this program, we’ve created a list of Python dictionaries.
- After that, we’ve used Python input function to take ID which will be in a string format(by default).
- Then we’ve specified Python for loop which will check value of key with specified input.
- For demonstration, we’ve passed the available ID value and as a result, it has printed all the values of that dictionary.
- This is how we can easily get input in Python, store it and checks it.
Example 3: Input two Numbers and add them
firstNum= input('Enter first number?') secondNum= input('Enter second number?') result=int(firstNum)+int(secondNum) print(f'The sum of {firstNum} and {secondNum} is '+str(result))
Output
Enter first number? 34 Enter second number? 56 The sum of 34 and 56 is 90
- We’ve inputted two numbers by using Python input function.
- By default, the input data is in string format so we’ve converted it to integer using Python int function.
- After that, we’ve added these two numbers.
- Finally, we’ve printed the result.
Example 4: Get Name, ID, Age and Gender of User and store in Python Dictionary
listVals=['name','ID','age','gender'] dictVal={} # Enter name(in characters), id(in numbers), age(in numbers), gender(in M/F) for val in listVals: dictVal.update({val:input(f'Enter {val}:')}) print('Your name is '+dictVal['name']) print('Your ID is '+dictVal['ID']) print('Your age is '+dictVal['age']) print('Your gender is '+dictVal['gender'])
Output
Enter name: Zeeshan Enter ID: 34372898 Enter age: 24 Enter gender: M Your name is Zeeshan Your ID is 34372898 Your age is 24 Your gender is M
This is a simple Python program which will take input from user, store it in a dictionary and prints it. Steps are as follows:
- First we’ve created a Python list with some items. They’ll be used as keys in Python dictionary.
- After that, we’ve created an empty dictionary. New items will be added in that dictionary.
- We’ve used Python for loop which will take details from user and store it in a dictionary using update method.
- Finally, we’ve printed the values of items in dictionary using its keys.
This is how we can easily get input in Python. I’ve tried to explain it using multiple examples so you can have a better idea of how its done. Hope you like this post.
Don’t hesitate to ask if you still have any questions regarding how to get input in Python. I’ll be very glad to answer all.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly get input in Python. I’ll be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
[Solved] How To Convert Python Tuple To String
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Find Minimum Value In Python Tuple [Detailed Python Guide]
How To Use Python String Format_Map Method [Python Easy Guide]
How To Use Python String Title Method [Python Easy Guide]
How To Easily Use Python String Replace Method [Detailed Python Guide]