Archangel Macsika Sikademy Image
Archangel Macsika

Learn all About Numbers and Mathematical Functions in Python

Tutorial 10 of 11 | 6 minutes read
Learn all About Numbers and Mathematical Functions in Python - Sikademy

Number Data Types in Python

We use number data types in python programming to store and work with numeric values.

There are three main numeric data types in Python:

  • Int: They are used to represent positive or negative whole numbers with no decimal point. Python 2 has two integer types, int and long, representing small and large integers. However, in Python 3, the long data type was discontinued while the int data type can contain any number size.

  • Float: is a positive a negative number containing one or more decimals. It is often called a Float, or "floating-point number." A float may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • Complex: Complex numbers appear as x + yj, where x is the real part and y is the imaginary part.

How to Assign and Check Number Data Types in Python

In python programming, number data types are automatically created when you assign a value to them.

To check the data type of a variable in python programming, use the type() function.

Example:



x = 10
y = 4.90
z = 9j

print(type(x))
print(type(y))
print(type(z))

Output:

<class 'int'>
<class 'float'>
<class 'complex'>

Here are some more examples of number data types:

intfloatcomplex
10.3545.8j
100090929046785.890799383.14j
0x92042.6+e120.123j
-1234567-567.9879.322e-36j
-0×650-71.24e903e+11J

In Python programming, you can represent an integer in binary, octal, or hexadecimal form.



x = 0o37
y = 0xA0F
z = 0b1101011

print(x)
print(y)
print(z)

print(type(x))
print(type(y))
print(type(z))

Output:

2575
31
107

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

How to Convert Number Data Types in Python

In python programming, you can convert from one data type to another using the int(), float(), and complex() functions:

int(x) is used to convert x to a plain integer.

float(x) is used to convert x to a floating-point number.

complex(x) is used to convert x to a complex number with real part x and imaginary part zero.

complex(x, y) is used to convert x and y to a complex number with real part x and imaginary part y where x and y are numeric expressions.

Example:



x = 989    # int
y = 6.009  # float
z = 88j    # complex

# convert from int to float:
a = float(x)

# convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))

Output:

989.0
6
(88+0j)

<class 'float'>
<class 'int'>
<class 'complex'>

You cannot convert complex data types into another number data type.

  • Sikademy Tips

Mathematical Functions in Python Programming

Mathematical Constants

There are two main mathematical constants defined in the python module.

ConstantNameDescription
eThe number e.The mathematical natural number e approximately equal to 2.71828
piThe number pi.The mathematical constant pi approximately equal to 3.14159

Trigonometric Functions

These are functions defined in the python module used to carry out trigonometric calculations.

FunctionNameDescription
acos(x)Arc CosineThis is used to get the arc cosine of a number (x), in radians.
asin(x)Arc SineThis is used to get the arc sine of a number (x), in radians.
atan(x)Arc TangentThis is used to get the arc tangent of a number (x), in radians.
atan2(y, x)Arc TangentThis is used to get the arc tangent in form of atan(y/x), in radians.
cos(x)CosineThis is used to get the cosine of a number (x), in radians.
hypot(x, y)HypotenuseThis is used to get the Euclidean norm, sqrt(x*x + y*y).
sin(x)SineThis is used to get the Sine of a number (x), in radians.
tan(x)TangentThis is used to get the tangent of a number (x), in radians.
degrees(x)DegreesThis is used to convert a number (x) to degrees.
radians(x)RadiansThis is used to convert a number (x) to radians.

Mathematical Functions

The following functions are included in Python programming to perform mathematical calculations.

FunctionNameDescription
abs(x)Absolute ValueGet the absolute value of x.
ceil(x)CeilingGet the ceiling of x i.e. the smallest integer not less than x.
exp(x)ExponentialGet the exponential of x i.e. ex.
fabs(x)Absolute value functionGet the absolute value function of x.
floor(x)FloorGet the floor of x i.e. the largest integer not greater than x.
log(x)Natural LogGet the natural logarithm of x, for x > 0.
log10(x)Base-10 LogGet the base-10 logarithm of x for x > 0.
max(x1, x2,...)Maximum NumberGet the largest number of a list of numbers.
min(x1, x2,...)Minimum NumberGet the lowest number of a list of numbers.
modf(x)ModeGet the fractional and integer parts of x in a two-item tuple.
pow(x, y)PowerGet the value of x to the power of y i.e. xy.
round(x [,n])Round OfGet x rounded to n digits from the decimal point.
sqrt(x)Square RootGet the square root of x.

Wrap Off

Number data types in python programming are used to store and work with numeric values.

There are three main numeric data types in Python: int, float, complex.

If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.

Tutorial Exercise

Using random numbers of your choice, test each of the functions shown in the mathematical functions table.


Enjoy this Tutorial? Please don't forget to share.