
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I specify hexadecimal and octal integers in Python?
The Hexadecimal and Octal are part of Numeric Types in Python. Let’s see how to specify them one by one.
For hexadecimal type, add a preceding 0x. For example −
0x11
For Octal type (base 8), add a preceding 0 (zero). For example −
0O20
Hexadecimal Integers in Python
Hexadecimal Number System uses 10 digits and 6 letters, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Letters represent the numbers starting from 10. A = 10. B = 11, C = 12, D = 13, E = 14, F = 15 Also called as base 16 number system.
Example
To represent hexadecimal type, add a preceding 0x −
a = 0x12 print("Hexadecimal = ",a) print("Type = ",type(a))
Output
Hexadecimal = 18 Type = <class 'int'>
Octal Integers in Python
Octal Number uses eight digits, 0,1,2,3,4,5,6,7. Also called as base 8 number system. Each position in an octal number represents a 0 power of the base (8). Last position in an octal number represents a x power of the base (8).
Example
To represent Octal type (base 8), add a preceding 0 (zero) −
a = 0O20 print("Octal = ",a) print("Type = ",type(a))
Output
Octal = 16 Type = <class 'int'>
Let us see other examples −
Convert Decimal to Octal
Example
To convert Decimal to Octal, use the oct() method and set the Decimal Number as a parameter −
# Decimal Number dec = 110 # Display the Decimal Number print("Decimal = ",dec) # Display the Octal form print('The number {} in octal form = {}'.format(dec, oct(dec)))
Output
Decimal = 110 The number 110 in octal form = 0o156
Convert Decimal to Hexadecimal
To convert Decimal to Hexadecimal, use the hex() method and set the Decimal Number as a parameter −
Example
# Decimal Number dec = 110 # Display the Decimal Number print("Decimal = ",dec) # Display the Hexadecimal form print('The number {} in hexadecimal form = {}'.format(dec, hex(dec)))
Output
Decimal = 110 The number 110 in hexadecimal form = 0x6e
- Related Articles
- How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?
- Formatter Specifier for Octal and Hexadecimal in Java
- Convert Hexadecimal to Octal in Java?
- JAVA Program to Convert Octal to Hexadecimal
- How do I specify an arrow-like linestyle in Matplotlib?
- How do you specify and enforce an interface spec in Python?
- Program to Convert Hexadecimal to Octal in C program
- How do I specify different layouts for portrait and landscape orientations in Android?
- How can I generate random integers between 0 and 9 using Python?
- How do we specify the buffer size when opening a file in Python?
- How do I convert between tuples and lists in Python?
- How can I do "cd" in Python?
- How do I Install MySQLdb in Python?
- How do I write JSON in Python?
- How do I install Python SciPy?
