0 votes
1 view
in Python by (47.8k points)

How do you express an integer as a binary number with Python literals?

I was easily able to find the answer for hex:

>>> 0x12AF 

4783 

>>> 0x100 

256

and octal:

>>> 01267 

695 

>>> 0100 

64

How do you use literals to express binary in Python?

1 Answer

0 votes
by (107k points)

To express an integer as a binary number with Python literals you can use the below-mentioned ways:-

In Python you can express binary literals using the prefix 0b or 0B below is the example for the same:

>>> 0b101111

image

If you want to express integer into binary then you can use the new bin function to get the binary representation of a number:

>>> bin(173) 

image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 4, 2019 in Python by Tech4ever (20.3k points)
+3 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...