Number System and Conversions:
Number Systems:
- Binary Number System ( 0-1 )
- Octal Number System ( 0-7 )
- Decimal Number System ( 0-9 )
- Hexa-Decimal Number System ( 0-9,A-F )
Interpreter Takes input as any Number system and produce the output as only Decimal Number System.
We can give the any number system number as a input to the variables in python, By giving the special characters at the beginning of the number.
Special Characters are:
- 0b for Binary Number System. example: x= 0b1010
- 0o for Octal Number System. example: y=0o257
- no-specify for Decimal Number System. example : z= 1569
- 0x for Hexa-Decimal Number System. example : a= 0x28CE
NOTE:
- Here 0 means zero, not a character
Binary Number System:
- The base or radix of binary number system is 2.
- The possible digits that are used in binary number system are 0 and 1
- We can give the input values in the form of binary numbers format by giving ob as prefix.
Example 1:
>>> x=0b1010 -----> Binary value
>>> print(x)
10 -----> Decimal value
Explanation:
Converting binary number to Decimal number
1010 =1*2*2*2 +
0*2*2 + 1*2 + 0*1
=8+0+2+0
=10
Example 2:
>>> x= 0b1020
SyntaxError: invalid syntax
Octal Number System:
- The base or radix of octal number system is 8.
- The possible digits that are used in octal number system are 0 to 7.
- We can give the octal number system number as input by giving 0o beginning of the number.
Example 1:
>>> y= 0o123 -----> Octal number
>>> print(y)
83 ------> Decimal number
Explanation:
123 = 1*8*8
+ 2*8 + 3*1
= 64+16+3
= 83
Decimal Number System:
- The base or radix of decimal number system is 10.
- The possible digits that are used in decimal number system are 0 to 9.
- The default number system followed by the python is decimal number system.
Example 1:
>>> z=1234 ----> Decimal number
>>> print(z)
1234 -----> Decimal number
Explanation:
1234 = 1*10*10*10 +
2*10*10 + 3*10
+ 4*1
= 1000+ 200 +
30 + 4
= 1234
Hexa-Decimal Number System:
- The base or radix of hexa-decimal number system is 16.
- The possible digits that are used in hexa-decimal number system are 0 to 9 and A to F.
- We can give the hexa-decimal number system number as input by giving 0X as prefix.
Example 1:
>>> a= 0x25 ------> Hexa-Decimal number
>>> print(a)
37 ------> Decimal number
Explanation:
25 = 2*16 + 5*1
= 32+5
= 37
Note:
- We can give any number system number as input but we will get the output in the form of decimal number system format by default.
Number System Conversions:
By using pre-defined
functions we can display the any number in the form of any number system
format.
Pre-defined functions are:
- bin( ) -----> for binary Number system.
- oct( ) ------> for Octal Number System.
- hex( ) -----> for Hexa-Decimal Number System.
Example 1:
Binary number to octal, decimal and hexa-decimal number.
x=0b1010 # input is binary number
print(oct(x)) #octal number
print(x) # decimal number
print(hex(x)) # hexa decimal number
Output:
0o12
10
0xa
Example 2:
Octal nuber to Binary,decimal and hexa-decimal number.
x=0o123 # input is octal number
print(bin(x)) # binary number
print(x) # decimal number
print(hex(x)) # hexa decimal number
Output:
0b1010011
83
0x53
Example 3:
Decimal number to Binary, Octal and Hexa-decimal number.
x=157 # input is decimal number.
print(bin(x)) #binary number.
print(oct(x)) # octal number.
print(hex(x)) # hexa decimal number.
Output:
0b10011101
0o235
0x9d
Example 4:
Hexa-Decimal number to Binary,Octal and Decimal number.
x=0x2b5c # input is hexa-decimal number.
print(bin(x)) #binary number.
print(oct(x)) # octal number.
print(x) # decimal number.
Output:
0b10101101011100
0o25534
11100
Note:
- in python # represent Comment.
Note:
- Decimal point numerical values (Float Values) can be represented in the form of decimal number system only.
No comments:
Post a Comment