Python Programming – Accepting Input From The Console

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better.

Python Programming – Accepting Input From The Console

In Python, input is used to request and get information from the user, and print is used to display information on the screen.

input ( )
In Python, input is accomplished by using an assignment statement combined with a special function called input( ). Syntax of input ( ) is as follows:
variable = input(<prompt>)
Here, the prompt is an expression that serves to ask the user for input; this is almost always a string literal (i.e., some text inside quotation marks). When Python encounters an input expression, it evaluates the prompt and displays the result of the prompt on the screen. It then pauses and waits for the user to type an expression and press the Enter key. The expression typed by the user is then evaluated to produce the result. As the input provided is evaluated, Python expects a valid expression. If the input provided is not correct then either syntax error or exception is raised by Python.

>>> name = input(‘Enter your name: ‘)
Enter your name: Vivek
>>> print(‘Hello’, name)

Note that a comma is used to separate the individual items being printed, causing a space to appear between each when displayed. Thus, the output of the print function, in this case, is Hello Vivek, and not HelloVivek. Let us take some more examples:
>>> x=input(“something:”)
something:10
>>> x
‘ 10 ‘
>>> x=input(“something:”)
something10’ # entered data is treated as string with or without ”
>>> x
‘ 10 ‘
>>> x = input(“x: “)
X : 34 .
type(x)
<class ‘str’> # input( ) function always returns a value of string type
>>> y = input(“y: “)
y: 42
type(y)
<class ‘str’>
>>> print(x + y)
3442
Here, the values entered (34 and 42) would be supplied by the user. Your program would then print out the value 3442, which is the concatenation, i.e., joining of two strings. In order to add them, you need to turn them into numbers. That can be done using int( ) or float( ).
>>> x = int(input(“x: “))
X: 34
type (x)
<class ‘int’>
>>> y = int(input(“y: “))
y: 42
type(y)
<class ‘int’>
>>> print(x + y)
76
Now, type the following:
>>> price=float ( input ( ‘ Enter price: ‘ ) )
Enter price: 12.50
>>> qty=int (input ( ‘ Enter quantity: ‘ ) )
Enter quantity: 3
>>> print(price * qty)
37.5

Let’s Try

Type the following code and write the output.

print(‘Enter an integer value:’)
X = input ( )
print( ‘ Enter another integer value: ‘ )
Y = input( )
num1 = int(x)
num2 = int(y)
print(num1, ‘+’, num2, ‘=’, num1 + num2)

Example 6
program demonstrate how to enter data using input command.

print ( ‘what is your name ‘)
Name = input()
print ( ‘ Hello! ‘ + name + ‘Nice to meet you ! ‘ )RUN
>>>
What is your name?
Priya
Hello! Priya nice to meet you!
>>>

Example 7.
Program to demonstrate input command with a message.

name = input ( ” Enter your name : ” )
print ( “you are Welcome “, name )RUN
>>>
Enter your name: Puja
You are welcome Puja
>>>

Example 8.
Program to accept student’s detail using the input function and display it.

name = input ( ” Enter student Name : ” )
marks = input ( ” Enter Total Marks : ” )
address = input ( ” Enter Address : ” )
Print ( ” printing student’s Details ” )
print ( ” Name ” , ” Marks ” , ” Address ” )
print ( name, marks , address )
RUN
>>>
Enter student Name: Dev
Enter Total Marks: 467
Enter Address: c2, Janak puri
printing student’s Details
Name Marks Address
Dev 467 c2, Janak puri
>>>

The input( ) function reads a line from input and uses this input string in your Python program, converts it into a string, and returns it. Then, you can

Let’s Try

Write the following code and check the output:
(a)

age = input ( ” Your age? ” )
print ( ” You are ” + age + ” years old.” )

(b)

emp_name = input ( ” Enter Employee Name : “)
salary = input ( ” Enter salary : ” )
print ( ” Employee Name “, ” salary “)
print ( emp_name, salary)

whatever you enter as input, the input function converts it into a string. Even if you enter an integer value, still input ( ) function will convert it into a string. You need to explicitly convert it into the integer in your code. Let’s understand this with an example.

Example 9.
Program to check input type in python.

# Program to check input type in Python
number = input (“Enter a number :”)
name = input(“Enter name :”)
print(“Printing type of input value”)
print (“Type of number”, type(number))
print (“Type of name”, type(name))
RUN
>>>
Enter a number:25
Enter name : Ansh
Printing type of input value
Type of number <class ‘str’>
Type of name <class ‘str’>
>>>

Note that in the above-mentioned program, both number and name variable types are str, i.e., string. You know that whatever you enter either any number (integer or float) or string as input, the input( ) function always converts it into a string. So, let’s understand how to accept integer value from a user in Python.
>>> num = input(‘Enter a number: ‘)
Enter a number: 10
>>> num
‘ 10 ‘
Here, you can see that the entered value 10 is a string, not a number. To convert this into a number, you can use int() or float() functions.
>>> int ( ‘ 10 ‘ )
10
>>> float ( ‘ 10 ‘ )
10 . 0
This same operation can be performed using the eval ( ) function.

Example 10.
Program to add two inputted numbers by converting both the numbers into an inttype using int( ).

# Program to add two inputted numbers
first_num = int ( input (“Enter first number : “) )
secondnum = int ( input (“Enter second number : “) )
sum = first_num + secondnum
print(“Addition of two number is: “, sum)RUN
>>>
Enter first number: 12
Enter second number: 10
The addition of two number is: 22
>>>

Note that in Figure 3.32 you have explicitly added a cast of an integer type to an input function, i.e., you converted input value to the integer type using int(). Now if you print the type of first_num, you will get the type as “int”.
>>> type(first_num)
<class ‘ int’ >
You can also accept float value from a user in Python and convert user input into float number using the float( ) function.

Let’s Try

Write the following code in python and check the output:

# program to accept float value from the user
float_num = float ( input ( ” Enter any float number : ” ) )
print ( ” input float number is : ” , float_num )
print ( ” type is : ” , type ( float_num) )

write the following codes in python and run the program assuming a=2 and b=4. Also, write the reason behind the first program producing incorrect output.

Let’s Try

Write the following code in Python and check the output:

# Program to accept float value from the user
float_num = float (input(“Enter any float number :”) )
print (“Input float number is: “, float_num )
print (“Type is:”, type(float_num) )

Write the following codes in Python and run the son behind the first program producing incorrect programs assuming a=2 and b=4. Also, write the rea- output.

a = input() a = int(input())
b = input() b = int(input())
s = a + b s = a + b
print(s) print(s)

eval ( )

eval() function is used to evaluate the value of a string. It takes a string as an argument, evaluates this string as number, and returns the numeric result. It can evaluate even expressions, provided the input is a string:

>>> eval ( ‘ 27 ‘ )
27
>>> eval ( ‘ 27 . 67 ‘ )
27 . 67
>>> eval ( ‘ 5+2 ‘ )
7
>>> eval ( “7*4+2” )
30

Example 11.
Program to convert given temperature in celsius into Fahrenheit.Use eval ( )for input.

#program to convert temperature in celsius into Fahrenheit
temp = eval (input ( ‘ Enter a temperature in celsius : ‘ ) )
print ( ‘ In Fahrenheit, that is ‘ , 9/5*temp+32)RUN
>>>
Enter a temperature in Celsius : 0
In Fahrenheit, that is 32. 0
>>>

Example 12.
program to calculate average of two inputted numbers. Use eval ( ) to output numbers.

#program to calculate average of two inputted numbers
num1 = eval ( input ( ‘ Enter the first number : ‘) )
num2 = eval ( input ( ‘ Enter the second number : ‘ ) )
print ( ‘ The average of the numbers you entered is ‘ , (num1+num2)/2)RUN
>>>
Enter the first number: 65
Enter the first number: 72
The average of the number you entered is 68 . 5
>>>

Refer More:

ZEEL Pivot Point Calculator

Leave a Reply

Your email address will not be published. Required fields are marked *