How to fix “SyntaxError: leading zeros in decimal integer literals are not permitted” in Python

SyntaxError: leading zeros in decimal integer literals are not permitted in python

If you are getting trouble with the error “SyntaxError: leading zeros in decimal integer literals are not permitted” in Python, keep reading our article. We will give you some methods to handle the problem.

Reason for “SyntaxError: leading zeros in decimal integer literals are not permitted” in Python

SyntaxError is one of the most common errors in the Python programming language that occurs when you use an incorrect syntax. For example, in the C programming language, you can increase an index i by syntax i++, but you will receive the error when using it like this in Python.

You will get error SyntaxError: leading zeros in decimal integer literals are not permitted when you declare an integer number with the number 0 at the beginning. The following example will show you the error.

Code:

class people:
	def __init__(self, name, age):
        self.__age = age
        self.__name = name

    def getName(self):
      	return self.__name

    def getAge(self):
      	return self.__age
  
person1 = people("John", 027)

Result:

 line 12
    person1 = people(“John”, 027)
                             ^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

The cause of the problem is that an integer that starts with 0 is declared a hex, octal or binary number. The system will be confused and give you an error message. 

Now you’ve understand the problem clearly. Let’s move on to discover how to solve the problem.

Solution to this problem

Remove zero number

The simplest way to fix the error is to remove the number ‘0’. In this way, you declare an integer in the correct syntax in the Python programming language. 

Code:

class people:
    def __init__(self, name, age):
        self.__age = age
        self.__name = name

    def getName(self):
      	return self.__name

    def getAge(self):
      	return self.__age
  
person1 = people("John", 27) 
myName = person1.getName()
myAge = person1.getAge()
 
print("My name is", myName,"and I am", myAge, "years old.")
print("Welcome to Learn Share IT.")

Result:

My name is John and I am 27 years old.
Welcome to Learn Share IT.

Declare a string instead of integer number

This error only occurs if your variable has the type ‘int’. If you want your variable contains the 0 number at the beginning, you can declare the variable as a string.

Code:

class people:
    def __init__(self, name, age):
        self.__age = age
        self.__name = name

    def getName(self):
      	return self.__name

    def getAge(self):
      	return self.__age

person1 = people("John", "027")
myName = person1.getName()
myAge = person1.getAge()
 
print("My name is", myName,"and I am", myAge, "years old.")
print("Welcome to Learn Share IT.")

Result:

My name is John and I am 027 years old.
Welcome to Learn Share IT.

Declare variable as binary number

Another way to solve the problem is declare the variable as a binary number. In this example, we want to define the age as equal to 27. We all know that in the binary mode, 27 is represented as 11011. So we will declare the age equal to 0b11011. You can also create initial value in hex or octal value.

Code:

class people:
    def __init__(self, name, age):
        self.__age = age
        self.__name = name

    def getName(self):
      	return self.__name

    def getAge(self):
      	return self.__age
  
person1 = people("John", 0b11011)
myName = person1.getName()
myAge = person1.getAge()
 
print("My name is", myName,"and I am", myAge, "years old.")
print("Welcome to Learn Share IT.")

Result:

My name is John and I am 27 years old.
Welcome to Learn Share IT.

Summary

Our article has explained the cause of error “SyntaxError: leading zeros in decimal integer literals are not permitted” in Python. We also give you a few ways to fix the error. If you have any questions, please give your comments. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *