Numbers

There are 3 types :

  1. Integer

  2. Float

  3. Complex

    1. Integer(int) : Integer is a non decimal number formed by the combination of 0 - 9 digts.

    2. Float(float) : Float is an decimal number that can be represented on number line.

    3. Complex(complex) : They are the numbers consist of an imaginary number and real number.

    We can use the type() function to know which class(int,float,complex) a variable or a value belongs to!!

Example - 1:

x = input("Enter any number :  ") 
#input() function is used to have input on console.
y = input("Enter any number : ")
z = input("Enter any number : ")
print(type(x)) 
#type(object) function returns the type of object.
print(type(y)) 
#print() is a function that outputs to your console window.
print(type(z))

Enter any number : 3

Enter any number : 4.5

Enter any number : 3+6j

<class 'str'>

<class 'str'>

<class 'str'>

Initially every value is a string in python so to convert them in int, float, complex we use specific functions for each conversion.

  1. For integer we use int()

  2. For float we use float()

  3. For complex we use complex()

Example - 2:

#id(object) As we can see the function. 
#Accepts a single parameter and is used to return the identity of an object. 
#This identity has to be unique and constant for this object during the lifetime. 
#Two objects with non-overlapping .
#lifetimes may have the same id() value.
#int
x = int(input("Enter a number : "))
y = int(input("Enter a number : "))
print("Type of X : ",type(x)) 
print("Type of Y : ",type(y))
print("Value of X : ",x)
print("Value of Y : ",y)
print("Id of X : ",id(x))
print("Id of Y : ",id(y))
x = 6
print("x = ",x,"y = ",y)
print("Id of X : ",id(x))
print("Id of Y : ",id(y))

Enter a number : 3

Enter a number : 4

Type of X : <class 'int'>

Type of Y : <class 'int'>

Value of X : 3

Value of Y : 4

Id of X : 1487936480

Id of Y : 1487936496

x = 6 y = 4

Id of X : 1487936528

Id of Y : 1487936496

Example - 3:

x = float(input("X : "))
y = float(input("Y : "))
print("X = ",x)
print("Type(x) = ",type(x))
print("Y = ",y)
print("Type(y) = ",type(y))

X : 6.789654321

Y : 876543.098765432

X = 6.789654321

Type(x) = <class 'float'>

Y = 876543.098765432

Type(y) = <class 'float'>

Example - 4:

#complex
x = 3+5j #here 3 is real and 5 is imaginary known as iota 
print("Type of X : ",type(x)) #The output of inner print function is the input of outer print function
# So The overall output is "none" 
print("Value of X : ",x)

Type of X : <class 'complex'>

Value of X : (3+5j)

Example - 5:

x = 7489231752389582589023580265786238589275890237472895789237892374899734
print(x)
7489231752389582589023580265786238589275890237472895789237892374899734

Last updated