Get professional AI headshots with the best AI headshot generator. Save hundreds of dollars and hours of your time.

Introduction to the oct() Function

In Python, the oct() function is used to convert integers (whole numbers) into their octal (base-8) representation. Octal representation uses a base of 8, and the digits range from 0 to 7. This can be particularly useful when you need to display or work with numbers in an octal format, such as when dealing with permissions in UNIX-like systems.

The syntax of the oct() function is quite simple:


Here, number is the integer that you want to convert to its octal representation. The function returns a string representing the octal value of the given integer.

Examples of Using oct()

Let’s dive into some examples to see how the oct() function works and how it can be used in different scenarios.

Example 1: Converting an Integer to Octal

# Example 1: Converting an Integer to Octal
decimal_number = 123
octal_representation = oct(decimal_number)

print(f"The octal representation of {decimal_number} is {octal_representation}")

In this example, we have an integer decimal_number with the value of 123. We use the oct() function to convert this integer into its octal representation. The resulting octal representation is stored in the variable octal_representation. Finally, we use the print() function to display the original decimal number and its corresponding octal representation.

When you run this code, the output will be:

The octal representation of 123 is 0o173

The prefix 0o indicates that the following digits are in octal format.

Example 2: Using Octal Numbers in UNIX Permissions

In UNIX-like operating systems, file and directory permissions are often represented using octal notation. The octal digits correspond to different permission levels: read (4), write (2), and execute (1). Let’s see how the oct() function can be used to create and display these permission values.

# Example 2: Using Octal Numbers in UNIX Permissions
read_permission = 4
write_permission = 2
execute_permission = 1

# Combine permissions to create different permission values
read_write = read_permission + write_permission
read_execute = read_permission + execute_permission
read_write_execute = read_permission + write_permission + execute_permission

# Display permission values in octal format
print(f"Read permission: {oct(read_permission)}")
print(f"Write permission: {oct(write_permission)}")
print(f"Execute permission: {oct(execute_permission)}")
print(f"Read + Write permission: {oct(read_write)}")
print(f"Read + Execute permission: {oct(read_execute)}")
print(f"All permissions: {oct(read_write_execute)}")

In this example, we have three variables read_permission, write_permission, and execute_permission representing the values of read, write, and execute permissions, respectively. We use these values to create different combinations of permissions, such as read + write, read + execute, and all permissions.

The oct() function is used to convert these permission values into their octal representation. The resulting octal representations are then displayed using the print() function.

When you run this code, the output will be:

Read permission: 0o4
Write permission: 0o2
Execute permission: 0o1
Read + Write permission: 0o6
Read + Execute permission: 0o5
All permissions: 0o7

Practical Applications of oct()

The oct() function is not only useful for converting integers to octal representation, but it also has practical applications in various programming scenarios. Some of these applications include:

1. File Manipulation

When working with low-level file operations, such as setting file permissions, the oct() function can be used to easily convert permission values to their octal notation.

2. Bitwise Operations

Octal representation can be used in bitwise operations. For instance, you can use octal numbers to represent sets of flags or options and then perform bitwise operations on them.

3. Embedded Systems

In embedded systems programming, memory addresses and hardware registers are often expressed using hexadecimal or octal notation. The oct() function can be handy for converting these values to a more readable format.

4. Educational Purposes

Teaching binary, octal, and hexadecimal number systems can be made more intuitive by using the oct() function to demonstrate octal conversions.


The oct() function in Python provides a straightforward way to convert integers to their octal representation. This is especially useful when working with octal-based systems, such as UNIX permissions, or when dealing with low-level programming tasks that require octal representation. By understanding how to use the oct() function, you can enhance your coding capabilities and tackle a broader range of programming challenges.

Remember that octal representation is just one of the many ways to represent numbers in different bases, and the oct() function is a powerful tool in your programming toolbox that allows you to seamlessly work with octal numbers.

In this tutorial, we covered the basics of the oct() function, provided examples of its usage, and explored its practical applications. Armed with this knowledge, you can confidently incorporate octal representation into your Python programming endeavors.

Leave a Reply

Your email address will not be published. Required fields are marked *