what are python numbers briefly describe each number type in python and give examples


what are python numbers

In Python, there are several types of numbers:

int (integer): a whole number, such as 42 or -5.

float (floating point): a decimal number, such as 3.14 or -0.01.

complex: a complex number, such as 3+4j or 2-5j.

Examples:

a = 4 # int

b = 3.14 # float

c = 4+3j # complex

 

Python also has a bool type which can only have two values True or False and it's used to represent truth values.

Additionally, Python has a Decimal and Fraction classes to work with decimal numbers and fractions respectively. These classes are present in the decimal and fractions modules respectively and are generally used for more precise computations.

Examples:

from decimal import Decimal

d = Decimal(3.14)

from fractions import Fraction

e = Fraction(3,4)

Comments