Home »
Python
Python Numeric Types
Python numeric types: In this tutorial, we are going to learn about the various Python numeric data types with examples – that can be used to store the various types of numeric values.
Submitted by Aditi Ankush Patil, on May 10, 2020
In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform.
The built-in data types in Python are
- Text Type
- Numeric Type
- Mapping Type
- Sequence Type
- Set Type
- Boolean Type
- Binary Type
In this tutorial, we are going to learn about the various numeric types in Python with examples.
To store numeric values, we need specific numeric data types. Python has some of the data types to define numeric values – these numeric data types can be used to store various numeric values.
There are 3 numeric data types in Python:
- int
- float
- complex
1) int
Integer numeric type is used to store signed integers with no decimal points, like -5, 2, 78, etc.
Example:
# Assigning integer values to Variables
x = 8
y = -9
# Manipulating the value of x
x = x + y
# Prints the updated value of x
print("x= ", x)
# Prints the Data Type of x
print("Data type of x: ", type(x))
Output
x= -1
Data type of x: <class 'int'>
2) float
Float numeric type is used to store floating-point values like 6.66, 58.9, 3.14, etc. Floats can also be in scientific notation, with E or e indicating the power of 10 (3.6e3 = 3.6x 103 = 3600).
Example:
# Assigning float values to Variables
x = 8.9
y = -9.1
# Manipulating the value of x
x = x - y
# Prints the updated value of x
print("x= ", x)
# Prints the Data Type of x
print("Data type of x: ", type(x))
Output
x= 18.0
Data type of x: <class 'float'>
3) complex
Complex numeric type is used to store complex numbers like 3.14j, 8.0 + 5.3j, 2+6j etc.
Format: Real + Imaginary component j
Note: The imaginary component of a complex number must be denoted by j. If we denote it using i it is considered as invalid syntax in Python.
Example:
# Assigning complex values to Variables
x = 1+8.5j
y = 4+9j
# Manipulating the value of x
x = x + y
# Prints the updated value of x
print("x= ", x)
# Prints the Data Type of x
print("Data type of x: ", type(x))
Output
x= (5+17.5j)
Data type of x: <class 'complex'>
Examples demonstrating all numeric data types
Let's look at some simple Python programs to demonstrate the numeric data types:
Note: type() is a function used to determine the type of a variable
Example 1: Program to print the data types of variables
# Assigning Values to Variables
a = 10
b = -1
c = 15.9
d = 6 + 8j
# Printing data type of the Variables
print("Data Type of a: ", type(a))
print("Data Type of b: ", type(b))
print("Data Type of c: ", type(c))
print("Data Type of d: ", type(d))
Output
Data Type of a: <class 'int'>
Data Type of b: <class 'int'>
Data Type of c: <class 'float'>
Data Type of d: <class 'complex'>
Example 2: Program to add complex numbers to integer and float type numbers
a = 2 + 9j # complex number
b = 2.8 # floating point number
c = 9 # integer
# addition of complex and integer
comp_plus_int = a + c
# addition of complex and float
comp_plus_float = a + b
# Printing result of addition and
# datatype of the result
print("Sum of complex and integer values= ",comp_plus_int)
print("Data Type After addition: ", type(comp_plus_int))
print()
print("Sum of complex and float values= ", comp_plus_float)
print("Data Type After addition: ", type(comp_plus_float))
Output
Sum of complex and integer values= (11+9j)
Data Type After addition: <class 'complex'>
Sum of complex and float values= (4.8+9j)
Data Type After addition: <class 'complex'>
Python Tutorial
ADVERTISEMENT
ADVERTISEMENT