Software Development Exam  >  Software Development Notes  >  Basics of Python  >  Number System Conversion in Python

Number System Conversion in Python | Basics of Python - Software Development PDF Download

Introduction

Number system conversion is a common task in programming, and Python provides built-in functions to convert numbers from one number system to another. Understanding number system conversions is essential for working with different types of data and solving mathematical problems. This article will introduce you to the concept of number system conversion in Python and provide simple code examples to help you understand the process.

Decimal to Binary Conversion

Binary is a base-2 number system that uses only two digits: 0 and 1. Converting decimal numbers to binary is a common operation in computer science. Python provides the 'bin()' function to convert decimal numbers to binary format.

Here's an example that demonstrates how to convert a decimal number to binary:

decimal_number = 10

binary_number = bin(decimal_number)

print(binary_number)  # Output: 0b1010

In the example above, the 'bin()' function takes the decimal number '10' as input and returns a binary representation of the number as a string ('0b1010'). The prefix '0b' indicates that the number is in binary format.

Binary to Decimal Conversion

Converting binary numbers to decimal is also a common operation. Python provides the 'int()' function to convert binary strings to decimal numbers.

Let's see an example:

binary_number = '1010'

decimal_number = int(binary_number, 2)

print(decimal_number)  # Output: 10

In the example above, the int() function takes the binary string '1010' and the base '2' as arguments. It returns the decimal representation of the binary number ('10' in this case).

Decimal to Hexadecimal Conversion

Hexadecimal is a base-16 number system that uses digits from 0 to 9 and letters from A to F to represent values from 10 to 15. Python provides the 'hex()' function to convert decimal numbers to hexadecimal format.

Here's an example:

decimal_number = 255

hexadecimal_number = hex(decimal_number)

print(hexadecimal_number)  # Output: 0xff

In the example above, the 'hex()' function takes the decimal number '255' as input and returns a hexadecimal representation of the number as a string ('0xff'). The prefix '0x' indicates that the number is in hexadecimal format.

Hexadecimal to Decimal Conversion

Converting hexadecimal numbers to decimal is straightforward in Python. The 'int()' function can also be used to convert hexadecimal strings to decimal numbers by specifying the base as '16'.

Let's see an example:

hexadecimal_number = 'ff'

decimal_number = int(hexadecimal_number, 16)

print(decimal_number)  # Output: 255

In the example above, the 'int()' function takes the hexadecimal string 'ff' and the base '16' as arguments. It returns the decimal representation of the hexadecimal number ('255' in this case).

Sample Problems

Problem 1: Convert the decimal number 42 to binary.

decimal_number = 42

binary_number = bin(decimal_number)

print(binary_number)  # Output: 0b101010

Problem 2: Convert the binary number 1101 to decimal.

binary_number = '1101'

decimal_number = int(binary_number, 2)

print(decimal_number)  # Output: 13

Problem 3: Convert the decimal number 128 to hexadecimal.

decimal_number = 128

hexadecimal_number = hex(decimal_number)

print(hexadecimal_number)

The document Number System Conversion in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
62 videos|72 docs|18 tests
Related Searches

Extra Questions

,

mock tests for examination

,

Number System Conversion in Python | Basics of Python - Software Development

,

Free

,

practice quizzes

,

Objective type Questions

,

pdf

,

video lectures

,

Previous Year Questions with Solutions

,

MCQs

,

shortcuts and tricks

,

Number System Conversion in Python | Basics of Python - Software Development

,

Important questions

,

Viva Questions

,

Semester Notes

,

ppt

,

Number System Conversion in Python | Basics of Python - Software Development

,

study material

,

Exam

,

past year papers

,

Sample Paper

,

Summary

;