Member-only story

7 Strange Ways To Write Numbers In Python

Liu Zuo Lin
4 min readFeb 12, 2025

--

Apart from the usual ways of defining numbers eg. 100, 3.14159, we can write numbers in stranger ways if we want to.

1) Scientific notation

When we write 3e8, we mean 3 * 10**8. 3e8 simply gives us a nice and convenient way to not have to write so many zeros

The numbers after e can be negative. When we write 3e-3, we mean 3 * 10**-3, which is 3 * 0.001. With this, we don’t have to write as many zeros after our decimal points too.

2) Adding underscores to our numbers

^ this can get annoying to read due to how many zeros there are, especially if we’re dealing with large numbers.

To make our numbers easier to read, we can consider adding underscores in between the digits to group them together. We typically group digits into groups of 3.

300_000_000 is much more immediately readable than 300000000

3) binary and friends

To write binary numbers directly, we use 0b{binary_number}

^ but when we print this, we get the number in base 10

  • to write octal (base 8) numbers, we use o instead of b
  • to write hexadecimal (base 16) numbers, we use x

--

--

Liu Zuo Lin
Liu Zuo Lin

Written by Liu Zuo Lin

SWE @ Meta | [Ebook] 101 Things I Never Knew About Python: https://payhip.com/b/vywcf

Responses (1)