Python oct() Function

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.

Python oct() Function

ArgumentintegerAn integer value or object implementing the __index__() method.
Return ValuestringReturns a string of octal numbers, prefixed with "0o".
Input : oct(1)
Output : '0o1'

Input : oct(2)
Output : '0o2'

Input : oct(4)
Output : '0o4'

Input : oct(8) 
Output : '0o10'

Input : oct(9)
Output : '0o11'

Input : oct(40)
Output : '0o50'

Input : oct(42)
Output : '0o52'

But before we move on, I’m excited to present you my brand-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).

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

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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!