In this Python Tutorial i want to show you about How to Get Input in Python, so some times in programming you need to get the user input, and based on that user input you want to do some certain actions. most programs today use a dialog box as a way of asking the user to provide some type of input. if you are familiar with C++ or Java, for example in C++ we can use cin for text input, in Python we can use input() method for getting the user input. This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list.
1 2 |
name = input("Enter your name : ") print("Your Name is : " , name) |
So in the above example first we are going to get the input from the user with input() method and after that we want to print the input value in the console.
Now this is the result.
If you want to enter a number in this input, this will be the result.
1 2 |
number = input("Please enter a number : ") print(number) |
Result
Let’s create another example. in this example we want to create a simple calculator, basically we are going to get two numbers from the user and after that we do arithmetic operations on the numbers.
1 2 3 4 5 6 7 8 9 10 11 12 |
num1 = int(input("Please enter first number :")) num2 = int(input("Please enter second number :")) add = num1 + num2 minus = num1-num2 multiply = num1 * num2 division = num1 / num2 print("Addition", add) print("Minus", minus) print("Multpilcation" , multiply) print("Division", division) |
Run the code and this is the result.