libraries are essentially sets of classes and functions that other people have written and that we might want to make use of.
turtle is an example of a library. classes (think homemade types) like these are contained in it:
To use a class Turtle within the turtle library, you need dot notation, such as turtle.Turtle(). These classes contain functions. A function within a class can be called a method and also requires dot notation.
- exitonclick() is a method, or function, of the Screen class.
- forward() is a method, or function, of the Turtle class.
So when I have named a Turtle, such as Paula, I use the dot notation, Paula.forward(), to use the forward method.
Options for getting and using these turtle classes with methods
Related commands include:
import
as
from
*
Look over this example. Go ahead and try it if you don't remember how it worked.
A convention in programming is to put all import statements at the top of the file. This example uses the as command to give turtle a nickname. Read it over. Trying is optional.
I can use the from command to import specific classes and functions from the turtle library. Notice how now I don't have to use the dot notation for all.
Here I am importing all functions from turtle and again I don't need to use dot notation.
What are good reasons to use or not to use these techniques?
A Few Examples Built in Python libraries
- turtle
- random
- math
- os
- sys
Other Python libraries can be installed.
There are many Python libraries that apply to a large variety of fields and purposes.
Try searching for Anaconda Python libraries and find two libraries that look interesting to you.
Use Google to learn about Python's built in calendar library.
Try using the 4 methods of importing with some functions from the calendar library. Functions you might try are isleap, leapdays, weekday.