# Introduction to Python - Lesson Two

Posted on 27-05-19 in Computing

##### This post is part 2 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

In this article we will cover :

1. Review of last classes material
2. Manipulation of variables
3. Introduction to conditionals

## Review

In the last class we learnt how to :

1. forward, left, right commands in Turtle
2. Using for _ in range(X) for looping
3. Using variables in place of numbers

Review Exercises

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

## Variables in more detail

In the last class we began using variables. These are useful for holding useful information we need in multiple places in our code or which we wish to alter!

For example, we can draw a spiral like

```import turtle as tl
step = 50
angle = 90

for _ in range(16):
tl.forward(step)
tl.left(angle)
step = step + 10

tl.exitonclick()
```

produces

So what's happening here? First we have created a variable and given it a value of 50, `side = 50`.

In programming we use the equals symbol to mean assignment. Everything on the right side, is stored in a variable with the name on the left. Here are some examples of valid variables :

```side = 100
angle_in_degrees = 45
height_of_student = 150
```

Note how we use the underscore (_) character to write longer variable names. In Python it is good practice to make your variable names understandable. Don't use single letter variables unless it makes sense!

```x,y = 5,10   # this is fine as x,y mean a position
i = 0  # i, short for index, is great for counting, do this.
a = 45  # this is bad! We have no idea what a is!
```

We can alter variables like we do in the spiral example. Here we have the line `side = side + 10`. The right hand side is run first. If side contains the number 50, we then add 10. The result 60 is stored as the new value of side. This is why we get a spiral!

We can do many mathematical operations in Python

• `side = side - 5` will subtract 5 from side
• `edge = edge / 2` will divide edge by 2
• `triangle_edge = triangle_edge * 2` will double triangle_edge

Let's introduce a new method called width

```pen_width = 5
tl.width(pen_width)
```

This will set the pen to be 5 units wide!

Exercise Set 1 Alter the above spiral code using width to match the folllowing! Match the operations used to the spirals we see!

## What if? Introducing Conditionals

To make anything more complicated, we will need to introduce conditionals. These are tests we can perform. If a test is True we can run that particular bit of code! Let's consider a case where we have width of 1 if the spiral side is less than or equal to 100 otherwise 10.

In code we write this as

```import turtle as tl
step = 30
angle = 90

for _ in range(16):
if step <= 100:
pen_width = 1
else:
pen_width = 10

tl.width(pen_width)
tl.forward(step)
tl.left(angle)
step = step + 10

tl.exitonclick()
```

This produces

The key bit of code here is

```if step <= 100:
pen_width = 1
else:
pen_width = 10
```

The first line is of the form `if <TEST> :` where <TEST> is, in our cases so far, just inequalities. In later classes we will look at many more tests. We can see, like in the loop, we must indent our code. We have two indented blocks here. The first runs if the test is True, the second runs if the test is false.

Exercise Set 2

Question!

```var = 5
var < 6
```

Answer! : This would be True as 5 is less than 6.

Determine if the following tests are True or False.

1. ```edge = 2
edge >= -1
```
2. ```var = 0
var >= -2
```
3. ```edge = -2
edge < 4
```
4. ```edge = 7
edge >= -5
```
5. ```side = 0
side <= -4
```
6. ```edge = 7
edge > 7
```
7. ```square_length = -1
square_length <= 4
```
8. ```edge = 7
edge <= -1
```
9. ```square_length = 2
square_length < 9
```
10. ```var = 1
var <= -4
```
11. ```var = 0
var > 4
```
12. ```side = 1
side >= -1
```
13. ```var = 4
var < -2
```
14. ```var = 4
var >= 4
```
15. ```square_length = 1
square_length < -5
```
16. ```square_length = 9
square_length <= 3
```
17. ```var = -2
var <= 9
```
18. ```square_length = -1
square_length > 9
```
19. ```var = 3
var >= 8
```
20. ```edge = 1
edge <= 4
```
1. Alter the conditional spiral code the produce the following images

## Conclusion

Today we have learnt the width method, looked at more complex uses of variables and concluded with an introduction to conditionals.