Python Program for Logic Building | How to Improve Coding Logic in Python

Akansha Yadav shows how to convert an integer to a Decimal object in Python using the decimal library. She imports the library, creates an integer variable, calls decimal.Decimal() to convert it, and uses type() to verify the data type changed from int to Decimal while the value stays the same.
Chapters
Let's see how to convert any integer into decimal. Now when we talk about conversion from one data type to another data type, it is efficient because in Python we have libraries. So we are going to use the library called decimal where you have to write down d in lowercase, and this is how you are gonna import it. Next I am going to create one variable in which I will be storing some value. You can even take input in this variable. Then I am printing decimal dot decimal which is gonna be D capital, decimal bracket, and then we will give the value which is in the variable integer value. Now we can also print the data type to cross-verify whether the conversion that we have tried to do is completed or not. So for that, I am going to use the type function and I am going to execute my entire code. And the output for this is the decimal dot decimal and the value is 42 still. It means the value remains the same, only the data type gets change. And we are doing it with the help of decimal.decimal. It means in the decimal library we have a decimal function which is used for this purpose. Now let us go through this entire code for the better understanding. First, I am using this library. What is decimal? Decimal is the library in Python which we use. Then we have integer value, which is a variable. A variable is a container in which we store the values, in which I am trying to store 42 value. Next, I have written a print statement with decimal, where d is lowercase. When I'm writing first time decimal, it is the name of the library. The next, second time, after dot, when I am writing decimal, it is D capital, which is a function in the decimal library to make a conversion of any data type to decimal. Then I am passing a parameter. What is a parameter over here? Parameter is a useful thing for the function. Which makes the execution happen. So integer value is the parameter. Then I am using a type function, and type function helps us do the cross-verify things, like we can check whether it is executed or not.










