Math in Python

The code below demonstrates some common mathematical operations in python as well as usage of Python's isinstance function.

The math library contains many methods including a method that ouputs the value of pi.

import math

radius        = 10.0
diameter      = 2 * radius
circumference = 2 * math.pi * radius
area          = math.pi * radius ** 2

print("Radius is", radius)
print("Diameter is", diameter)
print("Circumference is", circumference)
print("Area is", area)
Radius is 10.0
Diameter is 20.0
Circumference is 62.83185307179586
Area is 314.1592653589793

isinstance() function

In the next code cell, you will see how the isinstance() function works.

The isinstance() function checks to see if a variable holds a certain type of value like an integer, float, string, etc.

sqr_root_2 = math.sqrt(2)
is_sqr_root_2_an_integer = isinstance(sqr_root_2, int)
print("Is square root two an integer?", is_sqr_root_2_an_integer)

is_sqr_root_2_a_float = isinstance(sqr_root_2, float)
print("Is square root two a float?", is_sqr_root_2_a_float)
Is square root two an integer? False
Is square root two a float? True

Last updated