**What is Float in Python?**

In the world of programming, data types are crucial. They define the kind of value a variable can hold. In Python, one such data type is the “float”.

A float, short for floating point number, represents real numbers and is used to hold numbers that are not whole, i.e., they have a fraction part

**For instance, 3.14, 0.01, -20.23 are all examples of float values.**

Floats in Python are very versatile.

They can handle decimal numbers, but they can also represent very large or very small numbers using scientific notation. For example, the speed of light, which is approximately 299792458 meters per second, can be represented as 2.99792458e8 in Python.

**In Python, the data type is set when you assign a value to a variable.** If you assign a decimal number to a variable, Python will automatically interpret it as a float.

For example, if you write

, Python understands that **x = 20.5**

is a float.**x**

**float() function**

**If you want to explicitly specify the data type, you can use the float() function.**

This is particularly useful when you want to convert an integer or a string into a float. For example, `x = float(20)`

will convert the integer 20 into a float 20.0, and `x = float("20.5")`

will convert the string “20.5” into a float 20.5.

**Difference from Integer**

**While both integers and floats are used to represent numbers, they are used for different kinds of numbers.**

Integers are used for whole numbers, both positive and negative, without any decimal points.

Floats, on the other hand, are used for numbers that have decimal points. They can represent both very small fractions and very large numbers using decimal notation.

See Also: What is Integer in Python? | Easy Guide & Examples

**Example Codes of Float**

Let’s look at some examples of how to use floats in Python:

**Assigning a float value to a variable**

```
x = 3.14
print(x) # Outputs: 3.14
```

In this example, we assign the float value 3.14 to the variable `x`

. When we print `x`

, Python outputs 3.14.

**Converting an integer to a float**

```
x = float(10)
print(x) # Outputs: 10.0
```

Here, we use the float() function to convert the integer 10 into a float. When we print `x`

, Python outputs 10.0.

**Converting a string to a float**

```
x = float("3.14")
print(x) # Outputs: 3.14
```

In this example, we convert the string “3.14” into a float using the float() function. When we print `x`

, Python outputs 3.14.

**Performing arithmetic operations with floats**

```
x = 10.5
y = 2.5
print(x + y) # Outputs: 13.0
print(x - y) # Outputs: 8.0
print(x * y) # Outputs: 26.25
print(x / y) # Outputs: 4.2
```

In this example, we perform various arithmetic operations with the float values `x`

and `y`

. Python correctly handles the decimal points and outputs the expected results.

**Using floats in scientific notation**

```
speed_of_light = 2.99792458e8
print(speed_of_light) # Outputs: 299792458.0
```

In this example, we represent the speed of light using a float in scientific notation. When we print `speed_of_light`

, Python outputs 299792458.0.