Python Numeric Data Types
Python Numeric Data Types

Python has three built-in numeric data types including Integer (int), FloatingPoint (float), and Complex (complex). These data types are used to represent numerical values and they are able to store and manipulate numbers.

The use of numerical values is widespread and essential, as they have been applied in various fields and applications, such as Data Science, Engineering, Finance, Mathematics and many more.

In This article, you will learn the three basic numeric types in Python that are commonly used in programming.

Python Int

Python integer (int) is used only to represent whole numbers without decimal points. These numbers can be positive or negative. For example, 5 or -5 is a whole number but 5.5 is not a whole number. Consider the following example to create an integer variable.

Example:


# Create integer variable
var_a = 5
var_b = 2
var_c = -25  # Give negative number

# Display their value
print(var_a)
print(var_b)
print(var_c)

# Check their data type
print(type(var_a))
print(type(var_b))
print(type(var_c))

Output:


5
2
-25

<class 'int'> <class 'int'> <class 'int'>

We use a built-in function called type() to check the data type of a variable. He takes an argument, which can be the variable or specific value you want to check and it returns the data type of that variable.

Python int() Constructor

The int() constructor is used to convert a given value to an integer data type, this creates an integer value. Consider the following example to convert values to an integer.

Example:


# Convert values to integer
var_a = 20.7
var_c = int(var_a)
var_b = int('30', base=10)

print(var_b, var_c)

print(type(var_b))
print(type(var_c))

Output:


30 20

<class 'int'> <class 'int'>

Arithmetic Operations on Integers in Python

In this section, we will focus on several arithmetic operations to perform on integers including addition, subtraction, multiplication, division, and modulus operations. This section also provides code examples to illustrate these concepts.

Addition

We perform additions on integers using the + operator. The +operator takes two operands (integer values) and returns their sum as an integer.

Example:


# Create integer variable
a = 40
b = 20

# Perform + operation
sum_1 = a + b
sum_2 = sum_1 + 10

print(sum_1)
print(sum_2)

Output:


60
70

Subtraction

Similar to the addition operation, we perform subtraction on integers using the - operator. The -operator takes two operands (integer values) and returns an integer value that represents the difference between the two operands.

Example:


# Create integer variable
a = 40
b = 20

# Perform - operation
diff = a - b

print(diff)
print(diff-10)

Output:


20
10

Multiplication

We also perform multiplication on integers using the * operator. The *operator takes two operands (integer values) and returns an integer value that represents the product of the two operands.

Example:


# Create integer variable
a = 5
b = 8

# Perform * operation
prod = a * b

print(prod)
print(prod*10)

Output:


40
400

Division

We can also perform division on integers (but not always) using the / operator. This operator takes two operands and returns a floating-point value as the result.

Note: You can ignore the floating point of the result by converting it to int. Be careful when doing this in real applications as you might lose important numbers.

Example:


# Create integer variable
a = 60
b = 5

# Perform / operation
result = a / b

print(result)
print(int(result))
print(int(result/2))

Output:


12.0
12
6

Modulus

Python and many other programming languages use the modulus operator which is denoted by the percent symbol (%). The modulus operator returns the remainder of the integer division of two numbers.

The modulus operator (%) is used to calculate the remainder after the integer division of two numbers.

The modulus operator (%) takes two operands or arguments, (in this case, an integer data type). The first operand represents the dividend (the number that will be divided), and the second operand represents the divisor (the number that will divide the first operand). The operator then returns the remainder after dividing the first operand by the second operand.

For example, in the expression 5 % 2, 5, and 2 are the two operands, and the operator % calculates the remainder when 5is divided by 2which is 1.

Example:


# Create integer variable
a = 9
b = 7

# Perform % operation
result = a % b

print(result)
print(int(result) % 2)
print(int(result % int("8")))
print(5 % 2)

Output:


2
0
2
1

Python Float

Python’s float data type is a built-in numeric data type used to represent numbers with floating points. The variable with a float data type stores a sequence of digits that can include a decimal point and an optional exponent. For example, these numbers are all float type 5.32,9.14, -0.5, or 1.23e-4.

Example:


# Create a float type variable in Python
var_float1 = 6.89
var_float2 = 4.89
var_float3 = 89.6e-4

# Display the weight and height
print(var_float1)
print(var_float2)
print(var_float3)

# Check their type
print(type(var_float1))
print(type(var_float2))
print(type(var_float3))

Output:


6.89
4.89
0.00896

<class 'float'> <class 'float'> <class 'float'>

Python float() Constructor

The float() is a built-in constructor function used to create a floating-point number object from a numeric value or a string that represents a number.

The float()function can take one argument which can be a numeric value or a string that represents a number, and returns a float object.

Here we consider some examples of the float() function which accepts different types of arguments.

Example:


# Create a float type variable in Python
var_int = 6.89
var_str = 4.89

# use the float() function in Python
var_float1 = float(var_int)
var_float2 = float(var_str)
var_float3 = float("30")
var_float4 = float(55)

# Display the weight and height
print(var_float1)
print(var_float2)
print(var_float3)
print(var_float4)
print(float("4"))

# Check their type
print(type(var_float1))
print(type(var_float2))
print(type(var_float3))
print(type(var_float4))
print(type(float("4")))

Output:


6.89
4.89
30.0
55.0
4.0

<class 'float'> <class 'float'> <class 'float'> <class 'float'> <class 'float'>

Arithmetic Operations on Floats in Python

In this section, we will focus on several arithmetic operations to perform on float numbers including addition (+)subtraction (-)multiplication (*)division (/), and modulus (%) operations. This section also provides code examples to illustrate these concepts.

Addition

We perform additions on float numbers using the + operator. The +operator takes two operands (numbers with decimal points) and returns their sum as a float.

Example:


# Create float variable
a = 20.20
b = 5.45

# Perform + operation
result = a + b

print(result)
print(float(result))
print(float("2.5") + float(result + 3.25))

Output:


25.65
25.65
31.4 

Subtraction

Similar to the addition operation, we perform subtraction on float numbers using the - operator. The -operator takes two operands (e.g. float values) and returns a float value that represents the difference between the two operands.

Note: We are talking about when all the variables and values are float type, then the return value will be in a float type. For example 2.0 - 1.0 = 1.0.

Example:


# Create float variable
a = 20.20
b = 5.45

# Perform - operation
result = a - b

print(result)
print(float(result) - float("3"))
print(float(result-6) - float("0.6"))
print(2.0 - 1.0)

Output:


14.75
11.75
8.15
1.0

Multiplication

We also perform multiplication on floats using the * operator. The *operator takes two operands and returns a float value that represents the product of the two operands.

Example:


# Create float variable
a = 5.98
b = 5.1

# Perform * operation
result = a * b

print(result)
print(float(result) * float("9"))
print(float(result * 3) * float("0.2"))
print(3.0 * 3.0)

Output:


30.498
274.482
18.2988
9.0  

Division

We can also perform division on float numbers (but not necessarily always float) using the / operator. This operator takes two operands, then divides the first operand by the second operand, and returns a floating-point value as a result.

Example:


# Create float variable
a = 35.50
b = 5.0

# Perform / operation
result = a / b

print(result)
print(float(result) / float("2"))
print(float(result / 3) / float("6.50"))
print(3.0 / 1.50)

Output:


7.1
3.55
0.3641025641025641
2.0

Modulus

We can also find the reminder when two float numbers id divided by using the modulus operator (%). This operator (%) takes two operands (in this case, a floats data type). The operator then returns the remainder after dividing the first operand by the second operand.

For example, in the expression 9.0 % 5.0, 9.0, and 5.0 are the two operands, and the operator % calculates the remainder when 9.0 is divided by 5.0 which is 4.0.

Note: Although the modulus operator is used with both integer and float numbers, however, when used with float numbers, the result is not exact, since float division can sometimes result in small rounding errors.

Example:


# Create float variable
a = 9.0
b = 5.0

# Perform % operation
result = a % b

print(result)
print(float(result) % float("2"))
print(float(result % 3) % float("6.50"))
print(3.0 % 4)

Output:


4.0
0.0
1.0
3.0

The Modulus Operator (%) is used to check whether a number is divisible by another number or not.

Python Complex

The complex data type in Python is used to represent complex numbers. These numbers consist of two parts, one is the real part and the other imaginary part.

The imaginary part must contain a Jor jcharacter as a suffix to declare it as an imaginary number. While the real part is just a basic number without any character attached.

For example, the complex number 5 + 3j. The 5 is the real part while the 3 is the imaginary part. Consider the following example, to create a variable with complex type, in Python.

Example:


# create complex variable in Python
a = 7+4j
b = 0+3J
c = 3J
d = 3+0j
e = complex(3)

print(a)
print(b)
print(c)
print(d)
print(e)


# check its type
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

Output:


(7+4j)
3j
3j
(3+0j)
(3+0j)

<class 'complex'> <class 'complex'> <class 'complex'> <class 'complex'> <class 'complex'>

In the above example, the 0+3J and 3J represent the same complex number, with a real part of 0and an imaginary part of 3. Because when you create a complex number in Python with only an imaginary part and no real part, Python assumes that the real part is zero by default.

Similarly, the 3+0j and 3 both represent the same complex number which means if you create a complex number with only a real part and no imaginary part, Python assumes that the imaginary part is 0by default. For example, 3+0j and 3both represent the same complex number with a real part of 3 and an imaginary part of 0.

Note: the Complex() function used in the above code is an alternative way to create the complex number and was discussed in the next section.

Python complex() Constructor

The complex() is a built-in constructor function used to create a complex number object. A complex number is a number that consists of a real part and an imaginary part.

The complex()function can take a number of arguments that ranges from 0 to 2 and it returns the complex number. for example, if thecomplex() function takes no arguments, it returns the complex number 0j.

Consider the following examples to learn more about thecomplex() function in Python.

Example:


# create complex variable in Python
a = complex()
b = complex(0, 0)
c = complex(5,)
d = complex(7, 3)

# Display them on the console
print(a)
print(b)
print(c)
print(d)
print(complex(10.9, 4.89))


# check its type
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(complex(10.9, 4.89)))

Output:


0j
0j
(5+0j)
(7+3j)
(10.9+4.89j)

<class 'complex'> <class 'complex'> <class 'complex'> <class 'complex'> <class 'complex'>

The above example shows alternative ways to create complex numbers using complex() functions by taking different numbers of arguments.

Arithmetic Operations on Complex in Python

Similar to any other numeric data type in Python, we can perform various arithmetic operations on complex numbers, such as additionsubtractionmultiplicationdivision, and modulus operations.

The following subsections provide practical examples to illustrate these arithmetic operations.

Addition

We perform additions on complex numbers using the + operator. The +operator takes two operands and returns their sum as a complex number. When you add two complex numbers together, Python adds their real parts separately and their imaginary parts separately to create the resulting complex number.

Example:


# create complex variable in Python
a = complex()
b = complex(0, 0)
c = complex(5,)

# Perform + operation
result = a + c

# Display them on the console
print(result)
print(result + complex(0, 20))
print(b + 3 + complex(7, 3))

Output:


(5+0j)
(5+20j)
(10+3j)

Subtraction

Similar to the addition operation, we perform subtraction on complex numbers using the - operator. The -operator takes two operands and returns a complex number that represents the difference between the two operands.

Note: In Python, the real and imaginary parts of each number are separately subtracted from one another using the - operator, resulting in a new complex number.

Example:


# create complex variable in Python
a = complex()
b = complex(60, 8)
c = complex(5,)
d = -4-5j

# Perform - operation
result = b - c

# Display them on the console
print(result)
print(result - complex(0, 20))
print(a - 2 - complex(2, 7.35))
print(d - 2+4j)

Output:


(55+8j)
(55-12j)
(-4-7.35j)
(-6-1j)

Some of the mathematical operations performed in the above example are simple, and their operations happened like this:

For example, the bvariable contains 60+8j,  so 60 is the real numbers and is the imaginary numbers (is positive) while the c variable contains 5+0j, so is the real numbers and 0 imaginary numbers (the 3 has a positive prefix but hidden). Therefore the b+c is equal to (60-5) = 55 and (8-0) = 8. The final result is 55+8j. Because the operation of each part will be conducted separately and concatenated finally.

The other operation that is worth noting is the -4-5j – 2+4j. The operation happens like this: Compute the real part (-4 – 2) = -6, then compute the imaginary part (-5-4) = 1. The rest of the arithmetic operations can be done like this.

Multiplication

We also perform multiplication on complex using the * operator. The *operator takes two operands and returns a complex number that represents the product of the two operands.

Example:


# create complex variable in Python
a = complex()
b = complex(60, 8)
c = complex(5,)
d = -4-5j

# Perform / operation
result = b * c

# Display them on the console
print(result)
print(result * complex(0, 20))
print(a * 2 * complex(2, 7.35))
print(d * 2+5j)

Output:


(300+40j)
(-800+6000j)
0j
(-8-5j)
(23+2j)

Division

We can also perform division on complex numbers using the / operator. This operator takes two operands, then divides the first operand by the second operand, and returns a complex number as a result.

Note: The division of the complex numbers is a bit difficult to understand but we explain it to you in an easy way.

Example:


# create complex variable in Python
x = complex(60, 8)
y = complex(5)

# Perform / operation and print it
print(x/y)

Output:


(12+1.6j)

Here we explain in a step-by-step breakdown of how the result (12+1.6j) was produced by the expression x/y:

  1. x = complex(60, 8) and y = complex(5) define two complex numbers, x and y. x has a real part of 60 and an imaginary part of 8, and y has a real part of 5 and an imaginary part of 0.
  2. The expression x/y performs division of x by y. Since y has an imaginary part of 0, it is effectively considered as a real number, and the division is performed using regular division for real numbers. In other words, Python divides the real part of x by the real part of y and the imaginary part of x by the imaginary part of y, which is 0.
  3. The real part of x divided by the real part of y is 60/5 or 12.0.
  4. The imaginary part of x divided by the imaginary part of y is 8/0, which is undefined. However, since the imaginary part of y is 0, we do not need to perform this division.
  5. Therefore, the result of x/y is a complex number with a real part of 12.0 and an imaginary part of 8/5, which is approximately 1.6.
  6. The final result is a complex number which is (12+1.6j).

More examples about dividing the complex number:


# create complex variable in Python
a = complex()
b = complex(60, 8)
c = complex(5,)
d = -4-5j

x = complex(4, 8)
y = complex(2, 2)

# Perform / operation
result = b / c

# Display them on the console
print(result)
print(result / complex(0, 20))
print(a / 2 / complex(2, 7.35))
print(d / 2+5j)
print(x/y)

Output:


(12+1.6j)
(0.08-0.6j)
0j
(-2+2.5j)
(3+1j)

Modulus

In Python, we cannot use the modulus operator (%) in complex numbers because it does not support. The following example tries to use the modulus operator on a complex number and it generates an error: “TypeError: unsupported operand type(s) for %: ‘complex’ and ‘complex’“.

Example:


print(complex(20, 4) % complex(4, 8))

Output:


Traceback (most recent call last):
  File "c:\complex.py", line 149, in <module>
    print(complex(20, 4) % complex(4, 8))
          ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for %: 'complex' and 'complex'

Otherwise, we use the built-in function abs().

Example:


# create complex variable in Python
a = 20 + 4j

# Perform the abs()
var_modulus = abs(a)
print(var_modulus)

Output:


20.396078054371138

Conclusion

In conclusion, Python offers several numeric data types, including int, float, and complex, which allow us to work with different kinds of numerical values. These data types can be used and performed some arithmetic operations such as addition, subtraction, multiplication, division, and modulus. In this article, we have discussed Python Numeric Data Types and practically illustrated them using Python programming. Understanding these numeric data types and their arithmetic operations is crucial for any Python programmer looking to work with numerical data effectively. In short, Python’s numeric data types make it an excellent choice for scientific computing and data analysis.

Categorized in: