Notes

  • A software library contians procedures that can be used in creation of new programs
  • existing segments of code can come from internal or external sources
    • Libraries, or previously written code
  • APIs are specificagions for procedures in a library
  • Documentation for library or API is necessary in understanding key behaviors and how to utilize
  • library: collection of code from an external source that can be used to add functionality to a program
    • Saves time and effort in development
    • Implemented using keyword " .", tells program to look for library
  • Randomization generates a value within a range, between 2 numbers
  • Random includes different methods, such as randit(), seed(), getstate(), etc.
  • RANDOM(a, b) provides a random integer between a and b

Hacks

Lesson 3.14.1

import math

print("Find the hypotenuse of a right triangle!")

a = int(input("Input side a"))
print("Side a =", a)
b = int(input("Input side b"))
print("Side b =", b)

def side(x):
    return x * x
def equation(a, b):
    a = side(a)
    b = side(b)
    result = math.sqrt(a + b)
    return result

print("Hypotenuse is", equation(a, b))
Find the hypotenuse of a right triangle!
Side a = 3
Side b = 4
Hypotenuse is 5.0

I used import math, and created a program that helps you calculate the hypotenuse of a right triangle. The library use is in the function math.sqrt. The function provided the answer in a simple and efficient manner.

Lesson 3.15.1

Import random function generates a value a completely random number within a range between two numbers.

Other imports:

  • math
    • gives access to math functions defined by the C standard
  • cmath
    • gives access to math functions for complex numbers
  • your own module
    • you can create your own library by storing in a .py file
  • NumPy
    • gives access to comprehensive math functions, random number generators, linear algebra routines, Fourier transforms, and more. Must be installed with pip install

Lesson 3.15.2

There is a spinner divided into eight equal parts. 3 parts of the spinner are green, two parts are blue, one part is purple, one part is red, and one part is orange. How can you simulate this situation using a random number generator.

import random
spin = random.randint(1,8)

if spin <=3:
    spin = "green"
elif spin == 4 or 5:
    spin = "blue"
elif spin == 6:
    spin = "purple"
elif spin == 7:
    spin = "red"
elif spin == 8 :
    spin = "orange"

print("You spun: " + spin)
You spun: blue

What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?

12, 13, 14, 15, 16, 17 18, 19, and 20 can be outputted. Any number less than 12 or greater than 20 is excluded, as it's not in the parameters.