Updated April 3, 2023
Introduction to Python float to int
The following article provides an outline for Python float to int. In Python, all the values we use or store as a variable will have a unique data type. It explains about the nature of the value, and depending on that, Python automatically allocates a data type for that value, and it helps in defining the kind of operation that is allowed to be done and stored in a particular structure bypass it to the compiler, and the Data types are mutable and for specific operations. Users can change the data types from float to int or string and vice-versa.
Syntax of Python float to int
Python has different data types for a different set of values, Integers deals with numbers, and float deals with both decimal and numeric characters, Boolean deals with Binary values (True or False), and there are strings that could take alphanumeric values, and python allows different data structures like List, Tuple, Dictionary & Sets for working with different problems.
In the below example, we have declared 4 different values and checked the data types of the values to give an idea of the python data types.
Code:
x=20
y=35.35
z=1+3j
a=True
print(type(x))
print(type(y))
print(type(z))
print(type(a))
Output:
How to Convert float to int in Python?
Let us see the two functions used in the float to int conversion and vice versa.
1. float() Function
This function returns a floating-point value from a string or a number.
Syntax:
float(argument)
Only one parameter can be used in this method and is optional. There are two types of argument that can be used in this function.
- Number: The number can be any floating-point number or an integer.
- String: A string must contain numbers of any kind.
This method ignores any right or left whitespaces. NaN or int can be used. We can use mathematical operators as well.
2. int() Function
The int() function converts the given value to an integer value.
Syntax:
int(argument)
If no arguments are passed in the function, it returns zero. The default base is set as 10 if no base value is given. An integer object can be constructed using a number or a string.
Examples of Python float to int
Given below are the examples of Python float to int:
Example #1
Let us see a simple example to print the integer value of a given variable.
Code:
#code to print integer value
a = 5
print(int(a))
Output:
In the above code, we have declared the value of 5 to the variable called a.
In the next line, we have used the int() function to obtain the integer value of 5 and printed the same.
Example #2
Code:
#code to print integer value
x=10/3
print(x)
print(int(x))
Output:
In the above code, we have declared the value as 10/3 to the variable x.
In the next line, we have printed the value of x, which is a floating-point number.
In the next line, we have converted the value to an integer value using the int() function and printed the integer value.
Example #3
In the next example, we will see how to add two float values and convert them into an integer value.
Code:
#type conversion from float to int
a = 5.3
b = 3.2
c = a + b
print(float(c))
#convert float to int
print(int(c))
Output:
In the above example, we have assigned two values of 5.3 and 3.2 to a and b, respectively. In the next line, we have added both a and b assigned the variable to another variable c. In the next line, we have printed the float value of c using the float() function. Finally, we have converted the float value to the integer value in the next line and printed the same.
Example #4
In this example, we will see how to subtract two float values and convert the result to an integer value.
Code:
#code to subtract float values and convert it to int value
x = 10.1
y = 5.5
z = x-y
print(float(z))
print(int(z))
Output:
In the above code, two float values of 10.1 and 5.5 are assigned to two variables, x and y respectively. Then the subtracted value is stored in the z variable. In the next line float value of z is printed, and in the last line, the integer value of z is printed.
Example #5
Consider the number 1.82e18. It is another form of a floating-point number. The letter e represents the base10. Thus, the number 1.82e18 can be expressed as 1.82 * 10^18. Let us see a simple code using this form of a floating-point number.
Code:
#code to print two floating point numbers and convert it to int value
a=1.82e8
b=2e10
print(a)
print(b)
print(float(a))
print(float(b))
print(int(a))
print(int(b))
Output:
In the above code, two floating-point numbers, 1.82e8 & 2e10, are assigned to two variables, a and b respectively. The next two lines just print the value of a and b. In the next two lines, float values of a and b are printed by the float() function. Finally, the last two lines print the integer values of a and b, respectively.
Example #6
In this example, we will see how to do a simple arithmetic operation on floating-point numbers.
Code:
#code to add two float values convert it to int value
a=5.82e18
b=2.5e12
print(float(a))
print(float(b))
#add two values and assign to c
c =a + b
print(float(c))
print(int(c))
Output:
As done in the previous example, two floating-point numbers 5.82e18 & 2.5e12, are assigned to two variables, a and b, respectively. The next two lines print just the value of a and b. In the next two lines, float values of a and b are printed by the float() function. The two float values are added and stored in the third variable c. The last line prints the integer value of c using the int() function.
Conclusion
In this article, we have seen float to int conversion using various examples. The data type conversion is considered to be very important in python since it is used in various real-time scenarios. Moreover, when working with different kinds of algorithms in the data science field, we need to convert data types.
Recommended Articles
This is a guide to Python float to int. Here we discuss the introduction, how to convert float to int in Python? and examples, respectively. You may also have a look at the following articles to learn more –