Number system in python
Number system in python
Number System
The antheoehe volue thod i's used fothe Quonity
and usecd in moking colrulations is cle Roed ag NUMBFRs. The
woiding sys tem fou cencoing numbers logirely using dlgis or
Synbols is clefinecl as nomber system
Number stem is a sy stem forlo cernoling numbers in direot uys
to repoesen numbers io chifeot osms
Types of Number Systern
D
Birary Number SysBern
2) OcHal Number Sys lern
3) Oerinal Number System
4) Hescaceimal Number Systern
Birony Numbe ysterm
A Number systern wrth base or rodior 2 is
binory Number system. koouon os
Binary t perimal
foz birany to decimal convers ion, the birory numbe uses
assigned to eoch bit pastion weyht
eq: bina leB a birory number be lo01, in odr to (onveot i into
cerimal
IK2+Ox2 +0x2'+ lx2° = a
= (ai0
pogrom
cdeP binary. to clecirmalCbinory):
clecimol=0
Binary o octal
Sist convest binors number to decirmal numba by ossigning
oPight o each bit.Afle that conve hat cdeojmgl nomber
between
b octol by divdiog it by $ unil we get the YemaindeY
(6-1)
eg: Led Hhe binory number be I0
l001
Birary to Hescadecinal
.: ((00101), : a05)),
Pogrom
deP bindy. to. hex Cbirany):
decimol- biray- to. derimal Cbinay)
hex. cocles- O12 34 667 8qAC CDE F"
hex. str- !!
while cecimol >o:
hex.str : hex. (ocdes clecimal %16+hex. sdr
clecimal Il: 16
return hex.str
octl o Binory
ocbl numbes ore (onvaded to binany numbars by replocing
each chgit oth a three- bit binory numb
Let te octel number be 123
Corresponcing thee bit binary %r toch cigi is
The
2 3
001 OlO ol =(iol 0o11),
(123), - Co1oo),
Progrom
deP ocbl. to. birory (ot tal ):
derimal octal. to.cecimal (ocBol)
binay-'
while cdecimal >o:
binary str (decimal l%2)+ binary
clecimal Il=2
relaro bi rary
point (ocol o. binory Ci23))
taut: 1010!1
ocal to Decimal
An ocol number con be ronvetecd ino Derimal by asigning
topighl o each position
9: LeB the ocel numbes be 123
Assihn oeight eoch pasihon = x&2x8'+ 3xeO
- 83
ocal do Hradecimal
Aocal number ton be ronveo ed ib a heorocecimol number
by tonue ting the number into is clecimal ondthen the number to
hexaclecimal
Led he octol number be 123
Corres po nding decimol $3
53
(23), =(63),s
po0gyom
deP octLo. her Coclal):
cdecimol: ocol. to-cerioal (atl)
her-rodes "01234 56789ABCbEF"
hex-str-I)
while decimal >o:
hex-str- hex-codes clecimal oo 1o +hex-str
decimal-16
retarn hex- str
|10
2 1
retorn binory- st
point Cderimol.to-binory Clo))
tt output: 1o10
Derimal to Octal
o), =(12;
poogrom
de cecimol.to.octol (derimol):
octol.str:
wbile clecimol >o:
oclol.str - sth Cdeimal lo &) + tal. st
clerirmol I)= 8
retm octl. str
Derirmal to Herodecimol
foz clecirmol to harodecimal (onvexslon,the number is dividad
by 16 unil we gedo number betwpen oto Qond CA lo F) as the
remainde.
poogrom
deP clerimalto bex (decimal):
hex-cocles "l2345678g ABCOEF}
her. str !
while clecimol >o:
hex. str- hex-rocdes Cdecimol ooJ5]+ hex-str
cecimol I1=16
reBurn hez-str
paint (decirnol. to her Cio))
# output: A
Hexaderimal o Binary
The heraderi mal numbey is ronver tedl iob a Binary rumber
by replaring eoch hex cliit woh is binors PGutveleol
eg: Let tho hex number be FADE
torrespording binory Rr each hex
A
poogio
deP hex- to binary Chr.st):
clecimal: hex to binors Chex.Slr)
binany :'
while decimol >0:
binary sr (decimol %2)+ binory
decimal ||= 2
relurn binaiy
point Chexto. binory C'FADE))
#utput: 1))010o)110
Heoracleimol to becirpol
(eED), Ga53),0
return cecimal
poind(her-to-deimol C"BED"))
#outpul:3063
Heracdeimol to Dcal
relun ocBol