Remember
oct() returns a string containing an integer’s octal representation, including a 0o prefix.
oct() returns a string containing an integer’s octal representation, including a 0o prefix.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 8
print(oct(number))Top-level scope
8oct() returns a string containing an integer’s octal representation, including a 0o prefix.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 8
print(oct(number))
|
v
DISPLAYED RESULT
0o10
FINAL BINDINGS
number --> object 1 (int)
object 1: 8Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 8
print(oct(number))Top-level scope
8The example has finished. Compare the final output with the values and references above.
0o10
What do you expect this program to print?
number = 8
print(oct(number))oct() FunctionThe oct() function in Python converts an integer number into its octal string representation, prefixed with 0o.
Octal numbers use base 8 and are often used in file permissions and low-level computing.
oct(number)
number – A valid integer (positive or negative)0oprint(oct(10))
0o12
Explanation: 10 in decimal = 1×8 + 2 = 12 in octal
print(oct(-25))
-0o31
Explanation: The sign is preserved, and 25 in decimal = 3×8 + 1 = 31 in octal.
print(oct(int('0xA', 16))) # Hexadecimal to Octal
0o12
Explanation: 0xA (hex) = 10 (decimal) → 0o12 (octal)
chmod 755)oct() only accepts integers. Passing a float or string will raise a TypeError.0o.Functions like bin(), oct(), and hex() are often used in bit manipulation problems. Know how to use them and convert between bases.
oct(n) returns the octal string representation of an integer n.0o and is always a string.Write a Python program that reads a number from the user and prints its binary, octal, and hexadecimal forms.
num = int(input("Enter a number: "))
print("Octal:", oct(num))
print("Binary:", bin(num))
print("Hexadecimal:", hex(num))
Connect what you noticed in the example with the idea below.
oct() returns a string containing an integer’s octal representation, including a 0o prefix.
oct() returns an integer written in decimal.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.