Unit 3.14

  • library contains procedures that can be used in the creation of new programs, saves time and effort
  • simplifies the task of creating complex programs.
  • Application program interfaces (APIs)

Example

#instead of writing code to find the square root, the sqrt() 
# function in the math library  gives the answer with the number in the argument

import math
math.sqrt(64)
8.0
# list of people can be indexed to get a random person in the list of people

import random

people = ["Grace", "Claire", "Harry", "Taylor", "Annika", "Jane"]

num = random.randint(0,6)
print(num)
print(people[num])
5
Jane

Lesson 3.15

Hacks 3.15.1

  1. Define what an import random function do
    • import random imports the random library, which contains many randomizing functions
  2. List a few other things that we can import other than random
    • numpy
    • math
    • your own files
    • requests
    • a module with .py
    • package

Hacks 3.15.2

# one part is purple, one part is red, and one part is orange

import random

spinner = ["green", "green", "green", "blue", "blue", "purple", "red", "orange"]
index = random.randint(0,7)
print("Your spinner spun: " + spinner[index])
Your spinner spun: green

Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?

random(12,20) can produce outputs 12, 13, 14, 15, 16, 17, 18, 19, 20