How do you convert a number to base 3 in Python?

In your study of computer science, you have probably been exposed in one way or another to the idea of a binary number. Binary representation is important in computer science since all values stored within a computer exist as a string of binary digits, a string of 0s and 1s. Without the ability to convert back and forth between common representations and binary numbers, we would need to interact with computers in very awkward ways.

Integer values are common data items. They are used in computer programs and computation all the time. We learn about them in math class and of course represent them using the decimal number system, or base 10. The decimal number 23310233_{10}233​10​​ and its corresponding binary equivalent 11101001211101001_211101001​2​​ are interpreted respectively as

2×102+3×101+3×1002\times10^{2} + 3\times10^{1} + 3\times10^{0}2×10​2​​+3×10​1​​+3×10​0​​

and

1×27+1×26+1×25+0×24+1×23+0×22+0×21+1×201\times2^{7} + 1\times2^{6} + 1\times2^{5} + 0\times2^{4} + 1\times2^{3} + 0\times2^{2} + 0\times2^{1} + 1\times2^{0}1×2​7​​+1×2​6​​+1×2​5​​+0×2​4​​+1×2​3​​+0×2​2​​+0×2​1​​+1×2​0​​

But how can we easily convert integer values into binary numbers? The answer is an algorithm called “Divide by 2” that uses a stack to keep track of the digits for the binary result.

The Divide by 2 algorithm assumes that we start with an integer greater than 0. A simple iteration then continually divides the decimal number by 2 and keeps track of the remainder. The first division by 2 gives information as to whether the value is even or odd. An even value will have a remainder of 0. It will have the digit 0 in the ones place. An odd value will have a remainder of 1 and will have the digit 1 in the ones place. We think about building our binary number as a sequence of digits; the first remainder we compute will actually be the last digit in the sequence. As shown below, we again see the reversal property that signals that a stack is likely to be the appropriate data structure for solving the problem.

How do you convert a number to base 3 in Python?
Decimal-to-binary conversion

The Python code below implements the Divide by 2 algorithm. The function convert_to_binary takes an argument that is a decimal number and repeatedly divides it by 2. Line 7 uses the built-in modulo operator, %, to extract the remainder and line 8 then pushes it on the stack. After the division process reaches 0, a binary string is constructed in lines 11-13. Line 11 creates an empty string. The binary digits are popped from the stack one at a time and appended to the right-hand end of the string. The binary string is then returned.

The algorithm for binary conversion can easily be extended to perform the conversion for any base. In computer science it is common to use a number of different encodings. The most common of these are binary, octal (base 8), and hexadecimal (base 16).

The decimal number 233233233 and its corresponding octal and hexadecimal equivalents 3518351_{8}351​8​​ and E916E9_{16}E9​16​​ are interpreted as

3×82+5×81+1×803\times8^{2} + 5\times8^{1} + 1\times8^{0}3×8​2​​+5×8​1​​+1×8​0​​

and

14×161+9×16014\times16^{1} + 9\times16^{0}14×16​1​​+9×16​0​​

The function convert_to_binary can be modified to accept not only a decimal value but also a base for the intended conversion. The “Divide by 2” idea is simply replaced with a more general “Divide by base.” A new function called convert_to_base, shown below, takes a decimal number and any base between 2 and 16 as parameters. The remainders are still pushed onto the stack until the value being converted becomes 0. The same left-to-right string construction technique can be used with one slight change. Base 2 through base 10 numbers need a maximum of 10 digits, so the typical digit characters 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 work fine. The problem comes when we go beyond base 10. We can no longer simply use the remainders, as they are themselves represented as two- digit decimal numbers. Instead we need to create a set of digits that can be used to represent those remainders beyond 9.

A solution to this problem is to extend the digit set to include some alphabet characters. For example, hexadecimal uses the ten decimal digits along with the first six alphabet characters for the 16 digits. To implement this, a digit string is created that stores the digits in their corresponding positions. 0 is at position 0, 1 is at position 1, A is at position 10, B is at position 11, and so on. When a remainder is removed from the stack, it can be used to index into the digit string and the correct resulting digit can be appended to the answer. For example, if the remainder 13 is removed from the stack, the digit D is appended to the resulting string.

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

See DetailsRight Arrow

Start course

Python Data Science Toolbox (Part 1)

Beginner

3 hr

351.4K

Learn the art of writing your own functions in Python, as well as key concepts like scoping and error handling.

See DetailsRight Arrow

Start course

Data Types for Data Science in Python

Beginner

4 hr

56.6K

Consolidate and extend your knowledge of Python data types such as lists, dictionaries, and tuples, leveraging them to solve Data Science problems.

How do you convert a number to base 3?

The base 3, or ternary, system, uses only the digits 0,1, and 2. For each place, instead of multiplying by the power of 10, you multiply by the power of 3. For example, 120123→1×34+2×33+0×32+1×31+2.

How do you convert a number to a base in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in the case of binary numbers.

How do you change from base 2 to base 3?

If you want to go directly from base 2 to base 3, then it can be a little complicated. The "usual" way is to convert to base 10, then to base 3. It's always possible to convert a terminating base 2 number to a terminating base 10 number (this is because 10 is divisible by 2).

How do I convert a string to 3 in Python?

We can convert numbers to strings using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.