The Python float()
method converts a number stored in a string or integer into a floating point number, or a number with a decimal point. Python floats are useful for any function that requires precision, like scientific notation.
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.
There are two number types in Python: floating-point numbers (floats)
and integers.
While floats can contain decimals, integers cannot.
This tutorial will discuss the basics of floating-point numbers and how you can use the float()
method to convert strings and integers to floats in Python. We’ll also walk through programming examples that employ the float()
method to convert integers and strings to floating-point numbers.
Python Float
Any 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 int
. Here’s an example of a program that prints out an integer to the console:
print(4)
Our code returns: 4
.
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:
print(3.14)
Our code returns: 3.14
.
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: 39.9
.
Python float() Method
The float()
method is a built-in Python function that is used to convert an integer or a string to a floating-point value.
Here’s the syntax for the float()
method in Python:
float(value)
The float()
method takes in one parameter: the value you want to convert to a float. This parameter is optional and its default value is 0.0.
Converting an Integer to a Float in Python
You can use the float()
method in Python to convert an integer to a floating-point number. Here’s an example:
float_number = float(12) print(float_number)
Our code returns: 12.0
.
Here, we used the float()
method to convert an integer (12)
into a floating-point number (12.0)
. The .0
at the end tells us that our number has successfully been converted to a floating-point value.
Converting a String to a Float in Python
In Python, a string is a sequence of characters. Just as float()
can convert an integer to a floating-point number, it can convert a string to a floating-point number as well. To do so, the string must be numerical.
Here’s an example of float()
being used to convert a string to a floating-point number in Python:
float_string = float("12") print(float_string)
Our code returns: 12.0
.
In this example, a numerical value (12)
was stored as a string in our program. The float()
method converted this value into a floating-point number.
You can use the +
and -
signs to denote whether you want your string to be converted to a positive float or a negative float. Here’s an example of this in action:
float_string = float("-12") print(float_string)
Our code returns: -12.0
. Notice that our data is now stored as a float, rather than a string. This is evident because our number is not in quotation marks, and ends with .0
.
Strings can also contain NaN
, infinity
, or inf
. These combinations of letters are used to represent invalid numbers (NaN
) and infinity values (infinity
or inf
).
Python Float(): Example
Let’s walk through a more detailed example to show how to use the float()
method in Python.
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 (26.8)
in response to the prompt, our code returns the following:
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 round()
method to make sure we have a value that is rounded to one decimal place.
Then, we use the input()
method to ask a student to input an answer to the math problem. The parameter of the input method here is the question our program will ask students (What is 12.2 + 14.6?
)
On the next line, we use an if
statement to compare whether the student’s answer is equal to the answer our program calculated. Importantly, we use the float()
method to convert the variable student_input
to a float. We must do this because the input()
method returns data in a string format, and we cannot compare a string to a float. So, in order for our program to work, we needed to convert student_input
to a float.
If a student enters the correct answer, the message You’re right!
is printed to the console; otherwise, the console prints the correct answer, which follows the text we set to precede the answer (The correct answer to 12.2 + 14.6 is
).
Conclusion
Floating-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 float()
method to convert integers and strings to floating-point numbers.
You’re now ready to start working with floating-point numbers and the built-in Python float()
method like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.