Receiving input from keyboard in python

Receiving input from keyboard in python

Receiving input from keyboard


The built-in function input () is used to receive input from keyboard. It waits for input till you enter some from keyboard followed by enter key and returns the entered value as string.

Receiving string input from keyboard-

As we know built-in function input( ) is used to receive input from keyboard and it returns entered value as string, so to receive string value from user we will use same input( ) function as shown in the following statement:

 

n = input(” Enter your name: “)

 

Receiving integer input from keyboard-

As we know input() function is used to receive input from keyboard in python and it returns entered value as string, so to receive integer value from user we have to convert value returned from input() function into integer with the help of built-in function int() as shown in the following statement:

 

a = int(input(“Enter your age:“))

 

Receiving floating input from keyboard-

As we know input() function is used to receive input from keyboard in python and it returns entered value as string, so to receive floating value from user we have to convert value returned from input() function into float with the help of built-in function float) as shown in the following statement:

 

w = float(input(“Enter your weight: “)

 

Program for demonstration of receiving input from keyboard

n1= int(input(“Enter first number: “)

n2 = int(input(“Enter second number: “))

s =  n1 + n2

print(“Addition:“,s)

 

Output

Enter first number: 17

Enter second number: 23

Addition: 40

 

If you want to do assignment then for you

 

1. Write a program to calculate inverse of entered number

2.Write a program to calculate area of rectangle where length and breadth of rectangle will be entered through keyboard

3.Write a program to calculate simple interest where principle amount ,rate of interest  will be entered through keyboard

8 comments on “Receiving input from keyboard in python

Leave a Reply

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