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
  2. Choose a method from the package and import only the method
  3. import the package as a more convenient name.
import ANNOgesic
from ANNOgesic import crispr
import ANNOgesic as AG

Challenge 2: Turtle

Turtle is a python drawing package 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"

(see comment for image)

Challenge 3: Math

The math package allows for some really cool mathematical methods!

methods Action
ceil(x) rounds up integer
floor(x) rounds to largest intefer less than or equal to x
factorial(x) returns factorial of x
gcd(x,y) returns the greatest common denominator of x and y
lcm(x,y) returns 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
from math import *

x = int(input("Enter any number:"))
y = int(input("Enter another number:"))

# extra inclusion of an absolute value function to make sure x and y are >0
x = abs(x)
y = abs(y)

# numbers rounded up
xu = ceil(x)
yu = ceil(y)

# gcf of rounded up
gcd(xu,yu)

# numbers rounded down
xd = floor(x)
yd = floor(y)

# lcm of rounded down numbers; the lcm command didn't work so I made my own
def lcmfunction(xd, yd):

   if xd > yd:
       greater = xd
   else:
       greater = yd

   while(True):
       if((greater % xd == 0) and (greater % yd == 0)):
           lcm = greater
           break
       greater += 1

   return lcm

# factorial of each
factorial(x)
factorial(y)
6

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!

(see comment for image)