Home »
Python »
Python Programs
Create integer variable by assigning octal value in Python
Here, we are going to learn how to create an integer variable by assigning value in octal format in Python?
By IncludeHelp Last updated : April 08, 2023
Integer variable with octal value
The task is to create integer variables and assign values in octal format.
Octal value assignment
To assign value in octal format to a variable, we use 0o suffix. It tells to the compiler that the value (suffixed with 0o) is an octal value and assigns it to the variable.
Syntax to assign an octal value to variable
x = 0o12345678
Python code to create variable by assigning octal value
In this program, we are declaring some of the variables by assigning the values in octal format, printing their types, values in decimal format and octal format.
Note: To print value in octal format, we use oct() function.
# Python code to create variable
# by assigning octal value
# creating number variable
# and, assigning octal value
a = 0o1234567
b = 0o7654321
c = 0o1745
d = 0o100
e = 0o123
# printing types
print("type of the variables...")
print("type of a: ", type(a))
print("type of b: ", type(b))
print("type of c: ", type(c))
print("type of d: ", type(d))
print("type of e: ", type(e))
# printing values in decimal format
print("value of the variables in decimal format...")
print("value of a: ", a)
print("value of b: ", b)
print("value of c: ", c)
print("value of d: ", d)
print("value of e: ", e)
# printing values in octal format
print("value of the variables in octal format...")
print("value of a: ", oct(a))
print("value of b: ", oct(b))
print("value of c: ", oct(c))
print("value of d: ", oct(d))
print("value of e: ", oct(e))
Output
type of the variables...
type of a: <class 'int'>
type of b: <class 'int'>
type of c: <class 'int'>
type of d: <class 'int'>
type of e: <class 'int'>
value of the variables in decimal format...
value of a: 342391
value of b: 2054353
value of c: 997
value of d: 64
value of e: 83
value of the variables in octal format...
value of a: 0o1234567
value of b: 0o7654321
value of c: 0o1745
value of d: 0o100
value of e: 0o123
Python Basic Programs »
Advertisement
Advertisement