## What do numbers starting with 0 mean in python?

These are numbers represented in base 8 (octal numbers).

Some examples:

### Python 2 (old format)

**Note**: these forms only work on Python 2.x.

`011`

is equal to 1⋅8¹ + 1⋅8⁰ = 9,

`0100`

is equal to 1⋅8² + 0⋅8¹ + 0⋅8⁰ = 64,

`027`

is equal to 2⋅8¹ + 7⋅8⁰ = 16 + 7 = 23.

### Python 3 (new format)

In Python 3, one must use `0o`

instead of just `0`

to indicate an octal constant, e.g. `0o11`

or `0o27`

, etc. Python 2.x versions >= 2.6 supports both the new and the old format.

`0o11`

is equal to 1⋅8¹ + 1⋅8⁰ = 9,

`0o100`

is equal to 1⋅8² + 0⋅8¹ + 0⋅8⁰ = 64,

`0o27`

is equal to 2⋅8¹ + 7⋅8⁰ = 16 + 7 = 23.

## Python number starting with 0

A leading zero means an *octal* number, which is one that allows the digits `0`

through `7`

inclusive,

So, while `02132`

is a valid octal number, `02492`

is *not,* because it contains the non-digit (in the context of octal numbers) character `9`

.

It's no different from asking a computer to process a decimal number such as:

`num = 3v14159`

You should also be careful with things like `02132`

- it is *not* the same as the decimal number `2132`

, rather it's the octal variant `2x8`

, or ^{3} + 1x8^{2} + 3x8^{1} + 2x8^{0}`1114`

.

## Number with 0 in front of it

Literals beginning with zero in python 2 are octal. For example, octal 31 is 25 in base 10.

For a more complete answer, see What do numbers starting with 0 mean in python?

## Python cannot handle numbers string starting with 0. Why?

My guess is that since `012`

is no longer an octal literal constant in python3.x, they disallowed the `012`

syntax to avoid strange backward compatibility bugs. Consider your python2.x script which using octal literal constants:

`a = 012 + 013`

Then you port it to python 3 and it still works -- It just gives you `a = 25`

instead of `a = 21`

as you expected previously (decimal). Have fun tracking down that bug.

## leading zeros in python

Numbers with a leading zero in them are interpreted as octal, where the digits `8`

and `9`

don't exist.

It's worse in Python 3, leading zeros are a syntax error no matter which digits you use. See What’s New In Python 3.0 under "New octal literals". Also PEP 3127.

## Why leading zero not possible in Python's Map and Str

This happens because the leading zero means you are writing an octal number and you can't have 9 or 8 in an octal number. See:

`>>> a = 0123`

>>> a

83

>>> a = 010

>>> a

8

You can just do:

`>>> map(int, '08978789787')`

[0, 8, 9, 7, 8, 7, 8, 9, 7, 8, 7]

### Related Topics

What Is the Fastest Way to Stack Numpy Arrays in a Loop

Spark Equivalent of If Then Else

Check Json Data Is None in Python

Convert Regular Python String to Raw String

Finding the Index of the First Occurrence of Any Item in a List

Using Regex to Get the Value Between Two Characters (Python 3)

How to Stop a Running Function Without Exiting the Tkinter Window Entirely

How to Get Interactive Plots Again in Spyder/Ipython/Matplotlib

Matplotlib: Drawing Lines Between Points Ignoring Missing Data

How to Increase the Font Size of the Legend in My Seaborn Factorplot/Facetgrid

How to Remove the Decimal Point in a Pandas Dataframe

Add N Empty Rows in a Dataframe

How to Display Last 2 Digits from a Number in Python

Pandas Convert from Datetime to Integer Timestamp