In our previous tutorial, we learned how to print messages as output and indentation rules. In this tutorial, we will learn how receive input from the user.
To obtain data or to send any results, developers must communicate with the user. For this reason, Python provides two built-in function to get / read the user data which is slightly different from other programming language because most of the programming language use a dialog box as a way to ask the user to provide some sort of feedbacks.
>>> x = input("enter anything")
input ( ) function takes user input and then evaluates the expression which means Python determines automatically if user has entered a string or a number or a list. By default, data from user is stored as string. Let’s take an example :
We assign data types to the user input variable introducing int, float, str and so on before input function. See more >>
We are going to write a small program that asks user’s name and then we’re going to print for that user with specific greeting messages.
Both these input () and print () are the built-in functions in Python. For the above mentioned questions we are going to call the input function and in between parentheses we are going to add a command which gets printed on the terminal and gives instruction to the user. So, these these text inside parentheses print message on the terminal and and it will wait for the user to enter a value. Whatever the user enters in this input function , is temporarily store in the memory using a variable, for our case name is the variable.
Let’s extend above program and ask two questions. First, person name and secondly their favourite programming language. And then print a message in the pattern like ABC likes Python.
See more about declaring variables in our next article : Click Here >
1 Find Simple intrest using ( SI = P*T*R /100)
2 Find area of rectangle
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to firstname.lastname@example.org . See your articles appearing on AI HUB platform and help other AI Enthusiast.