+ 6
Can anyone help me??
read this program--- def oct2(n): print("passed octal no.:",n) n=str(n) decNum=int(n,8) print("Number in Decimal:",decNum) print("Number in Hexadecimal:", hex(decNum)) print("Number in Binary:", bin(decNum)) num=int(input("Enter an octal number:")) oct2(num) Here, what is the use of decNum=int(n,8) I can't understand. Can anyone help me??
6 ответов
+ 9
There are 4 main systems of representing numbers:
- Decimal numbers (base 10)
- Hexadecimal numbers (base 16)
- Binary numbers (base 2)
- Octal numbers (base 8)
In Python, the `int()` function is defined as:
def int(str, base=10):
# ...
It receives a string value and an optional base, which defaults to 10.
Basically, all it does is it converts a string into a number. The number is formatted with the base as its rule.
int("10", 8)
Will output 8 because it is base 8 (or an Octal value)
+ 6
It is a pleasure 😊
+ 4
Diego ooh sorry, did quick math 😶🙂
+ 4
Thank you Gordon and Edwin Pratt. Thank u soo much.
+ 2
Treating the string as a number under octal number system (base 8), and converts into int of decimal system (base 10)
https://code.sololearn.com/cTB38H7UyEfD/?ref=app
+ 2
Edwin Pratt int("10", 8) will output 8, not 13.