The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols 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)
- Hexadecimal Number System (base or radix = 16)
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
Python
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
Python
o = 0b1001
print("Binary to Octal", o, ":", oct(o))
Output:
Binary to Octal 9 : 0o11
3) Binary to Hexadecimal
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
Python
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
Python
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
Python
b = "342"
print("Octal to Decimal",b,":",int(b,8))
Output:
Octal to Decimal 342 : 226
3) Octal to Hexadecimal:
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
Python
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
Python
# 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
Python
# Decimal to Octal
a = 10
print("Decimal to Octal",a,":",oct(a))
Output:
Decimal to Octal 10 : 0o12
3) Decimal to Hexadecimal
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
Python
a = 1254
print("Decimal to Hexadecimal",a,":",hex(1254))
Output:
Decimal to Hexadecimal 1254 : 0x4e6
Hexadecimal Number System
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.
1) Hexadecimal to Binary:
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
Python
a = 0xFACE
print("Hexadecimal to Binary", a, ":", bin(a))
Output:
Hexadecimal to Binary 64206 : 0b1111101011001110
2) Hexadecimal to Octal:
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
Python
a = 0x94AB
print("Hexadecimal to Octal", a, ":", oct(a))
Output:
Hexadecimal to Octal 38059 : 0o112253
3) Hexadecimal to Decimal:
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
Python
b = 0x94AB
print("Hexadecimal to Decimal",b,":",int ("0x94AB",16))
Output:
Hexadecimal to Decimal 0x94AB : 38059
Similar Reads
Subtract Two Numbers in Python
Subtracting two numbers in Python can be done in multiple ways. The most common methods include using the minus operator (-), defining a function, using a lambda function, or applying bitwise operations. Each method has its use cases depending on simplicity, readability and performance needs. Using
2 min read
Python - Retain Numbers in String
Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones. Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to fo
2 min read
Insert a number in string - Python
We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
2 min read
Sum the Digits of a Given Number - Python
The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di
2 min read
Minimum of two numbers in Python
In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in min() function. [GFGTABS] Python a = 7 b = 3 print(min(a, b)) [/GFGTABS]Output3 Explanation: min() function compares the two numbe
2 min read
How to Add Two Numbers in Python
The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12. Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perfor
5 min read
Python Check If String is Number
In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number. Example: Using isdigit() Method [GFGTABS] Python # Python code to check if strin
6 min read
Frequency of Numbers in String - Python
We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6). Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match s
3 min read
Sum of number digits in List in Python
Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Pythonâs built-in functions like sum(), map(), and list comprehensions. For exa
2 min read
Find Square Root Of Given Number - Python
Given an integer X, find its square root. If X is not a perfect square, then return floor(âx). For example, if X = 11, the output should be 3, as it is the largest integer less than or equal to the square root of 11. Using built-in functionsWe can also find the floor of the square root using Pythonâ
3 min read