Nearby lessons

26 of 159

Python - Base Conversion

📌 What You Will Learn
  • Write integer literals in decimal, binary, octal, and hexadecimal form
  • Understand the 0b, 0o, and 0x prefixes
  • Convert any base to binary using bin()
  • Convert any base to octal using oct()
  • Convert any base to hexadecimal using hex()

What is Base Conversion?

Numbers can be represented in different bases or number systems. Python allows an integer literal to be written in decimal, binary, octal, or hexadecimal form.

Although we can specify values in any of these forms, the Python Virtual Machine (PVM) always provides values only in decimal form.

Integer Literal Forms

Form Base Allowed Digits Prefix
Decimal Base-10 0 to 9 None
Binary Base-2 0, 1 0b or 0B
Octal Base-8 0 to 7 0o or 0O
Hexadecimal Base-16 0 to 9, a to f 0x or 0X

Writing Literal Values in Different Forms

All of the following are valid integer literals:

🐍Code Cell
1a = 10 # decimal form
2b = 0o10 # octal form
3c = 0X10 # hexadecimal form
4d = 0B10 # binary form
5 
6print(a)
7print(b)
8print(c)
9print(d)
Output
10
8
16
2

Explanation

Each literal is written using a different base, but Python converts every value internally and displays it in decimal form.

  • 0o10 = 8 in decimal
  • 0X10 = 16 in decimal
  • 0B10 = 2 in decimal

The bin() Function

We can use bin() to convert a value from any base to binary. It returns a string prefixed with 0b.

🐍Code Cell
1print(bin(15))
2print(bin(0o11))
3print(bin(0X10))
Output
0b1111
0b1001
0b10000

Explanation

  • bin(15) converts the decimal value 15 to binary, giving 0b1111.
  • bin(0o11) converts the octal value 11 to binary, giving 0b1001.
  • bin(0X10) converts the hexadecimal value 10 to binary, giving 0b10000.

The oct() Function

We can use oct() to convert a value from any base to octal. It returns a string prefixed with 0o.

🐍Code Cell
1print(oct(10))
2print(oct(0B1111))
3print(oct(0X123))
Output
0o12
0o17
0o443

Explanation

  • oct(10) converts the decimal value 10 to octal, giving 0o12.
  • oct(0B1111) converts the binary value 1111 to octal, giving 0o17.
  • oct(0X123) converts the hexadecimal value 123 to octal, giving 0o443.

The hex() Function

We can use hex() to convert a value from any base to hexadecimal. It returns a string prefixed with 0x.

🐍Code Cell
1print(hex(100))
2print(hex(0B111111))
3print(hex(0o12345))
Output
0x64
0x3f
0x14e5

Explanation

  • hex(100) converts the decimal value 100 to hexadecimal, giving 0x64.
  • hex(0B111111) converts the binary value 111111 to hexadecimal, giving 0x3f.
  • hex(0o12345) converts the octal value 12345 to hexadecimal, giving 0x14e5.

The Conversion Functions at a Glance

Function Converts To Prefix Example
bin() Binary 0b bin(15) -> '0b1111'
oct() Octal 0o oct(10) -> '0o12'
hex() Hexadecimal 0x hex(100) -> '0x64'

Points to Remember

  • The bin(), oct(), and hex() functions all return strings.
  • They can accept an integer written in any base.
  • The result always includes the base prefix 0b, 0o, or 0x.
  • Hexadecimal digits can be written in upper or lower case in literals.
  • The PVM always displays values in decimal form regardless of how they were written.
📝 Key Takeaways
  • Integer literals can be written in decimal, binary (0b), octal (0o), and hexadecimal (0x) form
  • The Python interpreter always displays values in decimal form
  • bin() converts a number from any base to a binary string prefixed with 0b
  • oct() converts a number from any base to an octal string prefixed with 0o
  • hex() converts a number from any base to a hexadecimal string prefixed with 0x

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8