Python beginners

1. Introduction

This course is intended to help teach you some basic python skills.

It is intended for people who have never done python before, or people who want to brush up on their basic python skills.

For some of the more complicated tasks there are code solutions, but only use these if you are done or if you are really stuck (it's probably better to ask for help instead of looking at the answer though).

Some of the challenges will be tough and you will probably find it frustrating at times but that is how you learn! They've been designed to be difficult to try and get you to think.

If you read everything carefully and ask for help with things that make you confused you should be fine.

Click on "Getting started" to open the next tutorial.

2. Getting started

To get started coding python you'll need to setup an editor where you can type and run python.

Note: We are learning using python3 (python2 is now outdated).

Choose one of these methods:

1. Mu python (best)

You can download Mu from their website.

Open mu-editor and you should see something like this:

Mu main screen

2. Online (fastest)

The fastest way to get going with python is using an online editor such as repl.

3. Terminal (advanced)

If you are a bit more advanced you might want to use the terminal or command prompt to run python.

Download python 3 if you are on windows make sure you select "Add python to PATH".

Create a folder where you will put your code.

In either terminal or command prompt (depending on your OS) use the command cd to change to the directory where python. To run the code use the command python3 {NAME_OF_FILE} (where {NAME_OF_FILE} is the name of your python file).

3. Hello World

Whenever we learn a new programming language normally the first thing we do is to say hello world.

In python we would use the print command to say things e.g.

print("Text that you want to print out")

Mu help

If you are using Mu then once you've typed in your code you need to press the Run button.

Mu hello world

Then you need to save the file:

Mu save

You should see this output:

Mu output

You can call it whatever you want, in this example I call it python_introduction. Later in this course you might want to create multiple files, just make sure you save them with unique names.

Try it

  1. Change the print command to say Hello world
  2. Run it and check that it outputs what you expect

Tip: The quotation marks around the text are important! They tell python that we want it to treat it as text instead of code.

4. Comments

Sometimes when we program we want to leave comments to ourselves explaining what the code does.

In python anything after a # is ignored

# This isn't valid python code but it doesn't matter because of the hashtag
print("Actual python code")

Try it

Remove the hashtag from the code above and see what happens (hint: you should get a syntax error).

This is what happens on Mu when you try to run the program without the hashtag:

Mu syntax error

5. Variables

If we want to store data we use something called variables. They are incredibly important in coding and we'll be using many more in the future.

Each variable has a name and data associated with it.

Copy and run this code and see what happens:

# The name `my_variable` can be anything as long as they are no spaces and doesn't begin with numbers
my_variable = 10
print(my_variable)

# The quotation marks here are important, see what happens when you delete them
my_name = "{ENTER_YOUR_NAME}"
print("Hello" + my_name)

# Pick your favourite number
my_favourite_number = 7
# The str() is VERY important read below for details
print("Your favourite number is " + str(my_favourite_number))

Interesting information

On the line that said:

print("Your favourite number is " + str(my_favourite_number))

We had to use a function called str(...). Try deleting str(...) and see what happens (leave my_favourite_number).

It should say: TypeError: can only concatenate str (not "int") to str.

Essentially what that means is that we can only join strings to strings. A string is another word for text.

my_favourite_number is a number (called an int or integer) so we need to convert it to a string. That's what the str function does.

Task

Create a variable called my_age and set it equal to your age. Then make a print statement say Your age is {YOUR_AGE} (where {YOUR_AGE} is the variable).

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution.

6. Input

So far our programs have been a bit limited. Now we are going to learn how to make our programs interactive.

We're going to use a new function called input which asks the user a question. We pass in a string to input which is the message that is printed to the user before they enter their response (i.e. input("SOME_MESSAGE")).

Try running this program: (it should ask you for your name, just type it in and then hit enter)

name = input("Enter your name: ")
print("Hello " + name)

Try it

Create a program that asks a user for their favourite colour and then prints {THEIR_COLOUR} is my favourite colour too! (replace {THEIR_COLOUR} with the colour they pick - use a variable).

Bonus

If we look at the example we have put a space at the end of the input string "Enter your name: ", why is that? Is it important?

Also why is there a space in the print statement "Hello " + name? Is this important?

Before you read on, play around running the example code with and without the spaces and try to think about those two questions.

So the answer: For the input it just creates a nice bit of space after the question otherwise the text the user enters will be directly after the :. This is just aesthetic (aesthetic means how something looks as opposed to how something works) and it doesn't really affect much about how the code runs.

We have a space in the print for a similar reason: otherwise "Hello" and name would be right next to each other with no space.

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the colour task.

7. Lists

Lists allow us to store a group of items in one variable.

To get started we need to learn some terminology:

  • Each item in a list is called an element.
  • In most programming languages (including python) we start counting from 0 so the first element of the list is at index (aka position) 0, and the second is at position 1 and so on...

Read through the code (including the comments) and then ry running this example:

# Create a list with the elements 1, 2 and 3
numbers = [1, 2, 3]

# Prints out the entire list (you should see "[1, 2, 3]" in your console)
print(numbers)

# Print the first element on the list (you should see "1" in your console)
print(numbers[0])

# Add a new number to the list
numbers.append(20)
# Now we should see "[1, 2, 3, 20]" in the console
print(numbers)

# Change the thrid number to be 42
numbers[2] = 42
# Now we should see "[1, 2, 42, 20]" in the console
print(numbers)

# We can remove elements by value
numbers.remove(42)
# Now we should see "[1, 2, 20]" in the console
print(numbers)

# We can also remove elements by their index (aka position)
# Here we delete the first element
del numbers[0]
# Now we should see "[2, 20]" in the console
print(numbers)

# We can also find the length of a list using the len function (at this point it should be 2)
print(len(numbers))

Task

  1. Create an empty list called shopping_list
  2. Add several items (at least 5) to this list e.g. milk, eggs
  3. Print the list
  4. Pick two elements and remove them using shopping_list.remove(...)
  5. Print the list again (now that the items have been removed)

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the number guessing task.

8. Conditionals

if statments are how we make decisions in our code, in general they are called conditionals.

In general they follow this pattern:

if EXPRESSION_1:
  # If EXPRESSION_1 is true do this:
  do(this)
elif EXPRESSION_2: # elif is short for "else if"
  # If EXPRESSION_1 is false AND EXPRESSION_2 is true then do this:
  do(something_else)
else:
  # If EXPRESSION_1 AND EXPRESSION_2 are both false do this:
  do(something_else_entirely)

Try running the example below and run it multiple times so you can see both messages (run once with 'blue' and once with another colour):

Watch out: After the : at the end of the line you must indent your code (hit the tab key, or your editor may do it automatically).

colour = input("What is your favourite colour? ")

if colour == 'blue':
  print("That's my favourite colour too!")
else:
  print(colour + " isn't my favourite colour, blue is!")

Try it

  1. Choose a secret number between 1 and 99.
  2. Ask the user to guess what the secret number is.
  3. If the user guesses the correct secret number then say they're in the secret area of your program.
  4. If their guess is less than the secret number (Hint: if their_guess < secret_number:) then tell them to pick a higher number next time
  5. If their guess is higher than the secret number then tell them to pick a lower number next time.

Watch out: Remember when we had to use str to convert the number to a string to print it out? Well, when we read the guess from the input we need to convert it to a number. To do that we use the int function (e.g. int(input("..."))).

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the number guessing task.

9. Loops (i)

For this tutorial and the next two we're going to be improving the guessing game code from the last tutorial so make sure you have a working copy of it (save it and create a new file for the rest of the tasks).

We're first going to learn about while loops.

They follow the pattern:

while EXPRESSION:
  # Keep running the code in the while loop as long as EXPRESSION is true
  do(something)

EXPRESSION is just a placeholder for some condition we might have such as in the if statements, e.g. while colour != "red": which will run again and again as long as the variable colour is not "red".

Indentation

Again like the if statements the indentation is important as it indicates what is within the while loop.

Here is an example demonstrating this:

# Set the initial value of colour so that it is defined
colour = ""
while colour != "red":
  # This line is indented so it is part of the while loop
  colour = input("Choose the correct colour: ")

# Since this line isn't indented it isn't in the while loop
print("You guessed the correct colour")

# This line is indented when it shouldn't be
# This will cause a syntax error
  print("This causes an error")

Delete the last print that causes the error and then run the code.

Task

Now that you understand while loops, we're going to improve our guessing game code from earlier.

  1. Add a while loop to your guessing game code so that it will keep on running until you guess correctly
  2. If their guess is less than the secret number then tell them to pick a higher number
  3. If their guess is more than the secret number then tell them to pick a lower number
  4. Once they guess correctly tell them they are correct (hint: have this after the end of the while loop)

Hint: set their guess to be 0 before the start of the while loop otherwise you'll get an error saying that it isn't defined.

Get a friend to play the game and see if they can guess your secret number!

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the number guessing task with the while loop.

10. Loops (ii)

We're going to adapt the code from last time to only allow 3 guesses. This is going to be quite tricky since your going to have to work this out yourself but you'll get some hints.

You'll need to create a counter variable to keep track of the number of guesses e.g. guess_counter = 0.

At the end of the while loop you'll want to add one to the counter each time guess_counter = guess_counter + 1.

You'll need to add a check in the while loop to check if the maximum guesses have been reached. To do this you'll need to write (after you increase the guess_counter):

if guess_counter == 3:
  break

break exits the while loop early.

Right now there are two conditions for whether we exit the while loop either:

  1. We guessed correctly
  2. or we reached the maximum number of guesses

We need to check which one happened, so we should add an if statement to check if the guess was correct (and print the message saying they guessed correctly). We should then add an else statement which means that they reached the maximum number of guesses so we should print You have reached your maximum number of guesses!

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution.

11. Randomness

So far our guessing game has been a bit boring for us since we know the answer already since we have to type it in ourselves.

What if we could get python to choose a random number?

Well we can, but first we need to learn about import.

There are loads of python features that are only accessible by using import. Python uses import because it reduces clutter (mess) so our code is clearer without getting confused with unfamiliar features.

When we import something we bring it into scope so we can use it.

To generate random numbers we need to import a module called random:

import random

Next we need to work out what function from random to use. To do this we can look at the real documentation for this module here.

If we scroll down on this page we find this definition:

randint documentation

This wording is a bit confusing but essentially it means that we pass in two numbers a and b and the function returns a number which is between a and b inclusive (inclusive means that the number is not only between a and b but it could also be a or b).

import random

random.randint(a, b)

Task

  1. Change your secret_number in the guessing game to be a randomly generated number between 1 and 99 (inclusive) using randint.
  2. If the user runs out of guesses tell them what the number was.
  3. Add a scoring system (see below).

Come up with a scoring system so that if the user guesses correctly they get 100 points and then if they don't get it give them a score based on how close they were to the actual number.

Scoring system

  1. If the user guesses correctly they should get 100 points.
  2. If the user runs out of guesses then we should calculate a score for their last guess.
  3. The score should be 100 - distance from the correct answer.

e.g. If your last guess was 50 and the answer was 52, you get 98 points. If your last guess was 42 and the answer was 30, you should get 12.

Hint: when we calculate the points we are probably going to do some sort of subtraction to give us the distance from the correct answer. Unfortunately depending on whether the guess was higher or lower than the answer the distance may be negative or positive. To fix this we use a function called abs(x) which takes in a value and if it's negative then it makes it positive.

If you get this working we should have a pretty good guessing game!

Try changing the number of allowed guesses and see if you can come up with any good strategies for winning.

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the final version of the guessing game.

12. For loops and lists

A for loop is another kind of loop but it is designed to be very easy to loop over lists.

Try running this example (iteration means one cycle of a loop):

example_list = ["one", "two", "three"]

# Word gets set to be the next element of the list each iteration of the loop
for word in example_list:
  print(word)

Here is an example where we sum the elements in the list:

numbers = [5, 2, 10, 4]

count = 0

for number in numbers:
  # count += number is a shorthand for count = count + number
  count += number

# Prints 21
print(count)

Task

We're going to improve our shopping list from earlier to make it more dynamic (hint: it may be useful to look at the list section for help using lists).

  1. Using a while loop ask to user to keep on entering items into a shopping list until they type STOP
  2. Using a for loop print all the elements of the list

Hint: If you're unsure how to write the loop which ends where the user types STOP you could write the while loop using while True: (which means it runs forever) and then use and if statment and a break statement to exit when the user types STOP (there are other methods if you can think of them).

Optional Bonus

Add some more features such as:

  • After the user enters the items into the list have another loop that lets the user delete items until they type STOP.
  • When the items are printed out also print the number next to it (e.g. 1. milk, 2. eggs ...). You'll need a counter to do this.

Ask people around you what they think would be useful for a shopping list or think of improvements yourself and then code it!

Solution

If you've done and want to check your answer (or you have got really stuck) then here is the solution to the task (not the bonus).

13. Conclusion

You've now learnt some python!

When you get home you could try writing programs to help you with your math homework!

If you want to learn more you can look up python tutorials online but in future we will have more advanced tutorials helping you make games, draw shapes and write more advanced programs.

If you noticed any issues with this course such as typos or bugs tell someone or you can report them youself here.