Followers

Latest techys

Part5- Python Fundamentals | Start from the basics



If u fell any difficulty in watching the content, try to reduce the size of this page or rotate your portable device if you are using it



In the previous article we learnt about string types and size of string. Today we shall learn about Numeric Literals. So let's go.



There are three types of  numeric literals in Python. They are:

    1. int (signed integers): They are often called just integers or ints, are positive or negative whole                                                   numbers. An integer literal must have at least one digit. It can contain (+) or(-) signs but commas(,) are not allowed.

  *Decimal points or fractional values are not allowed for int keyword when used.

             Python allows three types of integer literals:

                a) Decimal Integer Literals: An integer literal consisting of a sequence of digits of numbers except the digit zero. For example: 123, +25, -53, etc.

                

                b) Octal Integer Literal: It is a sequence of digits starting with 0o (digit zero followed by letter o). For example: Decimal number 8 will be written as 0o10 and decimal number 13 as 0o15.

    *An octal value can only contain digits from 0 to 7, 8 & 9 are said to be invalid digits in octal number i.e. 0o94, 0o68, etc. are invalid octal values as they contains numbers 8 & 9.


                c) Hexadecimal Integer Literals: These are a sequence of digits preceded by 0x or 0X.

For example: The number 12 will be written as 0XC. A hexadecimal number contains numbers from 0 to 9 and A to F only i.e. 0XE4, 0xPQR, etc. are said to be invalid hexadecimal literals as they contain values other than A to F.



    2. Floating Point Literals: These are also called real literals i.e. literals with fractional parts.

        There are two types of floating point literals:

                a) Fractional Form: A  real literal in fractional form consists of signed or unsigned digits along with a decimal point in between the digits.

    *The rule of writing a real literal in fractional form is that it must have at least one digit with the decimal point either before or after. It may also have either + or - sign preceding it. If no sign is alloted then it will be considered as a positive value.


Valid real literals: 7.0, +25.5, -99.9, 0.0000129, etc.

Invalid real literals:  2(no decimal point), +27/4(/- illegal symbol), 1256.54.87(two decimal points), 654,45.67(comma not allowed), etc.

            b) Exponent Form:  A real literal in exponent form consists of two parts namely mantissa & exponent. For example: 7.6 can be written as 0.76x101 =0.76 E01 where 0.76 is the mantissa part(part appearing before E) & 1 is the exponent part(part appearing after E) and E01 represents 101

    *The rule for writing a real literal in exponent form is that the mantissa must be either integer or a proper real constant and it should be followed by the letter E or e and the exponent. An exponent must have an integer.


Valid real literals: 152E05, 7.52E04, .35E-4, etc.

           *Even if there is no preceding or following digit of a decimal point, Python 3.x will consider it right. 

Invalid real literals: 1.7E(no digit specified for exponent), 0.76E1.5(exponent cannot be fractional), 765,875E07(comma is not allowed).



    3. Complex Number: Mathematically, a complex number is a number of the form A+Bi where i is the imaginary number equal to square root of -1 i.e. √-1.  

A complex number is made up of two parts namely real and imaginary parts. Ais called the real part and Bi is called the imaginary part where A & B are real numbers and i is an imaginary number.



We shall learn about some other literals in the next article.












No comments