Home » Python Show Python | read/take input as a float: Here, we are going to learn how to read input as a float in Python? To take input in Python, we use input() function, it asks for an input from the user and returns a string value, no matter what value you have entered, all values will be considered as strings values. Consider the following example, # python code to demonstrate example # of input() function val1 = input("Enter any value: ") print("value of val1: ", val1) print("type of val1: ", type(val1)) val2 = input("Enter another value: ") print("value of val2: ", val2) print("type of val2: ", type(val2)) val3 = input("Enter another value: ") print("value of val3: ", val3) print("type of val3: ", type(val3)) Output Enter any value: 10 value of val1: 10 type of val1: <class 'str'> Enter another value: 10.23 value of val2: 10.23 type of val2: <class 'str'> Enter another value: Hello value of val3: Hello type of val3: <class 'str'> See the program and output – Here, we provided three value "10" for val1 which is an integer value but considered as a string, "10.23" for val2 which is a float value but considered as a string, "Hello" for val3 which is a string value. How to take input as a float?There is no such method, that can be used to take input as a float directly – but input string can be converted into a float by using float() function which accepts a string or a number and returns a float value. Thus, we use input() function to read input and convert it into a float using float() function. Consider the below example, # python code to take float input # reading a value, printing input and it's type val1 = input("Enter any number: ") print("value of val1: ", val1) print("type of val1: ", type(val1)) # reading a value, converting to float # printing value and it's type val2 = float(input("Enter any number: ")) print("value of val2: ", val2) print("type of val2: ", type(val2)) Output Enter any number: 123.456 value of val1: 123.456 type of val1: <class 'str'> Enter any number: 789.123 value of val2: 789.123 type of val2: <class 'float'> Example to input two float numbers and find their sum and average # python code to read two float numbers # and find their addition, average num1 = float(input("Enter first number : ")) num2 = float(input("Enter second number: ")) # addition add = num1 + num2 # average avg = add/2 print("addition: ", add) print("average : ", avg) Output Enter first number : 123.456 Enter second number: 789.02 addition: 912.476 average : 456.238 The Python Programming languages use various data types to store values. The type of data a value is stored as will affect how a program can manipulate that value. For instance, you cannot run a string function on an integer or a mathematical operation on a string. Find Your Bootcamp Match
Select your interest By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. There are two number types in Python: This tutorial will discuss the basics of floating-point numbers and how you can use the Python FloatAny numerical value entered into Python will be seen as a number, so it’s not necessary to declare that a value is a number. When you insert a value without decimals, Python will interpret it as an integer (e.g., 24 or -72); values that include decimals will be interpreted as floats (e.g., 102.2 or -4.91). Integers
include positive whole numbers (e.g., 3, 7, 26), their negative counterparts (e.g., -3, -7, -26), and 0. In Python, integers are commonly referred to using the term Our code returns: Floating-point numbers, on the other hand, are real numbers. Unlike integers, floating-point numbers can store decimal values. Here’s a program that prints out a floating-point number to the console: Our code returns: Because both integers and floats are numbers (in terms of their data type), we can perform mathematical operations on them. So, if we wanted to perform floating-point arithmetic and add two floats together, for example, we could do so using the following code: math_sum = 22.5 + 17.4 print(math_sum) Our code computes 22.5 + 17.4, and then returns: Python float() MethodThe Here’s the syntax for the The Converting an Integer to a Float in PythonYou can use the float_number = float(12) print(float_number) Our code returns: Here, we used the
Converting a String to a Float in PythonIn Python, a string is a sequence of characters. Just as Here’s an example of
float_string = float("12") print(float_string) Our code returns: In this example, a numerical value You can use the float_string = float("-12") print(float_string) Our
code returns: Strings can also contain Python Float(): ExampleLet’s walk through a more detailed example to show how to use the Say that we are creating a math game for fourth-grade students who are learning about adding and subtracting decimal numbers. This game will give students a math problem to solve and will evaluate whether their responses are accurate. Because we are working with decimals, we’ll want our code to convert each number to a float. "Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!" Venus, Software Engineer at Rockbot Here’s the code we could use to create this game: answer = round(12.2 + 14.6, 1) student_input = input("What is 12.2 + 14.6?") if float(student_input) == answer: print("You're right!") else: print("The correct answer to 12.2 + 14.6 is", answer, ".") When we run our code and input the correct answer What is 12.2 + 14.6? 26.8 You're right! When we run our code and input the incorrect answer (for example, -2.4) in response to the prompt, our code returns the following: What is 12.2 + 14.6? -2.4 The correct answer to 12.2 + 14.6 is 26.8. Let’s break down our code. On the first line, we calculate the answer to a math problem (12.2 + 14.6) and use the Then, we use the On the next line, we use an If a student enters the correct answer, the message ConclusionFloating-point numbers are a crucial part of programming. They allow developers to work with decimal values. This tutorial discussed the basics of floating-point values in Python and walked through a few examples of how to use the You’re now ready to start working with floating-point numbers and the built-in Python |