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 nin 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 __, 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 matplotlib.pyplot as plt

# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
  
# plotting the points 
plt.plot(x, y)
  
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
  
# giving a title to my graph
plt.title('Rohan Graph')
  
# function to show the plot
plt.show()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb Cell 4 in <cell line: 1>()
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> import matplotlib.pyplot as plt
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> # x axis values
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> x = [1,2,3]

ModuleNotFoundError: No module named 'matplotlib'

Challenge 2: Turtle

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

screenshot

Challenge 3: Math

The math package allows for some really cool mathematical methods!

methods Action
ceil(x) rounded down on each integer (e.g. 10.1 --> 11)
floor(x) rounds to lowest integer less than or equal to x (e,g, 10.9 --> 10)
factorial(x) multiplies a number x by (x-1) until the new number = 1.
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
x = 3
y = 5

print(math.ceil(x))
print(math.ceil(y))
print(math.floor(x))
print(math.floor(y))
print(math.lcd(x,y))
print(math.gcd(x,y))
print(math.factorial(x))
print(math.factorial(y))
3
5
3
5
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb Cell 7 in <cell line: 9>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> print(math.floor(x))
      <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> print(math.floor(y))
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> print(math.lcd(x,y))
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9'>10</a> print(math.gcd(x,y))
     <a href='vscode-notebook-cell://wsl%2Bubuntu/mnt/c/Users/rohan/vscode/RohanRepository/_notebooks/2022-12-10-3.14-Libraries.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> print(math.factorial(x))

AttributeError: module 'math' has no attribute 'lcd'

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!

Screenshot