Python bin() (With Examples) (2024)

The bin() method converts a specified integer number to its binary representation and returns it.

Example

number = 15

# convert 15 to its binary equivalent print('The binary equivalent of 15 is', bin(number))

# Output: The binary equivalent of 15 is 0b1111

bin() Syntax

The syntax of bin() method is:

bin(number)

bin() Parameter

The bin() method takes in a single parameter:

  • number - an integer whose binary equivalent is calculated

bin() Return Value

The bin() method returns:

  • the binary string equivalent to the given integer
  • TypeError for a non-integer argument

Example 1: Python bin()

number = 5 

# convert 5 to its binary equivalentprint('The binary equivalent of 5 is:', bin(number))

Output

The binary equivalent of 5 is: 0b101

In the above example, we have used the bin() method to convert the argument 5 to its binary representation i.e. 101.

Here, the prefix 0b in the output 0b101 represents that the result is a binary string.

Example 2: Python bin() with a Non-Integer Class

class Quantity: apple = 1 orange = 2 grapes = 2 def func(): return apple + orange + grapes print('The binary equivalent of quantity is:', bin(Quantity()))

Output

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

Here, we have passed an object of class Quantity to the bin() method and got a TypeError.

This is because we have used a non-integer class.

Note: We can fix the TypeError above by using the Python __index__() method with a non-integer class.

Example 3: bin() with __index__() for Non-Integer Class

class Quantity: apple = 1 orange = 2 grapes = 2 def __index__(self): return self.apple + self.orange + self.grapes print('The binary equivalent of quantity is:', bin(Quantity()))

Output

The binary equivalent of quantity is: 0b101

Here, we have passed an object of class Quantity to the bin() method.

The bin() method doesn't raise a TypeError even if the object Quantity() is not an integer.

This is because we have used the __index__() method which returns an integer (in this case, sum of the fruits).

Also Read:

Python bin() (With Examples) (2024)
Top Articles
Gross Profit vs Net Profit – How are they different | Zoho Books
How Much Money Can Custom T-Shirts Make? – Printify
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6118

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.