11# Python: Binary to Decimal Conversion
22# binToDec and decToBin functions are rendered obsolete by the universal convert function
33
4- def binToDec (binNum ):
4+ def binToDec (binNum ): #function created to convert binary to decimal with parametere binNum
55 decNum = 0
66 power = 0
7- while binNum > 0 :
8- decNum += 2 ** power * (binNum % 10 )
9- binNum //= 10
10- power += 1
7+ while binNum > 0 : #loop will run till binNum is greater than 0
8+ decNum += 2 ** power * (binNum % 10 )
9+ binNum //= 10 # reducing binNum everytime by 1 digit
10+ power += 1 # increasing power by 1 each loop
1111 return decNum
1212
13- def decToBin (decNum ):
13+ def decToBin (decNum ): #function created to convert decimal to binary with parametere decNum
1414 binNum = 0
1515 power = 0
16- while decNum > 0 :
16+ while decNum > 0 :#loop will run till decNum is greater than 0
1717 binNum += 10 ** power * (decNum % 2 )
18- decNum //= 2
19- power += 1
18+ decNum //= 2 # reducing decNum everytime by 1 digit
19+ power += 1 # increasing power by 1 each loop
2020 return binNum
2121
22- def convert (fromNum , fromBase , toBase ):
22+ def convert (fromNum , fromBase , toBase ): #function for converting from any base to any other base
2323 toNum = 0
2424 power = 0
2525 while fromNum > 0 :
@@ -31,4 +31,4 @@ def convert(fromNum, fromBase, toBase):
3131# print (str(binToDec(101011)))
3232# print (str(decToBin(128)))
3333print (str (convert (127 , 10 , 8 ))) # converts 127 in base 10 to base 8
34- print (str (convert (101001 , 2 , 2 )))
34+ print (str (convert (101001 , 2 , 2 )))
0 commit comments