Q:

Create integer variable by assigning octal value in Python

belongs to collection: Python basic programs

0

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 the variable

    x = 0o12345678

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

# 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  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Create integer variable by assigning hexadecimal v... >>
<< Create integer variable by assigning binary value ...