Libraries
Introduction to libraries in Python, specifically the Turtle graphics library.
A library is a collection of functions that allow you to perform many actions without writing as much code. They make the job of programming so much easier, and really widen the horizons of possibility!
These functions are exactly like the functions we just learned about – except they’re written by other people. So, all we have to do is call them in order to use them.
But if we didn’t write them, how do we get access to them from this library? Well, all you have to do is import them using the import
statement:
Turtle is the name of the library we're importing. This is a graphics library popular for teaching beginner programmers. It enables us to create pictures and shapes by providing us with a virtual canvas. Once imported, we can use the functions that it provides to draw whatever we want.
For instance, let's draw a square:
Here we're calling turtle's Turtle()
function to create our turtle. To do so, we call it as turtle.Turtle()
. This follows the pattern of library.function()
. We use this pattern because, if we're importing several libraries (which we can do!), we need to specify which library to call the function from.
We then use some of turtle's functions to draw the square!
You can see a full list of functions available in Turtle's documentation. This is linked in the Resources section.
This code may seem a bit verbose. How about we simplify it using functions?
That looks better!
Last updated