Different types of Numbers supported by Python

If you are interested to learn about the python datatype

Python Numbers

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them.

There are three numeric types in Python:

• `int`
• `float`
• `complex`

Variables of numeric types are created when you assign a value to them:

Example

```x = 1    # int
y = 2.8  # float
z = 1j   # complex```

To verify the type of any object in Python, use the `type()` function:

Example

```print(type(x))
print(type(y))
print(type(z))
```

Int

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

Example

Integers:

```x = 1
y = 35656222554887711
z = -3255522

print(type(x))
print(type(y))
print(type(z))
```

Float

Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

Example

Floats:

```x = 1.10
y = 1.0
z = -35.59

print(type(x))
print(type(y))
print(type(z))
```

Float can also be scientific numbers with an “e” to indicate the power of 10.

Example

Floats:

```x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))```

Complex

Complex numbers are written with a “j” as the imaginary part:

Example

Complex:

```x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))```

Number Type Conversion

Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But sometimes, you need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.

• Type int(x) to convert x to a plain integer.
• Type long(x) to convert x to a long integer.
• Type float(x) to convert x to a floating-point number.
• Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
• Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions

You can convert from one type to another with the `int()``float()`, and `complex()` methods:

Example

Convert from one type to another:

```x = 1    # int
y = 2.8  # float
z = 1j   # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))
```

Mathematical Constants

The module also defines two mathematical constants −

Type Conversion

You can convert from one type to another with the `int()``float()`, and `complex()` methods:

Example

Convert from one type to another:

```x = 1    # int
y = 2.8  # float
z = 1j   # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))
```

Writing numbers in binary, octal, and hexadecimal in Python

More often than not, programmers need to deal with numbers other than decimal. To do this, you can use appropriate prefixes.

1. Binary Numbers in Python

When you want to write a binary number, use the prefix 0b or 0B. For example, we know that the binary for 7 is 111.

`&gt;&gt;&gt; print(0b111)`

Output

7

You can also apply conversion functions on these numbers.

`&gt;&gt;&gt; int(0b10)`

Output

2

2. Octal Numbers in Python

The prefix for octal is 0o or 0O.

`&gt;&gt;&gt; print(0O10)`

Output

8

The following code causes an error. This is because the octal number system does not have the number 8. It has the numbers 0-7.

`&gt;&gt;&gt; print(0O8)`

Output

SyntaxError: invalid token

`&gt;&gt;&gt; float(0B10)`

Output

2.0

3. Hexadecimal Numbers in Python

The hexadecimal number system has numbers 0-9 and then A-F. For that, use the prefix 0x or 0X.

`&gt;&gt;&gt; print(0xFF)`

Output

255

`&gt;&gt;&gt; print(0xFE)`

Output

254

Python Decimal Module

Let’s try out adding 1.1 and 2.2 in the shell, and let’s compare it with

` 3.3.>>> (1.1+2.2)==3.3`

Output

False

Why did it return False? Let’s try printing the sum.

`>>> 1.1+2.2`

Output

3.3000000000000003

Woah, how did this happen? Well, this is duly attributed to hardware limitations, and is not a flaw of Python. Because the hardware stores decimals as binary fractions, it isn’t possible to store it very accurately. Let’s take an example.

`>>> 1/3`

Output

0.3333333333333333

Fractions Module in Python

Another module that Python provides, the fractions module lets you deal with fractions. The Fraction() function returns the value in the form of numerator and denominator.

`>>> from fractions import Fraction>>> print(Fraction(1.5))`

Output

3/2

Different types of Numbers supported by Python