Introduction to Python - Lesson Three

Posted on 29-05-19 in Computing

This post is part 3 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. Review of loops and conditionals
  2. Further study of conditionals
  3. Cleaner syntax for interaction with variables

Review

  • Draw the following shapes
Hint! If you're stuck review material covered in the previous class! Introducing Python to students in more detail

Alternating Behaviour using not

Important!

Make sure you understand how to draw a pentagon using loops before proceeding!

There are plenty of times we are going to want to flip between 2 (or more!) options like 1,0,1,0,1,0,...

Consider the following pattern

Here we wish to flip from thick to thin lines. Let's look at a few ways of accomplishing this!

import turtle as tl

thin = False
sides = 8
edge = 100

for _ in range(sides):
    if thin:
        width = 1
        thin = False
    else:
        width = 10
        thin = True

    tl.width(width)
    tl.forward(edge)
    tl.left(360 / sides)

tl.exitonclick()

Here we can see I've introduced a new variable, thin. This doesn't hold a round number, but what we call a boolean. A boolean can be True or False only. In the previous lesson we looked at tests that produce these.

In the code the variable thin alternates between True and False, changing the pen width.

Another way to write this is

import turtle as tl

thin = False
sides = 8
edge = 100

for _ in range(sides):

    width = 1 if thin else 10
    thin = not thin

    tl.width(width)
    tl.forward(edge)
    tl.left(360 / sides)

tl.exitonclick()

Here, I've rewritten the if statement using just two lines In width = 1 if thin else 10 we say width is 1 if thin is True, otherwise set it to be 10. This is known as the ternary operator and is very useful!

On the second line I use thin = not thin. The not keyword flips True to become False and False to become True. Again, very handy!

Exercise Set 1

Draw the following shapes


Looking at the for loop in more detail

So far we have used for _ in range(5) to repeat a section of code, in this case 5 times. Let's alter this and see some more interesting behaviour!

sides = 8
edge = 100

for j in range(sides):
    current_width = 5 * j + 1
    tl.width(current_width)
    tl.forward(edge)
    tl.left(360 / sides)

tl.exitonclick()

This gives

Here instead of the underscore I've written for j in range(sides). Here j will count through 0, 1, 2, 3, 4, 5, 6, 7. Each run, will alter the value of the variable current_width which will go through 1, 6, 11, 16, 21, 26, 31, 36.

So the question becomes, how can we alter which numbers we count through? This is where range comes in. This creates a number pattern for us to count through. Here are some examples of using it :

  • range(5) gives 0,1,2,3,4
  • range(5,10) gives 5,6,7,8,9
  • range(10,100,10) gives 10,20,30,40,50,60,70,80,90
  • range(5,0,-1) gives 5,4,3,2,1

Therefore we can alter the code to be a little nicer like so

import turtle as tl

sides = 8
edge = 100

for current_width in range(1, 40, 5):
    tl.width(current_width)
    tl.forward(edge)
    tl.left(360 / sides)

tl.exitonclick()

Remember if you type, help(range) into the interactive mode of Python, text describing how it works will be given to you!

Exercise Set 2

import turtle as tl

for width in range(1, 10):
    tl.width(width)
    step = width * 10
    tl.forward(step)

tl.exitonclick()

Produces

  • Write the code to produce the following patterns

Summary

In this post we have continued our exploration of for loop and if statement.