Change language

bin () in Python

| | |

Naive methods for decimal to binary conversion

1. Using recursion

 ` # Function for printing a binary number for ` ` # entering a decimal numbers using recursion ` ` def ` ` decimalToBinary (n): `   ` if ` ` n" ` ` 1 ` `: ` ` # share with cumulative result ` ` # (cancel remainder) ` ` decimalToBinary (n ` ` / ` ` / ` ` 2 ` `) `     ` ` ` print ` ` (n ` `% ` ` 2 ` `, end ` ` = ` ` "") `   ` # Driver code ` ` if ` ` __ name__ ` ` = ` ` = ` ` ’__main__’ ` `: ` ` ` ` decimalToBinary (` ` 8 ` `) ` ` print ` ` (` ` "" ` `) ` ` decimalToBinary (` ` 18 ` `) ` ` print ` ` (` ` "" ` `) ` ` decimalToBinary (` ` 7 ` `) ` ` ` ` print `

Output:

` 1000 10010 111 `

2. Using the loop

 ` # Python code to demonstrate the naive method ` ` # using loop `   ` # function that returns a binary string ` ` def ` ` Binary (n): ` ` binary ` ` = ` ` "" ` ` i ` ` = ` ` 0 ` ` while ` ` n" ` ` 0 ` ` and ` ` i "` ` = ` ` 8 ` `: ` ` ` ` s1 ` ` = ` ` str ` ` (` ` int ` ` (n ` `% ` ` 2 ` `)) ` ` ` ` binary ` ` = ` ` binary ` ` + ` ` s1 ` ` ` ` n ` ` / ` ` = ` ` 2 ` ` i ` ` = ` ` i ` ` + ` ` 1 ` ` ` ` d ` ` = ` ` binary [:: ` ` - ` ` 1 ` `] ` ` ` ` return ` ` d ` ` `  ` print ` ` (` ` "The binary representation of 100 (using loops) is: "` `, end ` ` = ` `" ") ` ` print ` ` (Binary (` ` 100 ` `)) `

Output:

` The binary representation of 100 (using loops) is: 001100100 `

Using bin ()

Using bin () reduces the time required for coding and also removes the difficulties you might see in the above two methods.

`  Syntax:  bin (a)  Parameters:   a:  an integer to convert  Return Value:  A binary string of an integer or int object.  Exceptions:  Raises TypeError when a float value is sent in arguments. `

` `

` # Python code to demonstrate how it works # bin ()   # function that returns a binary string def Binary (n): s = bin (n)   # remove the prefix & quot; 0b & quot; s1 = s [ 2 :] return s1   print ( "The binary representation of 100 (using bin ()) is:" , end = "") print (Binary ( 100 )) `

Output:

` The binary representation of 100 (using bin ()) is: 1100100 `

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically