Student Summer Sprint
  • Welcome to Hackathons for Schools Student Summer Sprint!
  • Event Information
  • What is the Student Summer Sprint?
  • Useful Event Information and Links
  • Schedule
  • Our Organisers, Panelists and Mentors
    • Organisers
    • Panelists and Speakers
    • Mentors
  • Project Details
    • Themes
    • Judging Criteria and Winning Teams
    • Presentation Advice
    • Submission Checklist and FAQs
  • Coding Platform and GitLab
  • Technical Workshops
    • How to Use Git
      • Git and the Terminal
      • How to use Git - The Basics
      • Branching, Merging and Other Useful Commands
      • How to use Git - Further Resources
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • Web Development - Resources
    • Introduction to Python
      • Variables
      • Data Types and String Formatting
      • Input and Output
      • Conditional Statements
      • Functions
      • Libraries
      • CHALLENGES
        • Solutions
      • Learning Python - Resources
    • Discord Bots in Python
      • Discord Bots - Resources
    • Web Scraping in Python
  • Careers Advice and Opportunities
    • University Advice
      • University Advice - Further Resources
    • CV, Applications and Interviews
      • Creating a Great CV
      • UCAS Personal Statement Advice
      • Interview Hints and Tips
      • Ultimate LinkedIn Guide
      • Applications - Further Resources
    • Different routes into a tech career
      • Different Routes into Tech - Further Resources
    • What Now?
      • More Opportunities!
Powered by GitBook
On this page

Was this helpful?

  1. Technical Workshops
  2. Introduction to Python

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:

import turtle

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:

import turtle

t = turtle.Turtle()

t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)

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?

import turtle

t = turtle.Turtle()

def forwardRight():
    t.forward(100)
    t.right(90)

forwardRight()
forwardRight()
forwardRight()
forwardRight()

That looks better!

PreviousFunctionsNextCHALLENGES

Last updated 4 years ago

Was this helpful?