(close)

Use this link to get 70% off the Automate the Boring Stuff online video course.

Support me on Patreon

Prev: niNety nniinE BoOttels | Next: Periodic Table of the Elements
Use this link to get 70% off the Automate the Boring Stuff online video course.

Support me on Patreon

Numeral System Counters

We’re used to counting in the decimal numeral system, which uses 10 digits: 0 through 9. This system likely developed because humans counted on their fingers, and most people have 10 fingers. But other number systems exist. Computers make use of *binary*, a numeral system with only two digits, 0 and 1. Programmers also sometimes use hexadecimal, which is a base-16 numeral system that uses the digits 0 to 9 but also extends into the letters *A* to *F*.

We can represent any number in any numeral system, and this program displays a range of numbers in decimal, binary, and hexadecimal.

When you run *numeralsystems.py*, the output will look like this:

```
Numeral System Counters, by Al Sweigart [email protected]
````--snip--`
Enter the starting number (e.g. 0) > **0**
Enter how many numbers to display (e.g. 1000) > **20**
DEC: 0 HEX: 0 BIN: 0
DEC: 1 HEX: 1 BIN: 1
DEC: 2 HEX: 2 BIN: 10
DEC: 3 HEX: 3 BIN: 11
DEC: 4 HEX: 4 BIN: 100
DEC: 5 HEX: 5 BIN: 101
DEC: 6 HEX: 6 BIN: 110
DEC: 7 HEX: 7 BIN: 111
DEC: 8 HEX: 8 BIN: 1000
DEC: 9 HEX: 9 BIN: 1001
DEC: 10 HEX: A BIN: 1010
DEC: 11 HEX: B BIN: 1011
DEC: 12 HEX: C BIN: 1100
DEC: 13 HEX: D BIN: 1101
DEC: 14 HEX: E BIN: 1110
DEC: 15 HEX: F BIN: 1111
DEC: 16 HEX: 10 BIN: 10000
DEC: 17 HEX: 11 BIN: 10001
DEC: 18 HEX: 12 BIN: 10010
DEC: 19 HEX: 13 BIN: 10011

You can get the binary and hexadecimal representations of a number in Python by calling the `bin()`

and `hex()`

functions, respectively:

`>>> `**bin(42)**
'0b101010'
>>> **hex(42)**
'0x2a'

Convert these strings back into decimal integers by calling `int()`

and supplying the base to convert from, like so:

`>>> `**int('0b101010', 2)**
42
>>> **int('0x2a', 16)**
42

Keep in mind that the binary and hexadecimal “numbers” returned by `bin()`

and `hex()`

are actually string values: `bin(42)`

returns the string `'0b101010'`

and `hex(42)`

returns the string `'0x2a'`

. In programming, it is convention to add a `0b`

prefix to binary numbers and `0x`

prefix to hexadecimal numbers. That way, no one will confuse the binary number 10000 (the number 16 in decimal) with the decimal number “ten thousand.” The numeral systems program removes these prefixes before displaying the number.

```
1. """Numeral System Counters, by Al Sweigart [email protected]
2. Shows equivalent numbers in decimal, hexadecimal, and binary.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: tiny, math"""
5.
6.
7. print('''Numeral System Counters, by Al Sweigart [email protected]
8.
9. This program shows you equivalent numbers in decimal (base 10),
10. hexadecimal (base 16), and binary (base 2) numeral systems.
11.
12. (Ctrl-C to quit.)
13. ''')
14.
15. while True:
16. response = input('Enter the starting number (e.g. 0) > ')
17. if response == '':
18. response = '0' # Start at 0 by default.
19. break
20. if response.isdecimal():
21. break
22. print('Please enter a number greater than or equal to 0.')
23. start = int(response)
24.
25. while True:
26. response = input('Enter how many numbers to display (e.g. 1000) > ')
27. if response == '':
28. response = '1000' # Display 1000 numbers by default.
29. break
30. if response.isdecimal():
31. break
32. print('Please enter a number.')
33. amount = int(response)
34.
35. for number in range(start, start + amount): # Main program loop.
36. # Convert to hexadecimal/binary and remove the prefix:
37. hexNumber = hex(number)[2:].upper()
38. binNumber = bin(number)[2:]
39.
40. print('DEC:', number, ' HEX:', hexNumber, ' BIN:', binNumber)
```

After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:

- Enter a new row for
*octal*, the base-8 number system, using Python’s`oct()`

function. - Search the internet for “number system conversion” to find out how to implement your own
`bin()`

,`oct()`

, and`hex()`

functions.

Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.

- What happens if you change
`hex(number)[2:].upper()`

on line 37 to`hex(number)[2:]`

? - What error do you cause if you change
`int(response)`

on line 33 to`response`

?