Skip to content

Commit 7bf6773

Browse files
Added comments
1 parent 85cc70e commit 7bf6773

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

BinaryToDecimal.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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)))
3333
print (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

Comments
 (0)