Decimal: Decimal is that number system in which base is 10. (0 to 9)
Binary: In binary system the base is 2. (0 and 1 only)
Octal: The base for octal system is 8. (0 to 7)
Hexadecimal: Hexadecimal is that format in which base is 16. (0 to 9, A to F)
By default in Python decimal number is supported.
for binary we can write:
x=0b101 or
x=0B101
for octal
x=0O341
for hexadecimal
x=0x4A
Can you guess the output of the following script??
x=45
print(x) #45
x=0b101
print(x) #5
Both print statement will print the decimal value first one is 45 and another one is 5, then how can we able to print binary values.
The script can be as
x=0b101
print(bin(x)) #0b101
0b can be removed by using slicing which we study further.
Similarly for hexadecimal and octal we can write respectively as:
hex(x)
oct(x)
Slicing:
x=8
print(bin(x)[2::])
Slicing operator works on those data types where indexing is supported.
example:
x="mayank"
print(x[0:2:]) #ma
Here last index is not included.
Binary: In binary system the base is 2. (0 and 1 only)
Octal: The base for octal system is 8. (0 to 7)
Hexadecimal: Hexadecimal is that format in which base is 16. (0 to 9, A to F)
By default in Python decimal number is supported.
for binary we can write:
x=0b101 or
x=0B101
for octal
x=0O341
for hexadecimal
x=0x4A
Can you guess the output of the following script??
x=45
print(x) #45
x=0b101
print(x) #5
Both print statement will print the decimal value first one is 45 and another one is 5, then how can we able to print binary values.
The script can be as
x=0b101
print(bin(x)) #0b101
0b can be removed by using slicing which we study further.
Similarly for hexadecimal and octal we can write respectively as:
hex(x)
oct(x)
Slicing:
x=8
print(bin(x)[2::])
Slicing operator works on those data types where indexing is supported.
example:
x="mayank"
print(x[0:2:]) #ma
Here last index is not included.
Be First to Post Comment !
Post a Comment