- Write a simple python program to get input from
the user and use int () or float () function with the
read value to change the type.
Answers
Answer:
value = input("Please enter any number : ")
intValue = int(value)
floatValue = float(value)
print(f"You entered {value} which in {type(intValue)} is {intValue} and in {type(floatValue)} is {floatValue}")
Explanation:
int() and float() type conversions in python, taking the input from the user.
Language using : Python Programming
Program :
a=int(input("Enter an integer : "))
#Converting integer 'a' to float
print("Before coverting to float, type of variable a is ",type(a)," and value is : ",a)
a=float(a)
print("After converting, type of variable a is ", type(a), " and value is : ",a)
b=float(input("Enter a float value : "))
#Converting integer 'b' to int
print("Before coverting to integer, type of variable b is ",type(b)," and value is : ",b)
b=int(b)
print("After converting, type of variable b is ", type(b)," and value is : ",b)
Input :
Enter an integer : 20
Enter a float value : 9.786
Output :
Before coverting to float, type of variable a is <class 'int'> and value is : 20
After converting, type of variable a is <class 'float'> and value is : 20.0
Before coverting to integer, type of variable b is <class 'float'> and value is : 9.786
After converting, type of variable b is <class 'int'> and value is : 9
Explanation :
int(variable) converts the float valued variable to integer, and float(variable) converts the integer value to float.
Hope it helps!