As the name suggests are numeric values that are used in Python. For example in C/C++/Java there are int
, float
or double types. Similarly in Python, we have three numeric data types, they are explained below.
Three numeric types in Python.
int
): Numbers that do not have decimal values. In Python, int
is also a long type and it can be of any size.float
): Numbers that do have decimal values. In Python, float
also acts as a double type as it is a double precision floating point number.complex
): Numbers that have two parts, real and imaginary. First part is a normal number, the second part is an imaginary number which should be followed by "j".## Integer
# here my_int is an operand, 42 is a literal and its data type is int
my_int = 42
print(42) # 42
print(type(my_int)) # <class 'int'>
# use a underscore to keep a big number readable in code,
# Python ignores the '_' here, so you get '5000000' as output
some_value = 50_00_000
print(some_value) # 5000000
## Float
my_float = 3.0
# also a double
my_var = 3.3333
print(my_var) # 3.3333
print(type(my_float)) # <class 'float'>
## Complex
my_complex = 4.22 + 20j
my_complex = complex(4.22, 20) # alternative way
print(my_complex) # (4.22+20j)
# returns maximum from n numbers (n >= 2)
print(max(30, 20)) # 30
# returns minimum from n numbers (n >= 2)
print(min(30, 20)) # 20
# returns absolute value of a number
print(abs(-50)) # 50
# returns a rounded to decimal value of a number
print(round(3.1)) # 3
# returns the power of a number, similar to using '**' operator, eg "10**2"
print(pow(10, 2)) # 100
# returns quotient and remainder of integer division
print(divmod(6, 4)) # (1, 2)
# convert a number to a hexadecimal number
print(hex(42)) # 0x2a
my_int = 42
my_float = 3.0
# int to str
print(str(my_int)) # 42
# int to float
print(float(my_int)) # 42.0
# float to int
print(int(my_float)) # 3
# float to str
print(str(my_float)) # 3.0