2  Your First Python Code

Welcome to the first page of the course! Click on the headings below to move through the different exercises.

Let’s start where it’s traditional (and useful!) to start - printing an output out.

We can do this with the print() function.

A function is a bit of code that will perform a particular task.

Sometimes we write our own functions, but very often we’ll be using a function that someone else has created. There are lots of functions that come built in with Python, and they can do things like

  • print out a phrase or the result of a calculation
  • create a sequence of numbers
  • tell us how many numbers are in our sequence

Instead of having to write out all of the code required to tell the computer how to do something, we can just refer to the relevant function instead.

In Python, a function will always be followed by a pair of brackets. Usually - but not always - you’ll put something in them!

Now, imagine I wanted to print the word dog as my output.

The way I do this is print("dog")

Notice that I surrounded the word dog with quotation marks. This is so Python doesn’t start trying to work out what ‘dog’ is - instead it just recognises that it is something called a string - basically just a bit of text!

Print out the following words: Hello World!.

Tip

Delete the ________ inside the brackets and replace this with the text we want to print.

Print out the following words: This is the second thing I’m printing on the Python course..

Remember - you need to follow the structure print("the words I want to print")

Make sure you wrap your words in quotation marks inside the brackets!

You can use single quotation marks or double as long as they match - however, when you want to use an apostrophe, you will want to use double quotation marks for defining your string. Otherwise Python will get confused trying to match up which quotation mark belongs with which.

This will work: print("Grocer's apostrophe's drive people mad!")

And this will work: print('This sentence doesn't contain any apostrophes')

This won’t work (and will return an error): 'I can't stop writing code!'

Often when you are doing some code, you’ll want to be able to refer to the same thing multiple times - but not have to write it out each time.

For example, you might want to be able to store the identifier for a patient, then lookup several different bits of information about the same patient. Or maybe you want to set some start and end dates and run several queries against a database, using the same dates each time.

If we write these out manually each time, there’s a good chance we’ll make a mistake at some point. And the next time we want to do this in our code? We need to make sure we change it absolutely everywhere. Is there a better way?

Well, we can use things called variables. That way we only have to set something - say start_date - once, and then every time we want to use our start_date in our code, we just need to refer back to start_date rather than writing out the full date every single time.

So how can we do this? We can use an equals sign to assign the value of something to some kind of identifier like start_date or name.

For example, I could write

to create a variable called name with a value of Bob.

Notice that our variable - name - doesn’t have quotation marks around it. This is important!

But we do have quotation marks around Bob. That’s because we need to tell Python that Bob is a string - a bit of text.

The magic is now I can print the value of the variable - and it doesn’t print out name. Instead, it prints out Bob - because by assigning the value Bob to name, Python now knows that we actually want Bob!

(Note - you need to run the cell above first, or you’ll get an error when you try to run the next cell!)

Now this starts getting really powerful when we use it multiple times throughout our code.

The author of this course has a very daft dog called Monty. So let’s assign his name to the variable dog.

We’ll then compare what happens when we run print("dog") and print(dog).

Remember - "monty" is different to monty.

"dog" or 'dog' will work.

If you try copying and pasting quotation marks in from a program like Word, they might be ‘curly’ quotes - ones that have been made prettier for a document - and code doesn’t like that! This is something to watch out for further down the line.

So now we know a very useful thing - how to print an output.

You’ll actually use this a lot, particularly in your early scripts - it’s how you get Python to actually give you the results of your calculation! For example, generally if we just run the code 2 + 2, Python will do the calculation, but not tell us the answer. Not very helpful.

However, let’s see what happens if we tell it to print that.

Clever, eh? And notice something important - we didn’t put quotation marks around 2+2 when we put it in the print statement. This is because print is actually pretty smart - you can put more than just a string in your print statement and Python will work out what to do with it.

In comparison, if we put print in quotes, we’d get this output instead.

Python can do any kind of sum for us! So let’s get Python to do a slightly more complex one for us.

+ means plus
- is minus
* is multiply (e.g. 4 * 2 will equal 8)
/ is divide (e.g. 8 / 2 will equal 4)

Can you get Python to calculate 5 + 4 times 20 and print out the answer?

We’re expecting the answer to be 85!

You can write the whole sum in one go - you don’t need to break it up into chunks.

Don’t forget - you don’t need quotation marks when you want Python to actually print the answer!