A1-NOTES (Number Data Type)
A1-NOTES (Number Data Type)
Example:
Integer:5
Float :5.0
Complex numbers are writteren in the form ,x+yj,where x is real part and y is imaginary
part.
Important:
type() function is used to know which class a variable or a value belongs to and
isinstance() function to check if it belongs to a particular class.
Example:
a=5
print(type(a))
print(type(5.0))
c = 5 + 3j
print(c + 3)
print(isinstance(c, complex))
Python Decimal
We know that the sum of 1.1 and 2.2 is 3.3.but python seems to disagree.
Example:
Example:
print(1/3)(Ans. 0.33333333... )
Example:
print(1.1+2.2)
To overcome this issue, we can use the decimal module that comes with Python.
While floating-point numbers have precision up to 15 decimal places, the decimal
module has user-settable precision.
Example:
import decimal
print(0.1)
print(decimal.Decimal(0.1))
Example:
We know 25.50 kg is more accurate than 25.5 kg as it has two significant decimal places
compared to one.
print(D('1.1') + D('2.2'))
print(D('1.2') * D('2.50'))
Python Fractions
Python provides operations involving fractional numbers through its fractions module.
A fraction has a numerator and a denominator, both of which are integers.
This module has support for rational number arithmetic.
Example:
import fractions
print(fractions.Fraction(1.5))
print(fractions.Fraction(5))
print(fractions.Fraction(1,3))
Python Mathematics
Python offers modules like math and random to carry out different mathematics like
trigonometry, logarithms, probability and statistics.
Example:
import math
print(math.pi)
print(math.cos(math.pi))
print(math.exp(10))
print(math.log10(1000))
print(math.sinh(1))
print(math.factorial(6))
random
Example:
import random
print(random.randrange(10, 20))
print(random.choice(x))
random.shuffle(x)
print(x)
print(random.random())
print(random.uniform(1,20))
print(random.sample(x,k=2))