Numbers in Python – Detailed instructions From [A-Z]

We’ll examine how to use numbers in Python in this tutorial. Python is compatible with complex numbers, floats, and integers.

A number without a decimal point, such as 5, 6, 10, etc., is referred to as an integer.

A float is a decimal number, such as 6.7, 6.0, 10.99, etc.

A complex number, such as 7+8j, 8+11j, etc., has real and imaginary components.

Example: Python numbers

# Python program to display numbers of
# different data types

# int
num1 = 10
num2 = 100

# float
a = 10.5
b = 8.9

# complex numbers
x = 3 + 4j
y = 9 + 8j



An example using Python to determine a number’s class (data type)

The type() function can be used to determine a number’s class. A complex number belongs to the complex class, a float number to the float class, and an integer to the int class.

# program to find the class of a number

# int
num = 100
print("type of num: ",type(num))

# float
num2 = 10.99
print("type of num2: ",type(num2))

# complex numbers
num3 = 3 + 4j
print("type of num3: ",type(num3))

Python numbers data type

The method isinstance()

The isinstance() function determines whether a number is an instance of a certain class and then returns true or false depending on the outcome.
For instance:
If the number num is an integer number, the function isinstance(num, int) will return true.
If num is not an integer number, the isinstance(num, int) function will return false.

Example of the function “isinstance()”

num = 100
# true because num is an integer
print(isinstance(num, int))

# false because num is not a float
print(isinstance(num, float))

# false because num is not a complex number
print(isinstance(num, complex))



Related Articles

Leave a Reply

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

Back to top button