Python’s built-in oct(integer)
function takes one integer argument and returns an octal string with prefix "0o"
. If you call oct(x)
on a non-integer x
, it must define the __index__()
method that returns an integer associated to x
. Otherwise, it’ll throw a TypeError: object cannot be interpreted as an integer
.
Argument | integer | An integer value or object implementing the __index__() method. |
Return Value | string | Returns a string of octal numbers, prefixed with "0o" . |
Input :oct(1)
Output :'0o1'
Input :Output :
oct
(2)'0o2'
Input :Output :
oct
(4)'0o4'
Input :Output :
oct
(8)'0o10'
Input :Output :
oct
(9)'0o11'
Input :oct(40)
Output :'0o50'
Input :oct(42)
Output :'0o52'
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Python oct() for Custom Objects
If you call oct(x)
on a non-integer or custom object x
, it must define the __index__()
method that returns an integer associated to x
.
class Foo: def __index__(self): return 8 f1 = Foo() print(oct(f1)) # '0o10'
How to Fix “TypeError: ‘float’ object cannot be interpreted as an integer”?
Python’s oct() function can only convert whole numbers from any numeral system (e.g., decimal, binary, hexadecimal) to the octal system. It cannot convert floats to octal numbers. So, if you pass a float into the oct() function, it’ll throw a TypeError: 'float' object cannot be interpreted as an integer
.
>>> oct(3.14) Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> oct(3.14) TypeError: 'float' object cannot be interpreted as an integer
To resolve this error, you can round the float to an integer using the built-in round()
function or you write your own custom conversion function:
How to Convert a Float to an Octal Number in Python?
def float_to_octal(f, digits=4): whole = int(f) rem = (f - whole) * 8 int_ = int(rem) rem = (rem - int_) * 8 octals = [str(int_)] count = 1 while rem and count < digits: count += 1 int_ = int(rem) rem = (rem - int_) * 8 octals.append(str(int_)) return float("{:o}.{}".format(whole, "".join(octals))) print(float_to_octal(3.14)) print(float_to_octal(88.88))
The output are the octal representations of the float input values:
3.1075 130.7024
🔥 Recommended Tutorial: How to Convert an Octal Escape Sequence in Python – And Vice Versa?
Summary
Python’s built-in oct(integer)
function takes one integer argument and returns an octal string with prefix "0o"
.
>>> oct(1) '0o1' >>> oct(2) '0o2' >>> oct(4) '0o4' >>> oct(8) '0o10' >>> oct(9) '0o11' >>> oct(42) '0o52' >>> oct(40) '0o50'
If you call oct(x)
on a non-integer x
, it must define the __index__()
method that returns an integer associated to x
.
>>> class Foo: def __index__(self): return 8 >>> f1 = Foo() >>> oct(f1) '0o10'
Otherwise, it’ll throw a TypeError: object cannot be interpreted as an integer
.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.