Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - NUMBERS


As we have seen in the 'Data Types' topic, there are three types of Numbers supported by Python.


  1. 'int' Data Type that supports integers

    The 'int' Data Type supports, a whole number(that can be positive or negative) of indefinite length.

    Example :



    x = 5
    y = -14
    z = 4356674543356
    print(type(x)) 
    print(type(y))
    print(type(z))
        


    Output :




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


    So, if you see the above code, the variable 'x' is holding a positive number (i.e. '5'),

    x = 5
        


    The variable 'y' is holding a negative number (i.e. '-14')

    y = -14
        


    And the variable 'z' is holding a very large value (i.e. '4356674543356').

    z = 4356674543356
            


    Now, if you see the Data Type of all the variables,

    print(type(x)) 
    print(type(y))
    print(type(z))
        


    They are if 'int' type.

    <class 'int'>
        
  2. 'float' Data Type that supports floating point numbers

    The 'Data Type' for a floating point number is 'float'.

    Example :



    x = 5.987
    print(type(x))
        


    Output :




       float
  3. 'complex' Data Type that supports data which has a real and and imaginary part.

    In Mathematics, you can find lots of formulas where there is a real part and an imaginary part.

    Say, for example

    x = 2 + 5j


    Where '2' is the real part and the number '5'(i.e. Of '5j') is the imaginary part.

    Example :



    x = 2 + 5j
    print(type(x)) 
        


    Output :




      complex


    So, in the expressions,

    y = 2 + 5j


    Is of 'complex' Data Types.

    Now, if you want to check the real and imaginary part of '2 + 5j'.

    Let us see with the below example,

    Example :



    x = 2 + 5j
    print(x.real)
    print(x.imag) 
        


    Output :




      2.0
      5.0


    So, we have used the 'x.real',

    print(x.real)


    And, x.imag,

    print(x.imag)


    In the print statement and got the real number (i.e. '2.0') and the imaginary number (i.e. '5.0') in the output.