Introduction to Python - Lesson One

Posted on 21-05-19 in Computing

This post is part 1 of the Introduction to Programming" series
Introduction to Python - Lesson One

Introduction to programming using Python and Turtle

Introduction to Python - Lesson Two

Continuing our introduction to Python using Turtle

Introduction to Python - Lesson Three

Looking at loops and conditionals in more detail

Introduction to Python - Lesson Four

Further exploration of Turtle and an introduction to colour

Introduction to Python using Turtle : A Review

A brief review of the first part of this Turtle course

Introduction to Python - Using Colour

This is an aside / extension material for students who get ahead

In this article we will cover :

  1. The importance of typing exactly
  2. Drawing in Python using Turtle
  3. Why we use variables and loops

Getting Started

First off, open up IDLE! This is the editor we will be using for writing Python. Python is a popular programming language used all over the world.

IDLE has two modes we can use :

image of IDLE interactive mode. This is a programming environment that comes with a standard Python installation

Interactive Mode Here we can type commands the computer can understand and it will run immediately. This is good for playing around and learning how things work. It will not save your work.

If we click file we can create a new file

Showing how to select creation of a new file. This allows us to write scripts, code that we want to remember for later.

This will let us write a program. Here the code is saved so we can refer back to it later!

Drawing using Turtle

Let's begin by playing with a tool in Python called Turtle. This allows us to draw complicated patterns. We will limit ourselves initially to two commands :

  • forward(units) - Moves the turtle forward units steps
  • left(degrees) - Turns the turtle left by degrees degrees
  • right(degrees) - Turns the turtle right by degrees degrees

if we're ever unsure about how something works in Python we can use help like so

Demonstrationg how to use use the help function in Python to find more information about things!

Try getting the following code to work

Demonstration of turtle.forward command

is made using

import turtle as tl

and a square can be drawn like so

turtle drawing of a square
import turtle as tl

Exercise Set 1 1. Write the code to draw the following shapes

turtle drawing of a rectangle turtle drawing of a triangle turtle drawing of a hexagon turtle drawing of a house

In these cases we should notice how tedious it is to manually type the same lines of code over and over.

The most important principle to learn in programming is called DRY. This stands for DON'T REPEAT YOURSELF. This means that we need to find a way to stop typing the sames lines over and over. We want to avoid doing this, as it is hard to alter and easy to make mistakes!

For example, a square can be written as

import turtle as tl

for _ in range(4):

Here we have used a for loop. In this case it will repeat 4 times. We will explore how this works in more detail, in future classes. Remember you can use help(range) to find out more information!

Exercise Set 2

  1. Rewrite the code for the previous shapes with loops
  2. Draw the following spiral
rectangular spiral drawn using Turtle

Here we can see we need to use variables. These are objects with names that can hold numbers, text and other useful things.

import turtle as tl

side = 100
angle = 90
sides = 4
loops = 5
increase_by = 10

for _ in range(loops*sides):
  side = side + increase_by


Exercise Set 3

  1. Alter just the variables to produce the following patterns
rectangular spiral drawn using Turtle rectangular spiral drawn using Turtle rectangular spiral drawn using Turtle rectangular spiral drawn using Turtle
  1. Draw the following patterns
rectangular spiral drawn using Turtle Exercise using loops to produce regular polygons overlapping


Today we have looked at using the left and forward commands in turtle with a for loop and variables.