Numeric Data Types, Math Library and Python Modules

Numeric Data Types

Python Numbers

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

Whole numbers are generally represented using int, and fractional numbers are represented using floats.

Python provides a function called type() that tells us the data type or class of any value.

>type(8)
<class 'int'>
> type(5.123)
<class 'float'>
> a = -45
> type(a)
<class 'int'>
> b = 78.98
> type(b)
<class 'float'>
https://www.w3schools.com/python/python_numbers.asp

Type Conversion

You can convert one type to another with the int(), float(), and complex() methods: https://www.w3schools.com/python/python_numbers.asp

Random Number

Python has a built-in module called random that can be used to make random numbers: https://www.w3schools.com/python/python_numbers.asp

Python Casting

You can define a type on to a variable. This can be done with casting. https://www.w3schools.com/python/python_casting.asp

Python Built-in Numeric Operations

All numeric types (except complex) support the following operations.

>3 + 4
7
>3.0 + 4.0
7.0
>3.0 * 4.0
12.0
>4.0 * 3
12.0
>4.0 ** 3
64.0
>abs(-5)
5
> 3 / 2
1.5
> 3 // 2
1

For more detailed Python built-in numeric operations, such as int(), float(), pow(), check out the following links.

https://python4astronomers.github.io/python/types.html
https://www.python-ds.com/python-3-numeric-operations
https://docs.python.org/3/library/stdtypes.html
https://www.techbeamers.com/python-numbers/

Math Library

Python has a built-in module that you can use for mathematical tasks. Python provides many other useful mathematical functions in a special math library.

The math module has a set of methods and constants. https://www.w3schools.com/python/module_math.asp

The following example computes the roots of a quadratic equation.


# Program name: quad.py
# A program that computes the real roots of a quadratic equation.
# It illustrates the use of the math library.
# Note: this program crashes if the equation has no real roots.
# For now, we will make sure sqrt(value) is > 0

import math  # Makes the math library available. This tells Python we are using the math module.

def main():
    print("This program computes the roots of a quadratic equation.")
    a,b,c = 2,3,-4 
    d = math.sqrt(b**2 - 4 * a * c)
    root1 = (-b + d) / (2 * a)
    root2 = (-b - d) / (2 * a)
    print("The two roots are {0:0.2f} and {1:0.2f}".format(root1,root2)) 
    #Other ways to output the two roots in two decimal points.
    #print("The two roots are: {:.2f} and {:.2f}".format(root1, root2))
    #print("The two roots are: {:0.2f}".format(root1)  ,"and {:0.2f}".format(root2))  
    
main()

The output of the program:

This program computes the roots of a quadratic equation. 
The roots are 0.85 and -2.35

This program uses the sqrt() function from the math library module.

Here is the list of other functions that are available in the math library: https://www.w3schools.com/python/module_math.asp

These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for complex numbers. https://docs.python.org/3/library/math.html

Python Modules

Python provides more specialized capabilites for performing computer specific tasks. You can think of these modules as a library when you need to borrow according to your needs. We access a module by using the import command.

The following four modules are used for scientific computing.

A module is a file that has a collection of useful functions and can be imported as a whole file into any application. You can use it in multiple projects and share it with other programmers. Modules have .py extension.

Because the modules listed above are not part of core Python, they need to be imported.

Since NumPy module also includes the basic mathmetical functions, we can also import NumPy to replace the math library for compute the roots of a quadratic equation.

import numpy as np  # imports and assigns the abbreviation np for numpy

def main():
    print("This program computes the roots of a quadratic equation. ")
    a,b,c = 2,3,-4 
    d = np.sqrt(b**2 - 4 * a * c)
    root1 = (-b + d) / (2 * a)
    root2 = (-b - d) / (2 * a)
    print("The roots are {0:0.2f} and {1:0.2f}".format(root1,root2))

main()

Lab Assignment

1. Write a program that accepts two points (x1,y1) and (x2,y2) and determines the distance between them using

Your program output should be similar to the following. The output is required to be set in 2 decimal places.

This program calculates the distance between two points.

Enter x1: 23.5
Enter y1: 9.2

Enter x2: 3.5
Enter y2: 1.8

The distance between the two points is 21.33

2. Write a program to compute the volume and surface area of a sphere from the input radius. Here are the formulas:

Your program output should be similar to the following. The output, volume and surface area are required to be set in 2 decimal places.

This program computes the volume and surface area of a sphere.

Please enter the radius of the sphere: 5

The volume is 523.60 cubic units.
The surface area is 314.16 square units.

Demonstrate your answers and code to your lab instructor if the lab is in person.