Integers and floats are two different types of numerical data. Integers are whole numbers without any decimal points. For example:
age = 15 print(age) # output: 15
15
In this example, `age` is a variable that holds the value `15`, which is an integer. Integers can be positive or negative. Floats, on the other hand, are numbers that have decimal points. For example:
pi = 3.14 print(pi) # output: 3.14
3.14
In this example, `pi` is a variable that holds the value `3.14`, which is a float. You can perform various mathematical operations on integers and floats, such as addition, subtraction, multiplication, and division. For example:
x = 5 y = 2 sum = x + y print(sum) # output: 7 product = x * y print(product) # output: 10 quotient = x / y print(quotient) # output: 2.5
7 10 2.5
In this code, we have two variables `x` and `y` that hold the values `5` and `2` respectively. We can add them together (`x + y`), multiply them (`x * y`), or divide them (`x / y`). It's important to note that when you perform operations between an integer and a float, the result will be a float. For example:
result = x / 2
In this code, `x` is an integer and `2` is an integer, but the result of the division will be a float (`2.5`). You can also convert between integers and floats using the `int()` and `float()` [functions](/tutorials/functions). For example:
x = 5 y = float(x) # Convert x to a float print(y) # output: 5.0 z = int(y) # Convert y back to an integer print(x) # output: 5
5.0 5
In this code, we convert the integer `5` to a float using `float(x)`, and then convert it back to an integer using `int(y)`. Integers and floats are commonly used in mathematical calculations, storing measurements, and representing decimal values in Python.