wget https://raw.githubusercontent.com/aidenhuynh/CS_Swag/master/_notebooks/2022-11-30-randomvalues.ipynb

Libraries

  • A library is a collection of precompiled codes that can be used later on in a program for some specific well-defined operations.
  • These precompiled codes can be referred to as modules. Each module contains bundles of code that can be used repeatedly in different programs.
  • A library may also contain documentation, configuration data, message templates, classes, and values, etc.

Why are libraries important?

  • Using Libraries makes Python Programming simpler and convenient for the programmer.
  • One example would be through looping and iteration, as we don’t need to write the same code again and again for different programs.
  • Python libraries play a very vital role in fields of Machine Learning, Data Science, Data Visualization, etc.

A few libraries that simplify coding processes:

  • Pillow allows you to work with images.
  • Tensor Flow helps with data automation and monitors performance.
  • Matplotlib allows you to make 2D graphs and plots.

The AP Exam Refrence Sheet itself is a library! Screenshot 2022-12-11 221853

Hacks:

Research two other Python Libraries NOT DISCUSSED DURING LESSON and make a markdown post, explaining their function and how it helps programmers code.

API’s

  • An Application Program Interface, or API, contains specific direction for how the procedures in a library behave and can be used.
  • An API acts as a gateway for the imported procedures from a library to interact with the rest of your code.

Activity: Walkthrough with NumPy

  • Install NumPy on VSCode:
    1. Open New Terminal In VSCode:
    2. pip3 install --upgrade pip
    3. pip install numpy

REMEMBER: When running library code cells use Python Interpreter Conda (Version 3.9.12)

Example of using NumPy for arrays:

import numpy as np
new_matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
 
print (new_matrix)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Example of using NumPy for derivatives:

import numpy as np
 
# defining polynomial function
var = np.poly1d([2, 0, 1])
print("Polynomial function, f(x):\n", var)
 
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=", derivative)
 
# calculates the derivative of after
# given value of x
print("When x=5  f(x)'=", derivative(5))
Polynomial function, f(x):
    2
2 x + 1
Derivative, f(x)'=  
4 x
When x=5  f(x)'= 20

Random Values

  • Random number generation (RNG) produces a random number (crazy right?)
    • This means that a procedure with RNG can return different values even if the parameters (inputs) do not change
  • CollegeBoard uses RANDOM(A, B), to return an integer between integers A and B.
    • RANDOM(1, 10) can output 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10
    • In Python, this would be random.randint(A, B), after importing Python's "random" library (import random)
    • JavaScript's works a little differently, with Math.random() returning a value between 0 and 1.
      • To match Python and CollegeBoard, you could make a procedure like this

CollegeBoard Example: What is the possible range of values for answ3

CollegeBoard

Convert the following procedure to Python, then determine the range of outputs if n = 5.


PROCEDURE Dice(n)
    sum ← 0
    REPEAT UNTIL n = 0
        sum ← sum + RANDOM(1, 6)
        n ← n - 1
    RETURN sum

import random # Fill in the blank

def Dice(n):
    sum = 0 
    while n != 0:
        sum = sum + random.randint (1,6)
        n = n - 1
    print(sum)
Dice(5) # Will output a range of 5 to 30
19

Homework

  1. Write a procedure that generates n random numbers, then sorts those numbers into lists of even and odd numbers (JS or Python, Python will be easier).
import random


def sortAndGenerateOddEvenLists():   
   
   n = int(input("How many random numbers would you like to generate?"))
 
   # Generating n random numbers in the range of 1 to 100 and storing that in list numbers
   numbers = [random.randint(1, 100) for _ in range(n)]
   oddNumbers= []
   evenNumbers = []

   for num in  numbers:
     if num % 2 == 0:
        # if the number is even append to the list evenNumbers
        evenNumbers.append(num)
     else:
         # if the number is odd append to the list oddNumbers
        oddNumbers.append(num)

   print('Numbers: ', numbers)    
   #sort the list
   numbers.sort()
   print('Numbers sorted: ', numbers)
   print('Even Numbers: ', evenNumbers)
   print('Odd Numbers:', oddNumbers)

sortAndGenerateOddEvenLists()
Numbers:  [62, 90, 45, 10, 52, 5]
Numbers sorted:  [5, 10, 45, 52, 62, 90]
Even Numbers:  [62, 90, 10, 52]
Odd Numbers: [45, 5]
  1. Using NumPy and only coding in python cell, find the answer to the following questions: a. What is the derivative of 2x^5 - 6x^2 + 24x? b. What is the derivative of (13x^4 + 4x^2) / 2 when x = 9?
import numpy

# define polynomial using the poly1d class 
polynomial = numpy.poly1d([2, 0, 0, -6, 24, 0])

# Compute the derivative using the deriv function
derivative = polynomial.deriv()
print('The derivative of \n', polynomial, ' is: \n ', derivative)
The derivative of 
    5     2
2 x - 6 x + 24 x  is: 
      4
10 x - 12 x + 24
import numpy

# define polynomial using the poly1d class 
polynomial = numpy.poly1d([13, 0, 4, 0, 0])

d = numpy.poly1d([2])
# Compute the derivative using the deriv function
derivative = ((d * polynomial .deriv()) - (d.deriv() * polynomial)) / 4

# Compute the derivative at x = 9
res = derivative(9)

print('The derivative of \n', polynomial, ' is: \n ', derivative)
print('\nWhen x = 9, the derivative is: ' , round(res))
The derivative of 
     4     2
13 x + 4 x  is: 
      3
26 x + 4 x

When x = 9, the derivative is:  18990
  1. Suppose you have a group of 10 dogs and 10 cats, and you want to create a random order for them. Show how random number generation could be used to create this random order.
import random

# create a list of cats and dogs

cats = ["Cat1", "Cat2", "Cat3", "Cat4", "Cat5", "Cat6", "Cat7", "Cat8", "Cat9", "Cat10"]
dogs = ["Dog1", "Dog2", "Dog3", "Dog4", "Dog5", "Dog6", "Dog7", "Dog8", "Dog9", "Dog10"]

# concatenate two lists to form third list animals
animals =  cats + dogs

# using shuffle function to reorganize the animals list
random.shuffle(animals)

print('Cats: ', dogs)
print('Dogs: ', cats)
print('Animals: ', animals)
Cats:  ['Dog1', 'Dog2', 'Dog3', 'Dog4', 'Dog5', 'Dog6', 'Dog7', 'Dog8', 'Dog9', 'Dog10']
Dogs:  ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5', 'Cat6', 'Cat7', 'Cat8', 'Cat9', 'Cat10']
Animals:  ['Dog1', 'Cat6', 'Cat5', 'Dog5', 'Dog2', 'Dog4', 'Dog10', 'Dog8', 'Dog9', 'Cat3', 'Dog7', 'Cat2', 'Cat7', 'Cat10', 'Dog3', 'Cat8', 'Dog6', 'Cat1', 'Cat4', 'Cat9']