 GeeksforGeeks App
Open App Browser
Continue

# Number System in Python

The arithmetic value which is used for representing the quantity and used in making calculations are defined as NUMBERS. The writing system for denoting numbers using digits or symbols in a logical manner is defined as a Number system. Number System is a system that defines numbers in different ways to represent numbers in different forms.

Types of Number System

The number system in python is represented using the following four systems:

•  Binary Number System (base or radix =2)
•  Octal Number System (base or radix  = 8)
•  Decimal Number System (base or radix  = 10)

## Binary Number System

A number system with base or radix 2 is known as a binary number system. Only 0 and 1 are used to represent numbers in this system.

1) Binary to Decimal

For Binary to Decimal conversion, the binary number uses weights assigned to each bit position. like

```a = 1 0 0 1
a = 1*23 +0*22+0*21+1*20
a= (8+0+0+1) = 9```

## Python3

 `b ``=` `"1001"``print``(``"Binary to Decimal"``, b, ``":"``, ``int``(b, ``2``))`

Output:

`Binary to Decimal 1001 : 9`

2) Binary to Octal

First convert binary number to decimal number by assigning weight to each binary bit.

```a = 1 0 0 1
a =1*23 + 0*22+ 0*21 +1*20
a= (8+0+0+1) = 9 ```

Now, 9 can be converted into octal by dividing it by 8 until we get the remainder between (0-7).

`(1001)2 = (9)10 = (11)8`

## Python3

 `o ``=` `0b1001``print``(``"Binary to Octal"``, o, ``":"``, ``oct``(o))`

Output:

`Binary to Octal 9 : 0o11`

First convert binary number to decimal number by assigning weight to each binary bit.

```a = 100101
a =1*25+0*24+0*23+1*22+0*21+1*20
a= (32+0+0+4+0+1) = 37```

As 100101 in binary is represented as 37 in decimal we can convert 37 into Hexadecimal by dividing it by 16.

`a = (100101)2= (37)10 = (25)16`

## Python3

 `h ``=` `0b100101``print``(``"Binary to Hexadecimal"``, h, ``":"``, ``hex``(h))`

Output:

`Binary to Hexadecimal 37 : 0x25`

## Octal Number System

Octal Number System is one in which the base value is 8. It uses 8 digits i.e. 0-7 for the creation of Octal Numbers. It is also a positional system i.e weight is assigned to each position.

1) Octal to Binary:

Octal numbers are converted to binary numbers by replacing each octal digit with a three-bit binary number. In python bin( ) function is used to convert octal number to binary number. The value is written with ‘0o’ as a prefix which indicates that the value is in the octal form.

`eg.  (123)8 = (001 010 011)2 = (1010011)2  `

## Python3

 `O ``=` `0o123``print``(``"Octal to Binary"``,O,``":"``,``bin``(O))`

Output:

`Octal to Binary 0o123 : 0b1010011`

2) Octal to Decimal:

An octal number can be converted into a decimal number by assigning weight to each position. In python int( ) function is used to convert octal to decimal numbers. Two arguments are get passed, the first is a string of octal numbers, and the second is the base of the number system specified in the string.

```(342)8 =  3* 82 + 4*81 + 2*80
=  3*64 + 4*8 + 2*1
=   226
(342)8 =  (226)10          ```

## Python3

 `b ``=` `"342"``print``(``"Octal to Decimal"``,b,``":"``,``int``(b,``8``))`

Output:

`Octal to Decimal 342 : 226`

An Octal number can be converted into a hexadecimal number by converting the number into a decimal and then a decimal number to hexadecimal. In python hex( ) function is used to convert octal to hexadecimal numbers.

Let’s first convert b into a decimal number.

```b = (456)8
(456)8    = 4*82 + 5*81+ 6*80
(456)8   = (302)10 = (12E)16```

## Python3

 `h ``=` `0o456``print``(``"Octal to Hexadecimal"``, h,``":"``, ``hex``(h))`

Output:

`Octal to Hexadecimal 302 : 0x12e`

## Decimal Number System

A number system with a base value of 10 is termed a Decimal number system. and it is represented using digits between (0 to 9). Here, the place value is termed from right to left as first place value called units, second to the left as Tens, so on Hundreds, Thousands, etc.

1) Decimal to Binary.

For decimal to binary conversion, the number is divided by 2 until we get 1 or 0 as the final remainder. In python bin( ) function is used to convert decimal to binary numbers.

```Here, a = 10
(10)10 = (1010)2```

## Python3

 `# Decimal to Binary``a ``=` `10``print``(``"Decimal to Binary "``, a, ``":"``, ``bin``(a))`

Output:

`Decimal to Binary  10 : 0b1010`

2) Decimal to Octal

For Decimal to Octal conversion, the number is divided by 8 until we get a number between 0 to 7 as the final remainder. In python oct( ) function is used to convert decimal to octal numbers.

`so,  (10)10 = (12)8`

## Python3

 `# Decimal to Octal``a ``=` `10``print``(``"Decimal to Octal"``,a,``":"``,``oct``(a))`

Output:

`Decimal to Octal 10 : 0b1010`

For decimal to hexadecimal conversion, the number is divided by 16 until we get a number between 0 to 9 and (A to F)as the remainder. In python hex( ) function is used to convert decimal to hexadecimal numbers.

`so, (1254)10  =  (4E6)16`

## Python3

 `a ``=` `1254``print``(``"Decimal to Hexadecimal"``,a,``":"``,``hex``(``1254``))`

Output:

`Decimal to Hexadecimal 1254 : 0x4e6`

A number system with a base of 16 is called a hexadecimal number system. It is represented using numbers between 0 to 9 and the alphabets between A to F. As both numeric digits and alphabets are used in this system, it is also called an alphanumeric system.

The hexadecimal number is converted into a Binary number by replacing each hex digit with its binary equivalent number.

```a = FACE
F = (15)10 = (1111)2
A = (1)10   =  (0001)2
C = (12)10 = (1100)2
E =(14)10  = (1110)2
(FACE)16 = (1111000111001110)2```

## Python3

 `a ``=` `0xFACE``print``(``"Hexadecimal to Binary"``, a, ``":"``, ``bin``(a))`

Output:

`Hexadecimal to Binary 64206 : 0b1111101011001110`

The hexadecimal number is converted to an octal number by first converting that number to its equivalent binary number and then to a binary number to an octal number.

```Eg.)
(94AB)16 = (1001 0100 1010 1011)2
= 001 001 010  010 101  011
= (112253)8```

## Python3

 `a ``=` `0x94AB``print``(``"Hexadecimal to Octal"``, a, ``":"``, ``oct``(a))`

Output:

`Hexadecimal to Octal 38059 : 0o112253`

Hexadecimal number is converted into decimal number by assigning weight (16) to each hex digit.

```(94AB)16 = 9*163 + 4*162 + A*161+ B* 160
= 9* 4096 + 4*256 + 10*16 + 11*1
= (38059)10```

## Python3

 `b ``=` `0x94AB``print``(``"Hexadecimal to Decimal"``,b,``":"``,``int` `(``"0x94AB"``,``16``))`

Output:

`Hexadecimal to Decimal 0x94AB :  38059`

My Personal Notes arrow_drop_up