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?
Submitted by IncludeHelp, on April 26, 2019

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.