Example HTML page

Numbers in Python

Numbers

Numbers are one of the data types in Python. We can store numeric values. Python has four built-in numeric data types. Those are:

  • Integers
  • Floating point numbers
  • Complex numbers
  • Boolean

Ex

>>> 9	                  #integers
9
>>> type(9)
<class 'int'>
>>> int(67.7)
67
>>> int(-1.2)
-1
>>> int(0)
0


>>> 1.1	               #floating point numbers
1.1
>>> type(1.1)
<class 'float'>
>>> float(5.3)
5.3
>>> float(-5)
-5.0


>>> 3j	                #Complex numbers
3j
>>> type(3j)
<class 'complex'>
>>> complex(3j)
3j
>>> complex()
0j
>>> complex(-1j)
(-0-1j)

>>> bool(0)	       #Boolean
False
>>> bool('0')
True
>>> bool(1)
True
>>> bool('1')
True
>>> m=43
>>> n=m>50
>>> n
False

Common mathematical functions

Python provides the following built-in mathematical functions. The illustration of each is shown in below:

>>> abs(15)	   #abs ( ) -  returns the absolute value 
15
>>> abs(-15)
15
>>> abs(1.5)
1.5


>>> pow(2,5)	 #pow ( ) – returns the value of 2 to the power of 5(here)
32
>>> pow(3,2)
9


>>> round(6.9)      #round( )-returns the nearest round value of that number
7
>>> round(6.2)
6
>>> round(6.5)
6
>>> round(4.5,2)
4.5
>>> round(4.67585,2)   #Here 2 represents how many values are to be printed after the point
4.68
>>> round(4.4563847,3)
4.456


>>> max(1,1.1,-1.3)	#max( ) – returns the maximum value.
1.1
>>> max(16,-9,0)
16

>>> min(1,1.1,-1.3)	#min( ) – returns the minimum value
-1.3
>>> min(16,-9,0)
-9

Mathematical functions in math module

Python provides some built-in mathematical functions for doing calculations. Before going to that we should know about math module.

In Python the math module is a standard module. To use mathematical functions, you have to import the module using import math.

>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

>>> math.pi
3.141592653589793
>>> math.sin(30)
-0.9880316240928618
>>> math.ceil(4)
4

Some standard mathematical functions in math module are described in below examples.

>>> math.pi	         #math.pi - returns the value of pi
3.141592653589793

>>> math.e	         #math.e - returns the value of e
2.718281828459045

>>> math.ceil(5.6)     #math.ceil(5.6) – returns the nearest integer greater than or equal to 5.6
6
>>> math.ceil(5.3)
6
>>> math.ceil(5)
5


>>> math.floor(5.6)   # math.floor(5.6) – returns the nearest integer smaller than or equal to 5.6
5
>>> math.floor(5.3)
5


>>> math.sqrt(64)	  #math.sqrt(64)–returns the sqrt of 64 as float 8.0
>>> math.sqrt(121)
11.0


>>> math.log(2)	# math.log(2) – returns the logarithmic value of 2
0.6931471805599453

Trigonometric functions

>>> math.sin(45)	 
0.8509035245341184
>>> math.sin(math.pi)
1.2246467991473532e-16


>>> math.cos(0)
1.0
>>> math.cos(30)
0.15425144988758405


>>> math.tan(45)
1.6197751905438615
>>> math.tan(90)
-1.995200412208242

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest