Numerical data types in Python

In this article, we will be learning how to work with numerical data types in Python. Like every other programming language, python provides support for working with different types of numerical data types including integers, floats and complex numbers.

Numerical Types in Python

In Python, there are three distinct numeric data types and they include:

  • Integers
  • Floating point numbers
  • Complex numbers

Integers

Integers are simply whole numbers. Integers can be positive or negative but they do not have fractional or decimal parts.

Numbers like 1, 777, -101 and even 0 are Integers. However, integers are not only numbers in base 10 but also include whole numbers in other number systems like – binary, octal or hexadecimal.

Now, let’s briefly explain the important number systems that you may likely encounter while working with integers in Python.

Binary Numbers

A binary number is a number expressed in base 2 or a binary number system. Binary numbers have two digits – 0 and 1.

Octal Numbers

Octal numbers are numbers expressed in base 8 or octal number system. In this number system, the digits range from 0 to 7.

Decimal Numbers

These are numbers expressed in base 10. Numbers in base 10 consist of 10 digits with 0 being the smallest and 9 being the largest.

Hexadecimal Numbers

Hexadecimal numbers are numbers in base 16. They have 16 digits with 0 being the smallest and F being the largest. The digit F is equivalent to 15 in the decimal number system.

The table below presents the digits available in the binary, octal, decimal and hexadecimal number systems.

Binary Octal Decimal Hexadecimal
0 0 0 0
1 1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8
9 9
A
B
C
D
E
F

 

Floating point numbers

Floating point numbers are numbers that contain a decimal point. Also, floating point numbers can be positive or negative.

The following numbers are examples of floating point numbers:

  • 0.004
  • -1.50
  • 23.00045

Complex Numbers

Just like in mathematics, complex numbers have real and imaginary parts. For example, 2 + 5j is a complex number with 2 being the real part and 5 being the imaginary part.

With Python, you can easily extract the real and imaginary parts using the real and imag attributes of the number as shown below:


number = 2 + 5j

print(number.real)

print(number.imag)

#outputs

# 2.0

# 5.0

Arithmetic Operations

To perform arithmetic calculations with the numerical data types, arithmetic operators are used.

These operators include:

Addition +

Subtraction –

Multiplication *

Division /

Floor Division //

Modulus %

Exponentiation **

Unary Minus –

Unary Plus +


num1 = 20

num2 = 55

#Addition

print(num1 + num2)

# Subtraction

print(num1 - num2)

#Multiplication

print(num1 * num2)

#Division

print(num1 / num2)

#floor Division - removes the decimal points

print(num1 // num2)

#modulus or remainder

print(num1 % 2)

#Exponentiation

print(2 ** 3)

#Unary Plus

print(+num1)

#Unary Minus

print(-num1)

#outputs

# 75

# -35

# 1100

# 0.36363636363636365

# 0

# 0

# 8

# 20

# -20

Calculations involving Integers and Floats

If you are performing calculations involving Integer and floating point numbers, the outcome is usually a float.


num1 = 20

num2 = 5.5

#Addition

print(num1 + num2)

# Subtraction

print(num1 - num2)

#Multiplication

print(num1 * num2)

#Division

print(num1 / num2)

#outputs

# 25.5

# 14.5

# 110.0

# 3.6363636363636362

Comparing Numbers

You can perform comparisons between numbers using relational operators as shown below.

Greater than >

Less than <

Greater or Equal to >=

Less or Equal to <=

Equal to ==

Not Equal to !=


num1 = 20

num2 = 15

#Greater than

print(num1 > num2)

#Less than

print(num1 < num2)

#Greater or equal to

print(num1 >= num2)

#Less or equal to

print(num1 <= num2)

#Equal to

print(num1 == num2)

#Not equal to

print(num1 != num2)

#outputs

# True

# False

# True

# False

# False

# True

Number Type Conversion

You can convert numbers or even strings to a given numerical type using the following functions

  • Int()
  • Float()
  • complex()

num = 20

int_num = int(num)

print(int_num)

float_num = float(num)

print(float_num)

complex_num = complex(num)

print(complex_num)

#outputs

# 20

# 20.0

# (20+0j)

Binary, Octal and Hexadecimal Conversions

Conversion using the bin, oct, hex and int methods.


num = 9

#Binary

print(bin(num))

#Octal

print(oct(num))

#Decimal

print(int(num))

#Hexadecimal

print(hex(num))

#outputs

# 0b1001

# 0o11

# 9

# 0x9

Mathematical functions

These are functions that are used to perform mathematically related operations on numbers. Some of these functions include:

  • abs()
  • pow()
  • round()
  • math.trunc()
  • math.floor()
  • math.ceil()

abs()

This function returns the absolute value of a given number. It strips off the unary signs in a number.


print(abs(-10))

print(abs(1.05))

print(abs(+0.5))

#outputs

# 10

# 1.05

# 0.5

pow()

This in-built function returns the value corresponding to the number raised to a given power. This function accepts two arguments, the first being the number that you want to exponentiate, and the second being the power.


print(pow(2,2))

print(pow(2,3))

print(pow(2,4))

#outputs

# 4

# 8

# 16

round()

This function rounds or approximates a number to the nearest whole number.


print(round(10.2))

print(round(10.5))

print(round(10.6))

print(round(10.9))

#outputs

# 10

# 10

# 11

# 11

math.trunc()

In order to use this function, you have to import the math module. This function strips off the decimal points in numbers as shown below:


import math

print(math.trunc(10.2))

print(math.trunc(10.9))

#outputs

# 10

# 10

math.floor()

This function behaves like the trunc, by stripping off the decimal part of a floating point number, returning a whole number.


import math

print(math.floor(10.2))

print(math.floor(10.9))

#outputs

# 10

# 10

math.ceil()

This function approximates a floating point number to a whole number, greater than the integer part of the number.


import math

print(math.ceil(10.2))

print(math.ceil(10.9))

#outputs

# 11

# 11

Formatting numbers

You can manipulate how numbers are presented or formatted using the format() method.


#rounding to 2 decimal places

result = '{:.2f}'.format(1.35)

print(result)

#rounding to 1 decimal place

result = '{:.1f}'.format(1.35)

print(result)

#including per cent to a number

result = '{}%'.format(1.35)

print(result)

#outputs

# 1.35

# 1.4

# 1.35%

Random Numbers

The random module is a very interesting and useful module for generating random numbers. We’ll be looking at a few of the popular methods in the random module.

randint() method

This method is used to generate random integer numbers. This method requires that you provide arguments corresponding the range of numbers that it can generate.


from random import randint

num = randint(1,5)

print(num)

random() method

This method generates floating point numbers in the range of 0 and 1.


from random import random

num = random()

print(num)

choice() method

This method returns random items from a given sequence. It requires that you provide a sequence as an argument, and it returns a single item in the sequence in a random manner.


from random import choice

L = ['apple', 'grape', 'mango', 'banana']

result = choice(L)

print(result)

randrange() method

This method behaves like the randint(), except that it allows you to specify the smallest and largest values that can be generated, allowing you to skip values within these ranges.

The syntax is as follows


randrange(start, stop, step)

from random import randrange

result = randrange(1,10,2)

print(result)

Leave a Reply

Your email address will not be published. Required fields are marked *