Python Numbers, Type Conversion, and Mathematics

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. It is an open-source language.

Python Numbers:

There are several numeric data types in python let’s understand them one by one. In python, there is no need to define a data type of any of the variables. Once you assign a value to a particular variable it automatically detects its datatype.

E.g: A variable of numeric type will be created once you assign a value to them.

Types of numbers in Python:

  • Int
  • Float
  • Complex


Python Code

# integer type
variable1 = 5
# float type
variable2 = 3.142
# complex type
variable3 = 5j
# type() method return the type of variable
# data type of the variable 


<class ‘int’>
<class ‘float’>
<class ‘complex’>

Type conversion in python:

Sometimes we need to convert one type of data to another type to meet requirements. This is achieved with the help of type conversion. 

Type conversion is a technique of converting the value of one data type i.e.(integer, complex, float, etc.) to another data type.

Type conversion in python is divided into two parts:

  1. Implicit type conversion:- In Implicit type conversion, the python interpreter itself converts the type of a variable to another type without the user’s consent. 
  1. Explicit type conversion:- In Explicit type conversion, the user can explicitly change the data type of the variables. Python has the following built-in functions for type conversion:
  • int(): To convert any number or string of numbers to an integer.
  • float(): To convert numbers to floating-point.
  • complex(): To convert two numbers to a complex number.


Python Code

var1 = 1.2 #float
var1 = int(var1)  #convert 1.2 i.e float to integer
print(type(var1)) #integer

var2 = 1 #integer
var2 = float(var2)  #convert 1 i.e integer to floating point type
print(type(var2)) #float

var3 = 1 #integer
var4 = 2 #integer
var5 = complex(1,2) #convert 1 and 2 i.e integers into complex number
print(type(var5)) #complex


<class ‘int’>
<class ‘float’>
<class ‘complex’>

Python Mathematics

Python has a variety of modules like math and random to carry out different mathematical calculations and operations like trigonometry, logarithms, probability, statistics, etc.

The math module in python is a standard module and is always available. To use this module in our program we just have to simply import it using the command import math and it gives access to all the different functions present in the module.


Python Code

# importing math module in our program
import math

#square root of 4
print("The square root of 4 is:")

#value of pi
print("Value of pi is:")

#trigonometric cos value of pi
print("The trigonometric cos value of pi is: ")

#value of log of 1000 to the base 10
print("Value of log of 1000 to the base 10 is: ")

#factorial of 5
print("Factorial of 5 is: ")


The square root of 4 is:

Value of pi is:

The trigonometric cos value of pi is:

Value of log of 1000 to the base 10 is:

Factorial of 5 is:

Below is the list of some functions that are available in the math module of python with their description.

factorial(x)Returns the factorial of x
fabs(x)Returns the absolute value of x
fmod(x, y)Returns the remainder when x is divided by y
ceil(x)Returns the smallest integer greater than or equal to x.
floor(x)Returns the largest integer less than or equal to x
isfinite(x)Returns True if x is neither infinity nor a NaN (Not a Number)
isinf(x)Returns True if x is a positive or negative infinity
modf(x)Returns the fractional and integer parts of x
trunc(x)Returns the truncated integer value of x
exp(x)Returns e**x
log2(x)Returns the base-2 logarithm of x
log10(x)Returns the base-10 logarithm of x
pow(x, y)Returns x raised to the power y
sqrt(x)Returns the square root of x
acos(x)Returns the arc cosine of x
asin(x)Returns the arc sine of x
atan(x)Returns the arc tangent of x
atan2(y, x)Returns atan(y / x)
cos(x)Returns the cosine of x
sin(x)Returns the sine of x
tan(x)Returns the tangent of x
erf(x)Returns the error function at x
gamma(x)Returns the Gamma function at x
piThe mathematical constant is the ratio of the circumference of a circle to its diameter (3.14159…)
erfc(x)Returns the complementary error function at x
lgamma(x)Returns the natural logarithm of the absolute value of the Gamma function at x

Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]