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

Enthusiastic Network Engineer specializing in Cisco ACI, passionate about solving challenges. A lifelong learner who loves gaining and sharing knowledge. Profile: https://www.linkedin.com/in/bilel-ameur-71116b2b5
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x