<jupyter_start><jupyter_text>Introduction Python provided three kinds of number system:1. Decimal (base 10), no prefix2. Binary (base 2), prefix: 0b3. Octal (base 8), prefix: 0o4. Hexadecimal (base 16), 0~9, A, B, C, D, E, F, prefix 0x<jupyter_code># Decimal
print('Decimal:', 60)

print('Binary:', 0b111100, type(0b111100))

print('Octmal:', 0o74, type(0o74))

print('Haxadecimal:', 0x3c, type(0x3c))<jupyter_output>Decimal: 60
Binary: 60 <class 'int'>
Octmal: 60 <class 'int'>
Haxadecimal: 60 <class 'int'><jupyter_text>Convert  Decimal to Binary, Octmal and HaxadecimalIn this program, we have used built-in functions **bin(), oct() and hex()** to convert the given decimal number into respective number systems.These functions take an **integer (in decimal)** and return a **string**.<jupyter_code># Python program to convert decimal into other number systems
dec = 344

print("The decimal value of", dec, "is:")
print(bin(dec), "in binary and its type", type(bin(dec)))
print(oct(dec), "in octal and its type", type(oct(dec)))
print(hex(dec), "in hexadecimal and its type", type(hex(dec)))<jupyter_output>The decimal value of 344 is:
0b101011000 in binary and its type <class 'str'>
0o530 in octal and its type <class 'str'>
0x158 in hexadecimal and its type <class 'str'><jupyter_text>Binary, Octmal and Haxadecimal to Decimal1. We can print a value, and there is no need to convert it to decimal. Since print function prints the value in decimal format. 2. We still can use **int()** method to convert them (string format) into decimal by defining base of the number system<jupyter_code>dec = 156
print(f"Binary to Decimal: {bin(dec)} -> {int(bin(dec), 2)}")
print(f"Octal to Decimal: {oct(dec)} -> {int(oct(dec), 8)}")
print(f"Haxadecimal to Decimal: {hex(dec)} -> {int(hex(dec), 16)}")
def binary_to_decimal(binary_num: str) -> int:
    if binary_num[:2] not in ['0b', '0B']:
        return None
    index = len(binary_num) - 3
    reg = 0
    for num in binary_num[2:]:
        reg += int(num) * (2 ** index)
        index -= 1
    return reg
binary_to_decimal(bin(dec))
def to_decimal(binary_num: str, base=2) -> int:
    index = len(binary_num) - 3
    reg = 0
    for num in binary_num[2:]:
        reg += int(num) * (base ** index)
        index -= 1
    return reg
to_decimal(bin(dec))
to_decimal(oct(dec), 8)
def hex_to_decimal(hex_num: str) -> int:
    index = len(hex_num) - 3
    nums = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 
            'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15}
    reg = 0
    for num in hex_num[2:]:
        reg +=  nums[num] * (16 ** index)
        index -= 1
    return reg
hex_to_decimal(hex(dec))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15712187958883994, 'fraction_numerical': 0.04919236417033774, 'mean_word_length': 4.170777988614801, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '65237', 'license': 'MIT', 'n_tokens_mistral': 1091, 'n_tokens_neox': 994, 'n_words': 343, 'path': 'Python/Number System.ipynb', 'repo_name': 'bingrao/notebook', 'size': '2724'}