Python Numbers

What are the different types of Python Numbers
Author

Juma Shafara

Published

November 1, 2023

Keywords

python numbers

Photo by DATAIDEA

In python, there are three types of numbers

Types

Integer

An integer is a number without decimals

# Python Numbers: intgers

a = 3
b = 4
number = 5

print('a:', a)
print('b:', b)
print('number:', number)
a: 3
b: 4
number: 5

Floating Point

A floating point number of just a float is a number with decimals

# Python Numbers: floating point
a = 3.0
b = 4.21
number = 5.33

print('a:', a)
print('b:', b)
print('number:', number)
a: 3.0
b: 4.21
number: 5.33

Complex

A comple number is an imaginary number. To yield a complex number, append a j o J to a numeric value

# Python Numbers: complex

a = 3j
b = 5.21j
number = 4 + 5.33j

print('a:', a)
print('b:', b)
print('number:', number)
a: 3j
b: 5.21j
number: (4+5.33j)

Number Arthmetics

# Python Numbers: arthmetics

summation = 4 + 2
print('sum:', summation)

difference = 4 - 2
print('difference:', difference)

product = 4 * 2
print('product:', product)

quotient = 4 / 2
print('quotient:', quotient)
sum: 6
difference: 2
product: 8
quotient: 2.0

Number Methods

Number methods are special functions used to work with numbers

# sum() can add many numbers at once
summation = sum([1,2,3,4,5,6,7,8,9,10])
print(summation) # 55
55
# round() rounds a number to a 
# specified number of decimal places
pi = 3.14159265358979
rounded_pi = round(pi, 3)

print(pi) # 3.14159265358979
print(rounded_pi) # 3.142
pi: 3.14159265358979
rounded_pi: 3.142
# abs() returns the absolute value of a number
number = -5
absolute_value = abs(number)
print(absolute_value) # 5
absolute value of -5 is 5
# pow() returns the value of 
# x to the power of y
four_power_two = pow(4, 2)
print(four_power_two) # 16
16
# divmod() returns the quotient and 
# remainder of a division
quotient, remainder = divmod(10, 3)
print(quotient) # 3
print(remainder) # 1
Quotient: 3
Remainder: 1

What’s on your mind? Put it in the comments!

Back to top