Octal Numbers
Understanding Octal
Octal is a base-8 number system, which means it uses only eight digits (0-7) to represent numbers.
Representing Octal Numbers in Python
To represent an octal number in Python, you prefix it with 0o
or 0O
:
Python
octal_number =
0o75
# Equivalent to decimal 61
Converting Between Decimal and Octal
· Decimal to Octal:
o
Use the oct()
function:
Python
decimal_number =
61
octal_string =
oct(decimal_number)
# Output: '0o75'
· Octal to Decimal:
o
Use the int()
function with base 8:
Python
octal_string =
'0o75'
decimal_number =
int(octal_string,
8)
# Output: 61
Key Points
· Octal numbers are less commonly used than decimal or hexadecimal.
· Python provides built-in functions for easy conversion between decimal and octal.
· Octal numbers can be used for specific tasks, such as file permissions in Unix-like systems.