Essential Knowledge

Randomization: generates a value between two numbers

import random

answer1 = random.randint(0,3)
answer2 = random.randint(1,8)
answer3 = answer1 + answer2
print(answer3)

Library: a collection of code from an external source that can be used to add functionality to a program, they can be used to save time and effort in the development process (usually included in a program using a special keyword called " ")*

  • the key word " " tells the program to look for the library and use its code

anything past import is a library, python doesn't have all the "books" so you have to add on books

import math

math.sqrt(64)

examples of library:

  • Matplotlib- responsible for plotting numerical data, it is used for data analysis
  • SciPy- stands for scientific python and is used for high level scientific computations
  • PyGame- easy interface to the Standard Direct media Library (SDL) platform-independent graphics, audio, and input libraries. It is used for developing video games using computer graphics and audio libraries along with Python programming language.

Examples

  1. Why are libraries useful when writing a program?

--

  1. What keyword is used to add pre-made library?

import


Library/Libraries

  • Write a program that uses a library in any sort of manner
  • Explain your work/code
import math

x = int(input())
# set input which is a number as the value of x
math.sqrt(x)
3.0

Math is a library which you can use and using this library you can input any value to be squared, it is similar to a calculator


Implementing import function

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

  • Define what an import random function do

  • List a few other things that we can import other than random

import random
flip = random.randint(1,2)
 
if flip == 1:
    print("a")
else:
    print("b")
a
  1. What does import random function do

A random function picks something random in a list

  1. Few other thing that we can import other than random
import math

x = int(input())
# set input which is a number as the value of x
math.sqrt(x)
3.0

Random Number Generator

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

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

if spin >=3:
    print("green")

elif spin == 4 or 5:
    print("blue")

elif spin == 6:
    print("purple")

elif spin == 7:
    print("orange")

elif spin == 8:
    print("red")
blue
  1. What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?

Any number from 12-20 is included and numbers under or over 12-20 are excluded