Purpose: Help students streamline and make their coding experience easier through built in packages and methods from a library
Objective: By the end of the lesson, students should be able to fluently use methods from the turtle and math packages, and be able to look up documentation for any python package and us it.

fill in the blanks!

Libraries

Okay, so we've learned a lot of code, and all of you now can boast that you can code at least some basic programs in python. But, what about more advanced stuff? What if there's a more advanced program you don't know how to make? Do you need to make it yourself? Well, not always.

You've already learned about functions that you can write to reuse in your code in previous lessons. But,there are many others who code in python just like you. So why would you do again what someone has already done, and is available for any python user?

Packages allow a python user to import methods from a library, and use the methods in their code. Most libraries come with documentation on the different methods they entail and how to use them, and they can be found with a quick google search. methods are used with the following:

Note: a method from a package can only be used after the import statement.

Some libraries are always installed, such as those with the list methods which we have previously discussed. But others require a special python keyword called import. We will learn different ways to import in Challenge 1.

Sometimes we only need to import a single method from the package. We can do that with the word "from", followed by the package name, then the word "import", then the method. This will alllow you to use the method without mentioning the package's name, unlike what we did before, however other methods from that package cannot be used. To get the best of both worlds you can use "*".

To import a method as an easier name, just do what we did first, add the word "as", and write the name you would like to use that package as.

Challenge 1: Basic Libraries

  1. Find a python package on the internet and import it NumPy, random, pandas
  2. Choose a method from the package and import only the method ex: randint is a method of random package
  3. import the package as a more convenient name. import as
import random as rd
print(rd.randint(1,10))

from random import randint as ri
print(ri(1,10))

import math as m
nums = [1, 2, 3, 4]
print(m.fsum(nums))
7
7
10.0

Challenge 2: Turtle

Turtle is a python drawing library which allows you to draw all kinds of different shapes. It's ofter used to teach beginning python learners, but is really cool to use anywhere. Turtle employs a graphics package to display what you've done, but unfortunately it's kind of annoying to make work with vscode.
Use: repl.it
Click "+ Create", and for language, select "Python (with Turtle)"
Documentation
Task: Have fun with turtle! Create something that uses at least 2 lines of different lengths and 2 turns with different angles, and changes at least one setting about either the pen or canvas. Also use one command that isn't mentioned on the table below(there are a lot). Paste a screenshot of the code and the drawing from repl.it

Commands
forward(pixels)
right(degrees)
left(degrees)
setpos(x,y)
speed(speed)
pensize(size)
pencolor(color)

Note: Color should be within quotes, like "brown", or "red"

This is an image

Challenge 3: Math

The math package allows for some really cool mathematical methods!

methods Action
ceil(x) roundes to the smallest integer greater than or equal to x
floor() rounds to largest integer less than or equal to x
factorial(x) returns the factorial of x
gcd(x,y) returns the greatest common denominator of x and y
lcm(x,y) returns the least common multiple of x and y
Challenge: Create a program which asks for a user input of two numbers, and returns the following:
  • each number rounded up
  • each number rounded down
  • the lcm of the rounded down numbers
  • the gcf of the rounded up numbers
  • the factorial of each number
  • something else using the math package!
    Documentation
import math as m
x = float(input("Enter a number:"))
y = float(input("Enter another number:"))

# Rounded up
ceil_x = m.ceil(x)
print(ceil_x)
ceil_y = m.ceil(y)
print(ceil_y)

# Rounded down
floor_x = m.floor(x)
print(floor_x)
floor_y = m.floor(y)
print(floor_y)

# LCM of the rounded down
print(m.lcm(floor_x,floor_y))

# GCF of the rounded up
print(m.gcd(ceil_y,ceil_y))

# Square root
print(m.sqrt(ceil_x))
6
8
5
7
35
8
2.449489742783178

Homework: Putting it all together(complete only after the random values lesson)

Option 1: Create a python program which generates a random number between 1 and 10, and use turtle to draw a regular polygon with that many sides. As a hint, remember that the total sum of all the angles in a polygon is (the number of sides - 2) * 180. Note: a regular polygon has all sides and angles the same size. Paste a screenshot of the code and the drawing from repl.it

Option 2: use the "datetime" package, and looking up documentation, create a program to generate 2 random dates and find the number of days between

Extra ideas: customize the settings, draw a picture, or something else!

Extra: I also added a random sidelength generator

This is an image

Unit 3.15 Random Values

Purpose/Objectives: Teach student how to implement randomness into their code to make their code simulate real life situations. In this lesson students will learn:

  • How to import random to python
  • How to use random with a list or number range
  • How to code randomness in everyday scenarios

ADD YOUR ADDITIONAL NOTES HERE:

  • Random values are a number generated using a large set of numbers and a mathematical algorithm which gives equal probability to all numbers occuring
  • Using random number generation in a program means each execution may produce a different result
  • Random outputs in the word: Coin flip, marbles, dice
  • We need random values for code: games, statistical sampling, cryptography, etc

Random values can be used in coding:

import random
random_number = random.randint(1,100)
print(random_number)
19
def randomlist():
    list = ["apple", "banana", "cherry", "blueberry"]
    element = random.choice(list)
    print(element)
randomlist()
blueberry

Real life example: Dice Roll

import random
for i in range(3):
    roll = random.randint(1,6)
    print("Roll " + str(i + 1) + ":" + str(roll))
Roll 1:2
Roll 2:5
Roll 3:1

Challenge #1

Write a function that will a simulate a coinflip and print the output

import random
def coinflip():
    options = ["Heads", "Tails"]
    print(random.choice(options))

coinflip()
Tails

Homework

Given a random decimal number convert it into binary as Extra convert it to hexidecimal as well.

Extra: I did a decimal to octal converter too

import random
x = random.randint(1, 255)

## Decimal to Binary
numsBin = []
def convertBin(x):
    x = int(x)
    while x/2 != 0:
        rem = x % 2
        numsBin.append(rem)
        x = int(x/2)
    numsBin.reverse()
    binary = ""
    for i in numsBin:
        binary = binary + str(i)
    return binary

print(str(x) + " to binary: " + convertBin(x))

## Decimal to Hex
numsHex = []
dict = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}
def convertHex(x):
    x = int(x)
    while x/16 != 0:
        rem = x % 16
        numsHex.append(dict[rem])
        x = int(x/16)
    numsHex.reverse()
    hex = ""
    for i in numsHex:
        hex = hex + str(i)
    return hex

print(str(x) + " to hexidecimal: " + str(convertHex(x)))


## Decimal to Octal
numsOct = []
def convertOct(x):
    x = int(x)
    if x < 8:
        octal = x
        return octal
    else:
        while x/8 != 0:
            rem = x % 8
            numsOct.append(rem)
            x = int(x/8)
        numsOct.reverse()
        octal = " "
        for i in numsOct:
            octal = octal + str(i)
        return octal

print(str(x) + " to octal: " + str(convertOct(x)))
203 to binary: 11001011
203 to hexidecimal: CB
203 to octal:  313