Sunday, January 3, 2021

Python Pool: How to Convert int to float in Python Implicitly and Explicitly

Hello coders!! In this article, we will learn the conversion of int to float data type in python. At first, we should understand the difference between the two.

  • int – These are the positive or negative integers without any decimal point.
  • float – These are real numbers consisting of decimal point which divides the integer and the fraction part of the real number.

What is type casting?

It may happen during programming that one feels the need to change the data type of a certain variable to some other specific data type for better performance and match the data type of given operation. Type casting is of two type:

  • Implicit Type Casting – Interpreter performs it automatically without user’s interference. The lower data type is converted to a higher data type, like integer to float. This is why it is also known as upcasting.
  • Explicit Type Casting – The user converts the data type into a specified data type as per his or her requirement.

Implicit data conversion from int to float:

Implicit data conversion is done by Python itself either during compilation or during run time.

a = 1
print('Data type of a:',type(a))
b = 1.0
print('Data type of b:',type(b))
c= a+ b
print('Sum of a and b:',c)
print('Data type of their sum:',type(c))

Output & Explanation:

int to float pythonOUTPUT

In the above code, we took two variables, one of type int(‘a’) and the other of type float(‘b’). When we added these two variables, the result variable was seen to be of data type float. So, the int to float conversion occurs implicitly here as float has higher precision than an integer.

float() Function to convert int to float in Python:

float() is an in built function available in python that is used to convert the variables from int to float.

a = 1
print('Value before conversion:',a)
print('Data type:',type(a))
b=float(a)
print('Value after conversion',b)
print('Data type:',type(b))

Output & Explanation:

float() Function to convert int to float in Python

In this example, we took an int variable ‘a’ and assigned it the value 1. We then used the float() method to convert it into a float value. We can clearly see from the output the conversion of an integer variable to float.

Alternative for Implicit Conversion of int to float [Explicitly]:

a = 1
print('Value before conversion:',a)
print('Data type:',type(a))
a = a+0.0
print('Value after conversion',a)
print('Data type:',type(a))

Output & Explanation:

Alternative for implicit conversion of int to float:

As we can see, in the given code, we have indirectly used the explicit type conversion to convert variable ‘a’ into float by adding a decimal of 0.0. As float has higher precision, the value of the variable is converted into a float data type.

Must Read:

Conclusion:

In this article, we learned about type casting and saw how to explicitly and implicitly type cast int to float in Python programming.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

The post How to Convert int to float in Python Implicitly and Explicitly appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...