Numbers in Python [Explained]

Number types in Python

In Python, there are three number types:

  • int: integer, is a whole number, without decimals (positive or negative).
  • float: is a number, containing one or more decimals (positive or negative).
  • complex: Complex numbers are written with a “j” as the imaginary part.


x = 5    # int
y = 10.1  # float
z = 3 + 1j   # complex

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


Output:

<class 'int'>
<class 'float'>
<class 'complex'>



Number Data Type Conversion

x = 8    # int
y = 10.5  # float
z = 6+2j   # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

Output:

8.0
10
(8+0j)




print(type(a))
print(type(b))
print(type(c))

Output:

<class 'float'>
<class 'int'>
<class 'complex'>



https://www.w3schools.com/python/python_numbers.asp

Bilel A

Bilel A

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Learn Duty
0
Would love your thoughts, please comment.x
()
x