Python Libraries

For this course, we will be using two data sciences packages. The first is numpy, which has several tools for manipulating numbers. The second is matplotlib which allows you to graphically plot using Python.

Documentation

Using numpy

NumPy is the fundamental package for scientific computing with Python. There are complex mathematical functions that you can easily perform by using numpy. NumPy is a module in Python called numpy, and it can be imported by using import numpy.

import numpy as np

The as np clause aliases numpy as np. Remember, with import, we have to prefix every function with numpy.function, however with the alias, we prefix it with np.function.

Using Matplotlib

import matplotlib.pyplot as plt
import numpy as np

Matplotlib and numpy are tightly integrated, so you'll typically see something like the preamble above. It is also common practice to alias the names of matplotlib.pyplot and numpy as plt and np, respectively.

Turtle Graphics

Turtle is a simple graphics program that moves a "turtle" around, which can draw various objects.

You can create a turtle by importing the turtle library and using the code below:

import turtle
my_turtle = turtle.Turtle()

Now, you can have the turtle perform various actions, such as the following.

Function Description
my_turtle.forward(x) Moves the turtle forward by the given amount x
my_turtle.penup() Moves the pen up and disables drawing lines
my_turtle.pendown() Moves the pen down and enables drawing lines
my_turtle.left(x) Turns the turtle to the left by x degrees
my_turtle.right(x) Turns the turtle to the right by x degrees
my_turtle.goto(x, y) Moves the turtle to the given coordinates
my_turtle.goto((x, y)) Moves the turtle to the tuple-given coordinates
my_turtle.color("color") Sets the color to the given color [red, green, brown, yellow, etc]

There are more functions than are described in the table above, but they are sufficient to complete your lab. You can learn more in depth about Turtle: https://docs.python.org/3/library/turtle.html?highlight=turtle#turtle-methods.


Stephen Marz (20190518)