How do I get int literal attribute instead of SyntaxError in Python?


To get int literal attribute instead of SyntaxError, use a space or parenthesis. The int literal is a part if Numeric Literals in Python. Numeric Literals also includes the following four different numerical types −

  • int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

  • float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.

Let’s see why and what is the syntax error discussed in this question −

SyntaxError: Invalid Decimal Literal

In this example, you can see we will get the invalid decimal literal syntax error −

print(5) print(5.__class__)

Output

The output shows a syntax error


Let’s see how to fix it −

The int literal attribute

Example

This is how we can work around the int literal with space or parenthesis to fix the error −

print(5) print(5 .__class__) print((5).__class__)

Output

5
<class 'int'>
<class 'int'>

Example

There’s another example as well −

a = 7 print(a) print(a .__class__) print((a).__class__)

Output

7
<class 'int'>
&t;class 'int'>

Updated on: 19-Sep-2022

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements