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!
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.
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)