Everything About Python Numeric Data Types: Beginner’s Guide | by Misha Sv | Dec, 2022
In this article we will explore Python numeric data types.
Table of Contents
- Introduction
- Integers
- Floating point numbers
- Complex numbers
- Numerical operations
- Examples
- Conclusion
In Python, there are three distinct numeric types: integers, floating point numbers, and complex numbers. And numbers are created by numeric literals or as the result of built-in functions and operators.
Learning data types in each programming language is essential to understand the code and programs.
Numeric data types are widely used in many mathematical, statistical, data science, and machine learning solutions built in Python.
Python int (integer) numeric type represents an integer which is a whole number, it can be positive or negative, and it can be of unlimited length.
The int() constructor can be used to create an integer number in Python.
Consider the following examples:
a = 1
b = -2
c = int(5)
d = int('6')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'int'
class 'int'
class 'int'
class 'int'1 -2 5 6
Python float (floating point number) numeric type represents a number that contains one or more decimals, it can be positive or negative (including infinity).
The float() constructor can be used to create a floating point number in Python.
Consider the following examples:
a = 1.0
b = -1.10
c = float('5.5')
d = float('-inf')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'float'
class 'float'
class 'float'
class 'float'1.0 -1.10 5.5 -inf
Python complex (complex number) numeric type represents a complex number that contains one real and one imaginary parts, and is constructed from two real numbers.
The complex() constructor can be used to create a complex number in Python.
Consider the following examples:
a = 1+3j
b = -2+5j
c = complex(3,-7)
d = complex('1')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'complex'
class 'complex'
class 'complex'
class 'complex'(1+3j) (-2+5j) (3-7j) (1+0j)
All numeric types support the following operations:
Knowing numeric data types and their properties is essential for operations
Addition operation
Performing addition of integers will result in integer type output:
a = 1
b = 2c = a+b
print(c)
#Output: 3
Performing addition of integer and float will result in float type output:
a = 1
b = 2.0c = a+b
print(c)
#Output: 3.0
Subtraction operation
Similar to addition operation, performing subtraction of integer from integer will result in integer type output:
a = 5
b = 3c = a-b
print(c)
#Output: 2
and performing subtraction of float from integer will result in float type output:
a = 5
b = 3.0c = a-b
print(c)
#Output: 2.0
You can also subtract a negative number which will result in an addition operation:
a = 3
b = -6#Operation: 3 - (-6)
c = a-b
print(c)
#Output: 9
Multiplication operation
Performing multiplication of integers will result in integer type output:
a = 5
b = 2c = a*b
print(c)
#Output: 10
Performing multiplication of integer by float will result in float type output:
a = 5
b = 2.0c = a*b
print(c)
#Output: 10.0
Division operation
Performing division of integers will result in float type output:
a = 9
b = 3c = a/b
print(c)
#Output: 3.0
Performing division of integer by float will result in float type output:
a = 9
b = 3.0c = a/b
print(c)
#Output: 3.0
In this article we explored Python numeric data types including integers, floating point numbers, and complex numbers.
As your next step in learning Python consider reading about Python data structures in the following articles:
Originally published at https://pyshark.com on December 12, 2022.
In this article we will explore Python numeric data types.
Table of Contents
- Introduction
- Integers
- Floating point numbers
- Complex numbers
- Numerical operations
- Examples
- Conclusion
In Python, there are three distinct numeric types: integers, floating point numbers, and complex numbers. And numbers are created by numeric literals or as the result of built-in functions and operators.
Learning data types in each programming language is essential to understand the code and programs.
Numeric data types are widely used in many mathematical, statistical, data science, and machine learning solutions built in Python.
Python int (integer) numeric type represents an integer which is a whole number, it can be positive or negative, and it can be of unlimited length.
The int() constructor can be used to create an integer number in Python.
Consider the following examples:
a = 1
b = -2
c = int(5)
d = int('6')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'int'
class 'int'
class 'int'
class 'int'1 -2 5 6
Python float (floating point number) numeric type represents a number that contains one or more decimals, it can be positive or negative (including infinity).
The float() constructor can be used to create a floating point number in Python.
Consider the following examples:
a = 1.0
b = -1.10
c = float('5.5')
d = float('-inf')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'float'
class 'float'
class 'float'
class 'float'1.0 -1.10 5.5 -inf
Python complex (complex number) numeric type represents a complex number that contains one real and one imaginary parts, and is constructed from two real numbers.
The complex() constructor can be used to create a complex number in Python.
Consider the following examples:
a = 1+3j
b = -2+5j
c = complex(3,-7)
d = complex('1')print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a, b, c, d)
Output:
class 'complex'
class 'complex'
class 'complex'
class 'complex'(1+3j) (-2+5j) (3-7j) (1+0j)
All numeric types support the following operations:
Knowing numeric data types and their properties is essential for operations
Addition operation
Performing addition of integers will result in integer type output:
a = 1
b = 2c = a+b
print(c)
#Output: 3
Performing addition of integer and float will result in float type output:
a = 1
b = 2.0c = a+b
print(c)
#Output: 3.0
Subtraction operation
Similar to addition operation, performing subtraction of integer from integer will result in integer type output:
a = 5
b = 3c = a-b
print(c)
#Output: 2
and performing subtraction of float from integer will result in float type output:
a = 5
b = 3.0c = a-b
print(c)
#Output: 2.0
You can also subtract a negative number which will result in an addition operation:
a = 3
b = -6#Operation: 3 - (-6)
c = a-b
print(c)
#Output: 9
Multiplication operation
Performing multiplication of integers will result in integer type output:
a = 5
b = 2c = a*b
print(c)
#Output: 10
Performing multiplication of integer by float will result in float type output:
a = 5
b = 2.0c = a*b
print(c)
#Output: 10.0
Division operation
Performing division of integers will result in float type output:
a = 9
b = 3c = a/b
print(c)
#Output: 3.0
Performing division of integer by float will result in float type output:
a = 9
b = 3.0c = a/b
print(c)
#Output: 3.0
In this article we explored Python numeric data types including integers, floating point numbers, and complex numbers.
As your next step in learning Python consider reading about Python data structures in the following articles:
Originally published at https://pyshark.com on December 12, 2022.