Getting Started With turtle
Before you continue, there are two important things that youll need to do to make the most of this tutorial:
Python Environment: Make sure that youre familiar with your programming environment. You can use applications like IDLE or Jupyter Notebook to program with turtle. However, if youre not comfortable with them, then you can program with the REPL, which youll use in this tutorial.
Python Version: Ensure that you have version 3 of Python on your computer. If not, then you can download it from the Python website. For help setting things up, check out Python 3 Installation & Setup Guide.
The good thing about turtle is that its a built-in library, so you dont need to install any new packages. All you need to do is import the library into your Python environment, which in this case would be the REPL. Once you open your REPL application, you can run Python 3 on it by typing the following line of code:
This calls Python 3 into your REPL application and opens up the environment for you.
Before you begin your Python programming, you need to understand what a library is. In the non-computer world, a library is a place where different types of books are stored. You can access these books at any time, take whatever information you need from them, and return them to the same place.
In the computer world, a library works similarly. By definition, a library is a set of important functions and methods that you can access to make your programming easier. The Python turtle library contains all the methods and functions that youll need to create your images. To access a Python library, you need to import it into your Python environment, like this:
Now that you have turtle in your Python environment, you can begin programming with it. turtle is a graphical library, which means youll need to create a separate window (called the screen) to carry out each drawing command. You can create this screen by initializing a variable for it.
In Python, you use variables to store information that youll use later on in your program. You initialize a variable when you assign a starting value to it. Since the value of the variable isnt constant, it can change several times during the execution of your program.
Now, to open the turtle screen, you initialize a variable for it like so:
You should see a separate window open up:
This window is called the screen. Its where you can view the output of your code. The little black triangular shape in the middle of the screen is called the turtle.
However, you must also choose a name thats convenient for you to use, especially because youll be calling it very often throughout your program!
For example, choosing a name like my_turtle_screen_name would be tedious to keep typing, while a name like Joe or a would appear to be very random. Using a single alphabet character, like s in this case, would be much more suitable. Thats
In the next chapter, we’ll dive into some of Python’s basic syntax and concepts. But before that, let’s take a look at how you can run your code using IDLE or Jupyter Notebook.
IDLE is an integrated development environment (IDE) for Python. It provides a simple text editor with syntax highlighting, as well as a console window where you can execute your code line by line. To open IDLE on Windows and Mac:
– On Windows, go to Start > All Programs > IDLE (Python 3.x).
On Mac, open Finder, click Go in the menu bar at the top of the screen, then select Applications from the dropdown list. Find IDLE and double-click it to launch.
Jupyter Notebook is a web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. It’s particularly useful for data science and scientific computing because of its ability to display output inline with your code. To open Jupyter Notebook on Windows:
– Open the Anaconda Prompt (you can find it in Start > All Programs > Anaconda3). Type jupyter notebook and press Enter.
On Mac, open Finder, click Go in the menu bar at the top of the screen, then select Utilities from the dropdown list. Find Terminal and double-click it to launch. Navigate to your Anaconda installation directory (usually /anaconda3) using cd anaconda3, then type jupyter notebook and press Enter.
Once you’ve launched IDLE or Jupyter Notebook, you can start writing Python code! In the next chapter, we’ll cover some of Python’s basic syntax and concepts to help get you started.