Integers and floating-point numbers in Python
First of all, we have to know what is meant by the integers:
Integers are the numbers that include 0 , positive numbers and also the negative numbers.in order to store the integer values in python we can directly assign it to the variable python will dynamically stores the value and also it will allocate the space for it so that whenever we call that variable where the integer value is assigned can be retrieved and we can perform the operations on that.
Similarly, the floating point also means the same for initializing the values which are in decimal format in python we will use the float numbers.
Integers and floating points are the two basic datatypes of the python on which we can perform the operations.
We will look into the integer values in python:
Program 1:
#we will initialize the value 7 to the variable a then we will retrieve that value
A=7
print(A)
Output:
7
So here we can directly access the value which is assaigned to the variable by using the simple print statement.
Now we will some of the operations on the integer values so that we will get the clarity on how it can be modified.
Program 2:
# first we will assaign the value 2 to a
a=2
#now we will assaign the value 4 to b
b=4
# now we will add these two values
Sum=a+b
#now we will print the sum value
print(sum)
Output:
6
So, we can perform similarly all the multiple arithmetic operations with the integer variables.
We can also use this integer values in the control flow statements let’s take an example.
Now we want to know whether the person is eligible to vote or not then we will see the condition that person should cross the age of 18 so that only the person can vote so in python we will initialize the age value or else we will take the age from the input and then we will use the if else statement so that we will know whether the person is eligible to vote or not
Program 3:
age=int(input("Enter the age to check whether the person is eligible to vote or not : "))
#here we are using the integer value assigned to the variable for checking the condition.
if age>=18:
print("You are eligible to vote ")
else:
print("You are not eligible to vote ")
Output:
Enter the age to check whether the person is eligible to vote or not : 18
You are eligible to vote
What if the value is not true, we will check for once more
age=int(input("Enter the age to check whether the person is eligible to vote or not : "))
#here we are using the integer value assigned to the variable for checking the condition.
if age>=18:
print("You are eligible to vote ")
else:
print("You are not eligible to vote ")
Output:
Enter the age to check whether the person is eligible to vote or not : 7
You are not eligible to vote
So here in this aspect we are using the integer values to check whether the given condition is true or not so in the same way integers are used in many more operations.
Floating-point Numbers:
Similarly same like the integer value we can also perform the operations with the floating values also.
Floating point values are also the basic datatype of the python where we can store the decimal values and perform the operations on it.
We will look into some examples so we will understand how the floating point values will be used in python.
Program:
#Assaigning the floating-point number to the variable a with 6.72
a=6.72
#now printing the value assigned to that variable
print(a)
Output:
6.72
In this example the python had directly taken the value as float because we had assigned the float value to it, so it is the type of dynamically allocated the value as float by the python virtual machine
And also, we can convert any numeric datatype into float data type by using the type casting so that the value will be transformed into the float type.
Program:
#Assaiging the integer value to the variable a and then type casting it to the float value
a=3
#Using float method for type casting
b=float(a)
print(b)
Output:
3.0
This .0 has added to the end of the value because we have converted that respective value into the float value.
We will some examples where we will use the integer and float values so that we will get grip over them.
1.Take the input values and then perform multiplication operation on them.
Program 1:
a=int(input("Enter the first value : ") )
b=int(input("Enter the second value : ") )
print(" The multiplication of the given two numbers is: ",a*b)
Output:
Enter the first value : 2
Enter the second value : 3
The multiplication of the given two numbers is: 6
If we try to combine the integer value with the string value, we will see what happens with the same above example.
Program 2:
a=int(input("Enter the first value : ") )
b=int(input("Enter the second value : ") )
print(" The multiplication of the given two numbers is: "+a*b)
Output:
Enter the first value : 2
Enter the second value : 3
Traceback (most recent call last):
File "<string>", line 3, in <module>
TypeError: can only concatenate str (not "int") to str
So here it is showing that we cannot concatenate int value with the string value so we have to keep in mind that we should not add both the string and also the integer or the float value it results the same error so we have to separate it with, so that it will print side by side in python.
2. Performing on the Float Values
Take two float values and then perform both the values or equal or not.
# assaigning the float values to the variable a and b
# first taking the input of the value a
a=float(input("Enter the first float digit : "))
b=float(input("Enter the second float digit : "))
if a==b:
print("These two values are equal")
else:
print("Both the values are not equal")
Output:
Enter the first float digit : 3.45
Enter the second float digit : 3.45
These two values are equal
Hence, we can also perform the operations of the arithmetic with the float values also.
And one interesting topic with the float values of the python lets discuss that one.
Consider the program one after the other then we will notice something with the float values.
Sample Program 1:
# assaigning the float values to the variable a and b
a=2.3
b=3.4
print("The addition of both float values is : ",a+b)
Assumed Output:
Here according to the normal mathematics, we think that the value will be 5.7 right so but python will give some other let’s see.
Output:
The addition of both float values is : 5.699999999999999
Explanation:
The value of the output is not that we thought right so this is because of the limited precision in python.
First of all, there are infinitely many fractional numbers. For instance, you might round the result of dividing 1 by 3 to 0.33, 0.333333, or 0.333333333333333. There will always be more threes, regardless of precision. Many numbers are best-effort approximations since there are only so many bits available to store float values. Therefore, due to their limited accuracy and limited memory storage, floating-point numbers can occasionally produce unexpected results while executing arithmetic operations.
Overcoming the Lack of Precision:
A key characteristic of how floats are stored in computer memory is their restricted precision. Therefore, the problem of restricted precision when working with floats cannot be totally eliminated.
When working with floats in Python, there are a few strategies to lessen the effects of restricted precision:
Use the decimal module: The Python standard library includes the decimal module, which supports decimal floating-point arithmetic. This enables you to work with fixed-precision decimal numbers, which might be advantageous for applications that call for computations that are more accurate than what is achievable with normal floats.
Use NumPy: The NumPy module is a well-liked Python library for scientific computing that enables working with arrays of numbers. A data type called numpy.float128 that is offered by the NumPy module enables you to work with floating-point values more precisely than you can with conventional floats.
Round your floats: As was already explained, you can round a float to a specified number of decimal places by using the round function. By using fewer decimal places in your calculations, this can help mitigate the effects of low accuracy.