📚 Library

6th Grade 💻 Coding

A library in programming is a collection of ready-made functions and tools that someone else wrote and tested, which you can add to your program instead of writing everything from scratch.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

In Python you add a library with import. import random brings in a library of random numbers, and then random.randint(1, 6) picks a number like rolling a die. import math brings in math functions, like square root. There are libraries for drawing, working with pictures, building websites and much more.

Thanks to libraries, programmers “stand on the shoulders of giants”: they use the work of thousands of other people and focus on what’s new in their own project. Just make sure to use libraries from well-known, trusted sources, because other people’s code runs inside your program.

For example

  • Python’s turtle library lets you draw with a “turtle” that moves around the screen — a lot like a sprite in Scratch.
  • A game that needs to keep track of time uses the time library instead of counting seconds on its own.
  • Just as you don’t bake bread every time you want a sandwich, programmers don’t rewrite functions that already exist in a good library.

📌 Remember: Library = a ready-made toolbox. import = open the box.

Quick quiz

  1. What is a library in programming?

  2. How do you add a library in Python?

  3. Why should you use libraries from well-known sources?

🔎 Ask Thinkapedia another question