1. In your own words, what is a library?

    • A library is stored code from an external source that can help save time and effort while developing
  2. Why are libraries useful when writing a program?

    • Libraries can save time and effort during a developing process without damaging the quality of the code
  3. What keyword is used to add a pre-made library?

    • import

Hacks 3.14.1

Write a program that uses a library/libraries in any sort of manner. Explain your work/code

  
# import math module
import math
  
# function to check if prime or not 
def check(n):
    if n == 1:
        return False
          
        # from 1 to sqrt(n) 
    for x in range(2, (int)(math.sqrt(n))+1):
        if n % x == 0:
            return False 
    return True
  
# driver code
n = 12
if check(n):
    print("prime") 
else:
    print("not prime")
not prime

Hacks for lesson 3.15.1

  • Write a few lines of code that implements the import function

  • Define what an import random function do

    • the import random function defines a series of functions for generating or manipulating random integers
  • List a few other things that we can import other than random
    • math
    • flask
    • os
    • sys
    • datetime
import math

# Use the sqrt function from the math module
result = math.sqrt(250000000)
print(result)  
15811.388300841896

In this example, the import statement is used to import the math module, which contains functions for performing mathematical operations. The math.sqrt function is then used to calculate the square root of 16, and the result is printed to the console.

Hacks 3.15.2

  • For your hacks you need to create a random number generator that will simulate this situation:

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

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

    • The RANDOM function is a function in some programming languages (such as Excel and Google Sheets) that generates a random number between a given range of values. In the case of RANDOM(12, 20), the function will generate a random number between 12 and 20, inclusive.

    • This means that the possible output values from RANDOM(12, 20) are the numbers 12, 13, 14, 15, 16, 17, 18, 19, and 20. No other numbers outside of this range will be generated by the function.

import random

# Define a mapping from numbers to colors
# 3 green, 2 blue, 1 purple, 1 red, 1 orange

color_map = {
    1: "green",
    2: "green",
    3: "green",
    4: "blue",
    5: "blue",
    6: "purple",
    7: "red",
    8: "orange"
}

# Generate a random number between 1 and 8

random_num = random.randint(1, 8)

# Use the mapping to determine the color
# that the spinner lands on

spinner_color = color_map[random_num]

print(f"The spinner landed on {spinner_color}")
The spinner landed on green

This code uses the random module to generate a random number between 1 and 8, and then uses a dictionary (color_map) to map that number to a specific color. Finally, it prints the result of spinning the spinner.