03 numbers
title: Python Numbers author: Juma Shafara date: "2023-11" date-modified: '2024-11-25' description: What are the different types of Python Numbers keywords: [python numbers, number types, number arithmetics, number methods]¶

In python, there are three types of numbers
- Integer - `int`
- Floating Point - `float`
- Complex - `complex`
Types¶
Integer¶
An integer is a number without decimals
In [ ]:
Copied!
# Python Numbers: intgers
a = 3
b = 4
number = 5
print('a:', a)
print('b:', b)
print('number:', number)
# 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
In [ ]:
Copied!
# Python Numbers: floating point
a = 3.0
b = 4.21
number = 5.33
print('a:', a)
print('b:', b)
print('number:', number)
# 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
In [ ]:
Copied!
# Python Numbers: complex
a = 3j
b = 5.21j
number = 4 + 5.33j
print('a:', a)
print('b:', b)
print('number:', number)
# 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¶
Below are some of the basic arithmetic operations that can be performed with numbers
In [ ]:
Copied!
# 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)
# 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 inbuilt functions used to work with numbers
In [ ]:
Copied!
# sum() can add many numbers at once
summation = sum([1,2,3,4,5,6,7,8,9,10])
print(summation) # 55
# sum() can add many numbers at once
summation = sum([1,2,3,4,5,6,7,8,9,10])
print(summation) # 55
55
In [2]:
Copied!
# round() rounds a number to a
# specified number of decimal places
pi = 3.14159265358979
rounded_pi = round(pi, 3)
print('PI: ', pi) # 3.14159265358979
print('Rounded PI: ', rounded_pi) # 3.142
# round() rounds a number to a
# specified number of decimal places
pi = 3.14159265358979
rounded_pi = round(pi, 3)
print('PI: ', pi) # 3.14159265358979
print('Rounded PI: ', rounded_pi) # 3.142
PI: 3.14159265358979 Rounded PI: 3.142
In [ ]:
Copied!
# abs() returns the absolute value of a number
number = -5
absolute_value = abs(number)
print(absolute_value) # 5
# abs() returns the absolute value of a number
number = -5
absolute_value = abs(number)
print(absolute_value) # 5
absolute value of -5 is 5
In [ ]:
Copied!
# pow() returns the value of
# x to the power of y
four_power_two = pow(4, 2)
print(four_power_two) # 16
# pow() returns the value of
# x to the power of y
four_power_two = pow(4, 2)
print(four_power_two) # 16
16
In [ ]:
Copied!
# divmod() returns the quotient and
# remainder of a division
quotient, remainder = divmod(10, 3)
print(quotient) # 3
print(remainder) # 1
# divmod() returns the quotient and
# remainder of a division
quotient, remainder = divmod(10, 3)
print(quotient) # 3
print(remainder) # 1
Quotient: 3 Remainder: 1
Exercise¶
Write a program to assign the number 64.8678 to a variable named “number” then display the number rounded to two decimal places.