Skip to content

Understanding Number Systems and Binary Conversion in Python

Overview of Number Systems in Programming

Programming commonly uses different number systems:

  • Decimal (Base 10): The everyday number system, digits 0-9.
  • Binary (Base 2): Used internally by computers, digits 0-1.
  • Octal (Base 8): Digits 0-7, used in some computing contexts.
  • Hexadecimal (Base 16): Digits 0-9 and A-F, widely used in representing MAC addresses and IPv6.

Learn more about the foundational concepts in the Understanding the Real Number System: Key Concepts and Definitions.

Why Use Different Number Systems?

  • Binary: Fundamental to how data is stored and processed.
  • Octal and Hexadecimal: More compact representations of binary data.
  • Decimal: User-friendly for input/output operations.

See detailed explanations and practical implications in Understanding Data Representation in C Programming.

Converting Decimal to Binary Manually

To convert a decimal number (e.g., 25) to binary:

  1. Divide the number by 2.
  2. Write down the remainder.
  3. Divide the quotient by 2 again.
  4. Repeat until the quotient is 0.
  5. Read the remainders in reverse order to obtain the binary number.

Example for 25:

  • 25 ÷ 2 = 12, remainder 1
  • 12 ÷ 2 = 6, remainder 0
  • 6 ÷ 2 = 3, remainder 0
  • 3 ÷ 2 = 1, remainder 1
  • 1 ÷ 2 = 0, remainder 1

Reading remainders in reverse: 11001 (binary).

For a deeper understanding of working with number systems in Python, consider reviewing Основы работы с переменными и типами данных в Python.

Using Python for Number System Conversion

Python provides built-in functions to convert numbers:

  • bin(25) returns '0b11001' for binary.
  • oct(25) returns '0o31' for octal.
  • hex(25) returns '0x19' for hexadecimal.

The prefixes (0b, 0o, 0x) indicate the number system.

Learn more about nuances of octal values and related conversions in Understanding Octal Values and Macro String Replacement in C Programming.

Converting Binary to Decimal

To convert a binary number like 0b10101 to decimal, Python automatically recognizes the prefix:

int('0b10101', 2)  # returns 21

Manually, calculate:

  • Multiply each binary digit by 2 raised to the power of its position (starting from 0 on the right).
  • Sum the results.

Example: 10101 = (1×24) + (0×23) + (1×22) + (0×21) + (1×20) = 16 + 0 + 4 + 0 + 1 = 21.

Practical Tips & Homework

  • Practice converting numbers manually and using Python functions.
  • Try converting decimal values like 21, 52, and 65 to binary.
  • Convert binary numbers such as 0b110011010 back to decimal.

Understanding these conversions strengthens your grasp on bitwise operations and data representation in programming. This foundational knowledge is essential for fields like networking (MAC, IPv6), computer architecture, and low-level programming.

For extended insights into data types and ranges which support these operations, visit Understanding Integer Data Type: Size, Range, and Number Systems Explained.


If you have questions or want further explanations on specific conversions or bitwise operations, feel free to ask in the comments!

Keep this summary

Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.

Save to LunaNotes

Or summarise for another video.

32K+ people have turned 65K+ videos into notes

“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
Bhai Chrome Web Store review

This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.

Related summaries

How Computers Work: From Binary Numbers to CPU Design Explained

How Computers Work: From Binary Numbers to CPU Design Explained

This comprehensive guide explains how computers work from the ground up, starting with binary number representation and building toward CPU design. The video covers decimal to binary conversion, binary addition and subtraction, and the fundamental principles behind how computers process information using just zeros and ones.

Convert IPv4 Addresses: Binary to Dotted Decimal & Vice Versa

Convert IPv4 Addresses: Binary to Dotted Decimal & Vice Versa

This video provides a comprehensive tutorial on converting IPv4 addresses between dotted decimal and binary notation. Learn a simple, step-by-step method using the 8-bit power-of-two sequence to perform conversions accurately for all four octets of an IPv4 address.

Python Mastery Course: Complete Beginner to Pro Guide (2024)

Python Mastery Course: Complete Beginner to Pro Guide (2024)

Learn Python from scratch with this comprehensive guide covering variables, strings, numbers, and essential programming concepts. Master Python for AI, web development, and automation with step-by-step tutorials from an experienced software engineer.

Bit Manipulation in Java: Get, Set, Clear & Update Bit Operations

Bit Manipulation in Java: Get, Set, Clear & Update Bit Operations

Learn bit manipulation in Java with this comprehensive guide covering 4 major bit operations: Get, Set, Clear, and Update. Understand bitmasking techniques, left/right shift operators, and see practical code examples to master placement and programming interview questions.

Comprehensive Python Tutorial: Variables, Control Flow, Functions, Classes, and File Handling

Comprehensive Python Tutorial: Variables, Control Flow, Functions, Classes, and File Handling

This detailed video tutorial covers foundational to advanced Python programming concepts, including variables, operators, user input, control structures, loops, functions, classes with inheritance, exception handling, and file operations. Designed for learners aiming to build practical coding skills, it offers example-driven explanations and program walkthroughs for real-world application.

Found this summary useful?

Take it with you. One click puts it in your own LunaNotes library.

Save to LunaNotes

Start taking better notes today with LunaNotes