Python Program to accept integer or float value from user and print


March 2, 2022, Learn eTutorial
2197

What is the difference between integer and float values?

An integer is a Whole number, which will be from zero to any number. And float is a value with rational numbers. The float data type is used to store a double precision real number. 

How to accept an integer or float value in python?

In the last python program, we mention how to accept a string using the function Input. Here we are going to accept an integer value or float value.

In the python language, when we use the function Input it accepts only string values. So we have to convert the accepted string to an integer or float before it is saved in the assigned variable. for that, we use the int and float function. After that, we have to use the print function to display the output.

ALGORITHM

STEP 1: Accept a string from the user and convert that to an integer using the int function.

STEP 2: Print the number which is saved in the variable num.

STEP 3: Accept another string from the user and convert that to float using the float function.

STEP 4: Print the float data using the print function.

Python Source Code

                                          num = int(input("Enter an Integer: "))
print(num)

num = float(input("Enter a float value: "))
print(num)
                                      

OUTPUT

Enter an Integer:  23
23

Enter a float value: 7.7
7.7