Photo by DATAIDEA

Numbers

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

# By Juma Shafara

# 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
# round() rounds a number to a specified number of decimal places
pi = 3.14159265358979
rounded_pi = round(pi, 3)

print('pi:', pi)
print('rounded_pi:', rounded_pi)
pi: 3.14159265358979
rounded_pi: 3.142
# abs() returns the absolute value of a number
number = -5
absolute_value = abs(number)
print('absolute value of', number, 'is', absolute_value)
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
# divmod() returns the quotient and remainder of a division
# division = divmod(10, 3)
quotient, remainder = divmod(10, 3)
print('Quotient:', quotient)
print('Remainder:', remainder)
Quotient: 3
Remainder: 1

A few ads maybe displayed for income as resources are now offered freely. 🤝🤝🤝

Back to top