Numbers in Python

In this tutorial, I am going to discuss about **Numeric Datatypes in python**. There are different numeric datatypes in python like integer, floating-point, complex numbers. We will discuss how to represent binary, octal and hexa-decimal numbers in python and inter-conversion between different numeric datatypes.

## Numeric DataTypes in Python

As we have discussed in the DataTypes in python tutorial, we can represent and use integer, floating-point and complex numbers in python. Python language also let’s us convert between these datatypes,read more about type conversion in python. Since python also supports dynamic typing, we don’t need to specify the data type for a variable.

### Int, Float and Complex Datatypes

Let’s have a look at integer, floating-point and complex datatypes one by one.

#### Integer Numbers

Python can hold signed integer values in a variable. An integer variable in python can hold value of any length, only limitation is memory availability.

For example :

```
var1 = 974
var2 = -629
var3 = 9999999999999999999999999999999999999
print("var1 = ",var1)
print("var2 = ",var2)
print("var3 = ",var3)
```

```
var1 = 974
var2 = -629
var3 = 9999999999999999999999999999999999999
```

#### Floating-point Numbers

Python also supports signed floating-point real numbers i.e. numbers with decimal-point. In python, int and float numbers differ by presence or absence of decimal-point`'.'`

. Accuracy of a floating-point value in python is only up to 15 decimal places after that python rounds off the number.

For example :

```
val1 = -355.9746
val2 = 367.3354
val3 = 854.766431468964576772
print("val1 = ",val1)
print("val2 = ",val2)
print("val3 = ",val3)
```

```
val1 = -355.9746
val2 = 367.3354
val3 = 854.7664314689646
```

In the above example, observe that variable val3 gets rounded-off after 15 decimal places.

In python, the division operation which can be performed on integer or floating-point variables always results in a floating-point value.

```
print(8/4, type(8/4))
print(5.5/1.1, type(5.5/1.1))
```

```
2.0 < class 'float' >
5.0 < class 'float' >
```

In the above example, we have used `type( )`

function which we have seen while looking at Python Datatypes. This function takes an object as an input and gives us the type of that object as an output.

#### Complex Numbers

In python we can also represent complex numbers made up of real and imaginary parts. It is represented as *x+jy* where *x* is real part and *y* is an imaginary part.

```
var = 3+7j
print(var, type(var))
```

`(3+7j) < class 'complex' >`

In the above example, we can see that 3 is the real part and 7j is the imaginary part of the complex number.

Python language has a special class called *complex* whose objects are created for a complex number like *int* for integer and and *float* for floating-point number.

The imaginary part of the complex number can only be represented using *j* but we can’t use the letter *i* like we use on paper.

```
comp1 = 3+5i
print(comp1)
```

```
File "< ipython-input-18-5353732825c4 >", line 1
comp1 = 3+5i
^
SyntaxError: invalid syntax
```

Note that it is compulsory to provide a coefficient to the imaginary part otherwise an error will be shown.

`comp2 = 4+j`

```
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
< ipython-input-19-158abf8e48e2 > in < module >
----> 1 comp2 = 4+j
NameError: name 'j' is not defined
```

In the above case, 1 should be written as coefficient of the imaginary part as shown below.

`comp3 = 4+1j`

**Operations on Complex Number**

We can also perform the basic operations like addition, subtraction and multiplication on complex numbers just like on integer or floating-point numbers.

For example :

```
a = 3+5j
b = 5+7j
print("a = ",a)
print("b = ",b)
print("a + b = ",a+b)
print("2 * b = ", 2 * b)
print("a - b = ",a - b)
print("a * b = ", a*b)
```

```
a = (3+5j)
b = (5+7j)
a + b = (8+12j)
2 * b = (10+14j)
a - b = (-2-2j)
a * b = (-20+46j)
```

### Representing Binary, Octal and Hexa-decimal in Python

In python, we can also represent the binary, octal and hexadecimal numbers. This can be dome using correct prefixes while declaring the number variable.

The prefixes to be used are shown below :

Number System | Prefixes |
---|---|

Binary | 0b or 0B |

Octal | 0o or 0O |

Hexadecimal | 0x or 0X |

**1. Binary Numbers**

In python, we can represent binary numbers using *0b* or *0B* prefix. When we print a binary number, python automatically converts it and gives us the output in decimal.

Let’s look at an example :

`print(0b111)`

`7`

**2. Octal Numbers**

Octal numbers can be represented in python using *0o* or *0O* as a prefix. Just like binary, when we try to print an octal number, python converts it and gives output in decimal.

For example :

`print(0o11)`

`9`

**3. Hexadecimal Numbers**

Hexadecimal numbers can be represented in python using *0x* or *0X* as a prefix. Similar to binary and octal, when we try to print a hexadecimal number, python converts it and gives output in decimal.

For example :

```
print(0x33)
print(0xEA)
```

```
51
234
```

**NOTE : **

- The binary number system only uses 0 or 1, so if we use any other number it will cause an error.
- Octal numbers contain numbers from 0 to 7 only if we use 8 or 9 while representing an octal number then it results in error.
- Similarly, hexadecimal number system uses 0-9 and A-F for representation, if we use any other alphabets then it will give error.

Look at the below examples to understand better :

`print(0b102)`

```
File "< ipython-input-59-467decaea841 >", line 1
print(0b102)
^
SyntaxError: invalid syntax
```

`print(0o679)`

```
File "< ipython-input-60-5ebf8ec6647f >", line 1
print(0o679)
^
SyntaxError: invalid syntax
```

`print(0x74CU)`

```
File "< ipython-input-62-d891996fd014 >", line 1
print(0x74CU)
^
SyntaxError: invalid syntax
```

## Conversion between Datatypes in Python

Python provides us with different functions which are used for inter-conversion among the numeric datatypes integer, floating-point, complex numbers, binary, octal and hexadecimal numbers.

**1.int( )**

The `int()`

function is used to convert other numeric datatypes to integer type. It can also be used to convert other types to integer but we are focusing on numeric datatypes in this tutorial.

Let’s have a look at some examples :

```
a = 3.84
bin1 = 0b110
oct1 = 0o24
hex1 = 0x7A
comp = 3+7j
print("Float to int - ",int(a))
print("Binary to int - ",int(bin1))
print("Octal to int - ",int(oct1))
print("Hexadecimal to int - ", int(hex1))
print(int(comp1))
```

```
Float to int - 3
Binary to int - 6
Octal to int - 20
Hexadecimal to int - 122
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-66-a08372bf9b14 > in < module >
8 print("Octal to int - ",int(oct1))
9 print("Hexadecimal to int - ", int(hex1))
---> 10 print(int(comp1))
TypeError: can't convert complex to int
```

Observe that :

- Conversion of floating to int only truncates the decimal part and does not round off the number to nearest integer.
- Complex numbers cannot be converted into integer.

**2. float( )**

The `float()`

function is used to convert other numeric datatypes into floating-point type.

Following are some examples :

```
a = 35
bin1 = 0b110
oct1 = 0o24
hex1 = 0x7A
comp = 3+7j
print("Int to float - ",float(a))
print("Binary to float - ",float(bin1))
print("Octal to float - ",float(oct1))
print("Hexadecimal to float - ", float(hex1))
print(float(comp1))
```

```
Int to float - 35.0
Binary to float - 6.0
Octal to float - 20.0
Hexadecimal to float - 122.0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-67-b4e9bd4f9476 > in < module >
8 print("Octal to float - ",float(oct1))
9 print("Hexadecimal to float - ", float(hex1))
---> 10 print(float(comp1))
TypeError: can't convert complex to float
```

Like `int()`

function,`float()`

function also cannot convert complex number into floating-point number.

**3. complex( )**

The `complex()`

function is used to convert numeric datatype into complex number.

Following are some examples :

```
a = 3.84
b = 5
bin1 = 0b110
oct1 = 0o24
hex1 = 0x7A
print("Int to complex - ", complex(b))
print("Float to complex - ",complex(a))
print("Binary to complex - ",complex(bin1))
print("Octal to complex - ",complex(oct1))
print("Hexadecimal to complex - ", complex(hex1))
```

```
Int to complex - (5+0j)
Float to complex - (3.84+0j)
Binary to complex - (6+0j)
Octal to complex - (20+0j)
Hexadecimal to complex - (122+0j)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-70-62e0ff6b0fa6 > in < module >
9 print("Octal to complex - ",complex(oct1))
10 print("Hexadecimal to complex - ", complex(hex1))
---> 11 print(bin(comp1))
TypeError: 'complex' object cannot be interpreted as an integer
```

**4. bin( )**

The `bin()`

function is used to convert numeric datatype into binary number. It is also used if we want to print a binary number in its original representation as `print()`

function automatically converts binary to decimal.

Let’s see some examples :

```
a = 3.84
b = 5
bin1 = 0b110
oct1 = 0o24
hex1 = 0x7A
comp1 = 3+4j
print("Int to binary - ", bin(b))
print("Binary representation - ",bin(bin1))
print("Octal to binary - ",bin(oct1))
print("Hexadecimal to binary - ", bin(hex1))
```

```
Int to binary - 0b101
Binary representation - 0b110
Octal to binary - 0b10100
Hexadecimal to binary - 0b1111010
```

The `bin()`

function cannot convert floating-point number or complex number into binary. If we try to do so it throws error as shown in the below examples.

`print(bin(a))`

```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-75-6a7a0da7dc74 > in < module >
----> 1 print(bin(a))
TypeError: 'float' object cannot be interpreted as an integer
```

`print(bin(comp1))`

```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-76-7e7b78b52500 > in < module >
----> 1 print(bin(comp1))
TypeError: 'complex' object cannot be interpreted as an integer
```

**5. oct( )**

the `oct()`

function is used to convert other numeric datatypes into octal number. It is also used if we want to print a octal number in its original representation as `print()`

function automatically converts octal to decimal.

```
b = 5
bin1 = 0b1101
oct1 = 0o24
hex1 = 0x7A
print("Int to octal - ", oct(b))
print("Binary to octal - ",oct(bin1))
print("Octal representation - ",oct(oct1))
print("Hexadecimal to octal - ", oct(hex1))
```

```
Int to octal - 0o5
Binary to octal - 0o15
Octal representation - 0o24
Hexadecimal to octal - 0o172
```

Like `bin()`

function, `oct()`

function also does not convert floating-point or complex numbers into octal number.

**6. hex( )**

The `hex()`

function converts other numeric datatypes into hexadecimal number. It is also used if we want to print a hexadecimal number in its original representation as `print()`

function automatically converts hexadecimal to decimal.

```
b = 5
bin1 = 0b1101
oct1 = 0o24
hex1 = 0x7A
print("Int to hexadecimal - ", hex(b))
print("Binary to hexadecimal - ",hex(bin1))
print("Octal to hexadecimal - ",hex(oct1))
print("Hexadecimal representation - ", hex(hex1))
```

```
Int to hexadecimal - 0x5
Binary to hexadecimal - 0xd
Octal to hexadecimal - 0x14
Hexadecimal representation - 0x7a
```

Like `bin()`

and `oct()`

, `hex()`

function also does not convert floating-point or complex numbers into hexadecimal number.

## Conclusion

In this tutorial, we have discussed about different **numeric datatype representations** available in python which are : integer, floating-point, complex numbers, binary, octal and hexadecimal representation. We have also discussed about inter-conversion between these different datatypes in python.

If you have any questions please comment below also share your views and suggestions in the comment box.