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 
    return 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).

  2. 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?

  3. 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.

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 numsorter(n = int): # for n integers
    numbers = [random.randint(0, 1000) for i in range(n)] # assigns each number in range with an index value and number
    even = [num for num in numbers if num % 2 == 0] # If remainder is = 0, then a number is even so module helps
    odd = [num for num in numbers if num % 2 > 0] # If remainder is greater than 0, then its an odd number so we use module

    print("Odd Numbers:\n", odd)
    print("\nEven Numbers:\n", even)

n = int(input("Enter any number (1-1000)"))
numsorter(n) # runs procedure 
print("\nInput:\n", n)
Odd Numbers:
 [249, 947, 843, 413, 881, 367, 669, 415, 713, 731, 591, 391, 135, 857, 977, 37, 229, 369, 889, 801, 335, 123, 773, 983, 393, 267, 155, 307, 569, 559, 599, 481, 639, 527, 515, 803, 631, 793, 885, 273, 1, 403, 731, 517, 277, 617, 203, 799, 149, 29, 759, 877, 585, 203, 501, 561, 103, 129, 405, 875, 217, 863, 545, 889, 25, 249, 471, 665, 429, 977, 797, 787, 115, 827, 181, 729, 45, 139, 37, 47, 679, 247, 619, 41, 485, 491, 217, 741, 619, 881, 187, 377, 501, 787, 131, 763, 117, 429, 173, 879, 689, 663, 677, 637, 245, 949, 487, 257, 867, 297, 911, 869, 863, 47, 675, 303, 387, 941, 727, 69, 217, 783, 335, 217, 989, 451, 783, 449, 783, 865, 771, 389, 277, 647, 641, 235, 225, 179, 213, 437, 875, 489, 331, 71, 965, 741, 955, 807, 1, 587, 887, 413, 283, 77, 727, 155, 49, 963, 11, 811, 195, 591, 83, 831, 97, 411, 667, 385, 465, 165, 211, 667, 847, 909, 861, 655, 679, 29, 385, 825, 529, 657, 661, 79, 995, 961, 181, 745, 967, 83, 909, 217, 21, 795, 57, 893, 913, 349, 395, 699, 369, 553, 949, 977, 833, 989, 991, 837, 333, 535, 269, 549, 1, 361, 441, 349, 755, 287, 579, 23, 833, 769, 653, 867, 553, 391, 795, 861, 467, 453, 897, 857, 231, 429, 519, 107, 567, 123, 187, 953, 313, 683, 891, 617, 589, 21, 367, 481, 583, 349, 447, 463, 673, 67, 155, 99, 683, 233, 51, 181, 851, 755, 739, 601, 571, 937, 675, 807, 361, 537, 325, 835, 495, 97, 501, 535, 133, 447, 609, 475, 363, 225, 813, 365, 335, 813, 61, 177, 649, 187, 943, 335, 449, 603, 943, 571, 27, 509, 329, 789, 559, 27, 135, 615, 745, 591, 151, 431, 389, 649, 681, 511, 943, 865, 491, 921, 673, 869, 3, 127, 615, 253, 711, 953, 279, 503, 749, 239, 721, 117, 227, 973, 459, 273, 63, 213, 629, 477, 149, 979, 787, 387, 953, 277, 781, 269, 939, 547, 249, 309, 403, 273, 505, 225, 461, 303, 539, 795, 363, 123, 723, 731, 823, 517, 415, 289, 311, 481, 741, 477, 247, 965, 877, 893, 71, 997, 511, 393, 343, 763, 401, 995, 923, 251, 517, 21, 403, 385, 73, 381, 843, 331, 367, 521, 399, 799, 159, 797, 505, 997, 845, 425, 621, 325, 225, 57, 3, 463, 731, 711, 85, 689, 561, 677, 827, 329, 57, 313, 871, 473, 231, 309, 467, 137, 97, 981, 831, 145, 395, 749, 721, 251, 707, 995, 797, 579, 883, 295, 727, 659, 151, 777, 73, 285, 713, 877, 105, 797, 443, 427, 413, 913, 733, 495, 535, 349, 209, 687, 865, 43, 897, 431, 549, 783, 935, 715, 321, 213, 171, 261, 781, 761, 679, 57, 407, 199, 57, 119, 241, 531, 921, 815, 857, 837, 701, 647, 935, 731, 947, 345, 495, 73, 195, 381, 827, 683, 963, 479, 19, 33, 729]

Even Numbers:
 [90, 272, 84, 248, 282, 902, 738, 518, 460, 190, 242, 974, 526, 712, 258, 44, 754, 280, 774, 438, 682, 228, 522, 824, 804, 588, 314, 954, 926, 576, 70, 718, 916, 436, 528, 46, 544, 124, 946, 290, 492, 972, 146, 838, 18, 738, 72, 252, 602, 116, 230, 702, 286, 178, 332, 562, 870, 642, 858, 964, 980, 812, 628, 972, 384, 6, 622, 152, 496, 328, 618, 4, 782, 968, 268, 452, 46, 572, 866, 520, 850, 436, 948, 170, 118, 502, 976, 794, 444, 410, 132, 130, 188, 954, 832, 84, 998, 650, 946, 138, 906, 190, 552, 426, 956, 12, 760, 614, 282, 308, 404, 322, 508, 366, 442, 854, 878, 658, 492, 674, 984, 1000, 192, 368, 898, 114, 276, 356, 34, 590, 836, 736, 746, 376, 28, 332, 880, 896, 264, 686, 698, 486, 246, 4, 956, 810, 456, 950, 904, 226, 92, 216, 342, 846, 866, 956, 816, 666, 398, 380, 18, 460, 108, 900, 516, 212, 540, 788, 366, 430, 794, 254, 732, 740, 796, 616, 398, 72, 300, 124, 462, 908, 244, 428, 450, 496, 326, 548, 10, 350, 904, 740, 814, 214, 764, 466, 968, 104, 964, 276, 890, 360, 996, 812, 178, 364, 410, 224, 186, 250, 626, 516, 328, 654, 812, 884, 780, 528, 776, 894, 598, 328, 974, 50, 702, 182, 22, 512, 968, 322, 902, 798, 546, 704, 40, 726, 382, 840, 756, 370, 138, 64, 356, 750, 632, 878, 132, 776, 492, 440, 392, 678, 186, 478, 616, 200, 206, 506, 4, 736, 1000, 44, 508, 200, 440, 172, 444, 564, 386, 546, 322, 754, 922, 238, 412, 590, 68, 818, 42, 824, 918, 746, 146, 86, 208, 182, 162, 528, 190, 706, 572, 606, 300, 698, 374, 494, 352, 786, 460, 894, 274, 670, 556, 240, 894, 768, 728, 24, 700, 176, 858, 970, 894, 810, 980, 722, 118, 408, 550, 580, 688, 172, 344, 568, 674, 328, 368, 674, 828, 960, 502, 120, 986, 682, 68, 458, 186, 292, 386, 188, 924, 586, 282, 986, 148, 722, 670, 988, 96, 70, 686, 178, 424, 246, 726, 378, 390, 688, 260, 828, 52, 814, 156, 6, 186, 132, 690, 946, 98, 960, 634, 998, 40, 160, 32, 478, 404, 242, 602, 322, 760, 830, 38, 752, 212, 470, 482, 444, 894, 868, 262, 440, 376, 592, 562, 402, 728, 328, 756, 392, 526, 716, 762, 218, 210, 412, 666, 678, 270, 238, 596, 100, 488, 296, 890, 444, 818, 518, 20, 696, 120, 270, 734, 508, 798, 642, 984, 340, 332, 780, 912, 568, 738, 676, 562, 662, 528, 662, 662, 128, 782, 34, 560, 618, 490, 954, 118, 756, 956, 664, 216, 924, 502, 390, 708, 306, 756, 388, 472, 474, 712, 336, 790, 262, 462, 912, 444, 844, 592, 760, 560, 638, 292, 248, 762, 488, 946, 414, 198, 286, 486, 80, 90, 534, 576, 848, 508, 102, 382, 716, 434, 834, 442, 56, 434, 540, 698, 82, 604]

Input:
 1000

Homework 2

  • 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 as np
 
print("What is the derivative of 2x^5 - 6x^2 + 24x?\n")

var = np.poly1d([2, 0, 0, -6, 24, 0]) # works like x^5, x^4, x^3, x^2, x, 1
print("Polynomial function, f(x):\n", var)
 

derivative = np.polyder(var) # takes the derivative of a polynomial with higher powers
print("Derivative, f(x)'=\n", derivative)

print("\n\nWhat is the derivative of (13x^4 + 4x^2) / 2 when x = 9?\n")

var = np.poly1d([13, 0, 4, 0, 0]) /2 # dividing by 2 off the bat
print("Polynomial function, f(x):\n", var)

derivative = np.polyder(var) # takes the derivative of a polynomial with higher powers
print("Derivative, f(x)'=\n", derivative)
print("Derivative, f(9)'=\n", derivative(9)) # replaces x's with 9 and solves polynomial
What is the derivative of 2x^5 - 6x^2 + 24x?

Polynomial function, f(x):
    5     2
2 x - 6 x + 24 x
Derivative, f(x)'=
     4
10 x - 12 x + 24


What is the derivative of (13x^4 + 4x^2) / 2 when x = 9?

Polynomial function, f(x):
      4     2
6.5 x + 2 x
Derivative, f(x)'=
     3
26 x + 4 x
Derivative, f(9)'=
 18990.0

Homework 3

  • 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 

Cats = ["Cat" + str(i) for i in range(10)] # creates Cat1 and so on by adding the integer from range to a index value
Dogs = ["Dog" + str(i) for i in range(10)] # creates Dog1 and so on by adding the integer from range to a index value

Pets = Cats + Dogs # adds the lists together creating one big list

random.shuffle(Pets) # shuffles the big list 

print(Pets)
['Cat9', 'Cat5', 'Dog2', 'Cat3', 'Cat2', 'Cat8', 'Dog5', 'Cat7', 'Dog7', 'Dog3', 'Dog4', 'Dog9', 'Cat4', 'Cat0', 'Dog8', 'Cat6', 'Dog0', 'Dog1', 'Cat1', 'Dog6']