Python Numbers

What are the different types of Python Numbers
Author

Juma Shafara

Published

November 1, 2023

Modified

November 25, 2024

Keywords

python numbers, number types, number arithmetics, number methods

Photo by DATAIDEA

Numbers

Table of Contents

In this notebook, you will learn the following

  • 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

# 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

Below are some of the basic arithmetic operations that can be performed with numbers

# 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

# 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: ', pi) # 3.14159265358979
print('Rounded PI: ', 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

Exercise

Write a program to assign the number 64.8678 to a variable named “number” then display the number rounded to two decimal places.

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

Back to top