TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python oct() function with Examples

Python oct() function with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python oct() Function

Python oct() function is used to get an octal value of an integer number. This method takes an argument and returns an integer converted into an octal string. It throws an error TypeError if argument type is other than an integer.

The signature of oct() function is given below.

Signature

oct (integer)

Parameters

integer : An integer value which is to be converted into an octal string.

Return

It returns an octal string.

Let's see some examples of oct() function to understand it's functionality.

Python oct() Function Example 1

A simple python example to get an octal value of a decimal number.

# Python oct() function example
# Calling function
val = oct(10)
# Displaying result
print("Octal value of 10:",val)

Output:

Octal value of 10: 0o12

Python oct() Function Example 2

The oct() function accepts only integer value. The number passed other than integer will get an error. See the example below.

# Python oct() function example
# Calling function
val = oct(10.25)
# Displaying result
print("Octal value of 10.25:",val)

Output:

TypeError: 'float' object cannot be interpreted as an integer

Python oct() Function Example 3

In Python, binary, hexadecimal values are integer values. So, we can get octal conversion of binary and hexadecimal values as well. See an example below.

# Python oct() function example
# Calling function
val  = oct(0b0101) # Binary to octal
val2 = oct(0XAF) # Hexadecimal to octal
# Displaying result
# binary number
print("Octal value of 0b0101:",val)
# hexadecimal number
print("Octal value of 0XAF:",val2)

Output:

Octal value of 0b0101: 0o5
Octal value of 0XAF: 0o257

Next TopicPython Functions




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf