How to Take User Input in Python
How to take User Input:?
In Python we can take user input with built-in
function input() function. It is library function of Python which is used to
take user input through the terminal at run time.
For example,
My_name=input("Enter your name: ")
print("Hello," + My_name +
"!")
Output:
Enter your name: Dr. Manisha. M. More
Hello,Dr. Manisha. M. More!
In above example My-name is a variable which
is used to store the value which is entered at run time.
input() function is built-in function or
library function in Python used to enter values at run time. print() is used to display result on screen.
In the parenthesis of print() in double quotation Hello string is written and
after this + character is used, Here + character combine two strings one is
Hello and another is the string stored in My_name variable which is entered at
run time.
Another Example to input values at run time
number1=input("Enter first value:")
number2=input("Enter second number:")
print("Entered number is:",number1)
print("Entered second number is:",number2)
a1=input()
print("The third number is",a1)
Output:
Enter first value:200
Enter second number:300
Entered number is: 200
Entered second number is: 300
400
The third number is 400
In above example number1 and number2 are two variable
used to store two different values. Here the type of variable is not defined,
when user enter any value so based on the value it will be considered by the
specific type. In above example number1 variable store 200 and it is considered
as integer, number2 also have 300 an integer value.
This method is called dynamic typing. Like other
programming language variables are not explicitly defined by programmer. If
programmer does not mention any type of variable, then type of variable is
considered based on values stored in a variable.
Another
variable to input variable by defining the type of variable with the input()
function
n1=int(input("Enter first number:"))
n2=float(input("Enter second number:"))
string1=input("Enter your string:")
print("First number is:",n1)
print("Second number is:",n2)
print("The string is:",string1)
Output:
Enter first number:20
Enter second number:30.50
Enter your string:Python programming
First number is: 20
Second number is: 30.5
The string is: Python programming
In this example n1, n2 and
string1 variables are declared with its data type with user input function
input(). n1, n2 and string1 variables
are with its type respectively integer, float and string.
Another example to take user
input and perform addition of two integers.
number1=input("Enter
first number")
number2=input("Enter
second number")
addition=number1+number2
print("The addition
is:" , addition)
Output:
Enter first number20
Enter second number20
The addition is: 2020
In this example two integer
user inputs are stored in number1 and number2 variables. Then addition variable
is used to perform addition of two numbers. + operator is used to perform
addition. But in result instead of addition of two integers two integers are
combined and displayed as a result. This is happening because we have not
defined the data type of number1 variable and number2 variable and + operator
is used. In this case the compiler will consider the type of number1 and
number2 is string and it use this + character to combine these two strings.
This problem is type casting
or type conversion. Type casting in Python refers to changing the data type or
value of variable from one type to another type.
For example data type
conversion from int to string the str() is used. From string to int the int()
is used.
So to avoid type casting
problem we can define and take user input as follows number1=int(input("Enter
first value:"))
number2=int(input("Enter
second number:"))
sum=number1+number2
print("The addition of
two numbers is:",sum)
sub=number1-number2
print("The subtraction
is:",sub)
mult=number1*number2
print("The multiplication
is:",mult)
div=number1/number2
print("The division
is:",div)
rem=number1%number2
print("The remainder of
division is:",rem)
Output:
Enter first value:200
Enter second number:100
The addition of two numbers
is: 300
The subtraction is: 100
The multiplication is: 20000
The division is: 2.0
The remainder of division is:
0
Another way to work on type
casting is as follows,
number1=(input("Enter
first value:"))
number2=(input("Enter
second number:"))
sum=int(number1)+int(number2)
print("The addition of
two numbers is:",sum)
sub=int(number1)-int(number2)
print("The subtraction
is:",sub)
mult=int(number1)*int(number2)
print("The multiplication
is:",mult)
div=int(number1)/int(number2)
print("The division
is:",div)
rem=int(number1)%int(number2)
print("The remainder of
division is:",rem)
Output:
Enter first value:200
Enter second number:100
The addition of two numbers
is: 300
The subtraction is: 100
The multiplication is: 20000
The division is: 2.0
The remainder of division is:
0
In this example we have used
only input function to take user input and by default this input is considered
as string. The conversion of string data type to integer data type is done with
the int(number1) and int(number2) in the operation of arithmetic operation. Hence
string is converted to integer data type.
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏