Python Numeric literals: Integer, floating point, Imaginary literals

In this post we will discuss the Python numeric literals and under this literal, there are three types of literals: Python integer literals, Python floating point literals and Python imaginary literals (complex number with 0.0 real value).

Link : Python string literals

Before we discuss the three types, a small note on numeric literals. Numeric literals are those that are initialized to numeric data type object. All forms of non-negative numbers including integer number, real number, complex number, hexadecimal, binary number and octal number belong to numeric literals.

There are no sign number in numeric literals. A phrase such as -100 is not a numeric literal, they are a combination of the numeric literal 100 and the operator sign -ve.


Python integer literals

Any non-negative literals of the type integer, decimal, hexadecimal, octal and binary literals belong to integer numeric literals. Some example of integer literals are: 121(integer) , 100111(binary), 0xdeaebf(hexadecimal), 0o561(octal).

Link :Python integer data type

Some points to note:
 
i)There is no limit on the number of digits an integer literal can have.
 
ii)An underscore can occur beteen the digits. While evaluating the underscores are ignored.

>>> In=569_91_5
>>> In
569915
>>> hex=0x5_1ab4
>>> hex
334516
>>> oct=0o12671
>>> oct
5561
>>> bin=0b011_111_000
>>> bin
248

iii)Any zero before the non-zero decimal number is not allowed.

>>> num=0199
  File "<stdin>", line 1
    num=0199
           ^
SyntaxError: invalid token

We get ‘SyntaxError’ if any leading zero is found.



Python floating point literals

Any non-negative real number with fraction belong to floating point literals.

Link : Pyhton flaoting point data type

A floating point literals can also have an exponent form represented by the ‘e‘ character. Unlike the integer literals, they can have leading zero. Underscore are also allowed in floating point literals.

>> fl=0332.0919
>>> fl
332.0919
>>> fhk=0.277e3
>>> fhk
277.0
>>> hj=90.918_718_901
>>> hj
90.918718901
>>> f=0e0
0.0

Python imaginary literals

Imaginary literals have the form :
 
(integer literal or floating point literal) + ‘j’ ot ‘J’

The ‘j’ or ‘J’ character is the signature of the imaginary literals, without it the literal is reduced to either an integer or the floating point literal.

In imaginary literals all the rules of the floting point literals and the integer literals are applicable. Some instances of imaginary literals are shown below.

>>> im1=23j
>>> im2=0.56J
>>> im3=0.123e2J
>>> im4=0.56_77J
>>> im5=0917J

Note an imaginary literals is basically a complex number with 0.0 as the real value. To convert any imaginary literal to a complex number add a non-zero floating point literal as the real part.

Link : Python complex number




Leave a Reply

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