Python is a programming language which is fast becoming one of the most popular in the world. By learning Python, you are not only gaining a valuable tool to help you with your mathematical studies as part of your degree, but also learning the fundamental concepts of programming using a language which is used by innumerable companies, research institutes and ordinary people all over the world.
We will be using Python like a big calculator to begin with. But the ideas that we introduce we will eventually build into small programs and you are then well on your way to being a competent programmer in a more general sense.
You will be doing your programming using a service called repl.it. It allows you to run python code through a web interface, and for your code to be checked automatically. If you click this link you will be invited to sign up for the service (You should use your QUB email address so I can identify you) and will be able to see the assignments you need to complete.
print(1+1)
print(9-5)
print(2*3)
print(100/4)
print(7**2)
You can see that python can perform all the usual numerical operations: addition (+), subtraction (-), multiplication (*), division (/) and exponentiation (i.e. 'to the power of': **).
You can also see the use of the print command to get the code to display certain things. This is important, especially for checking your code. If you want to print multiple things on the same line you have to separate them with a comma.
print (1.0, 2.0, a, b)
We can also get fancy about it and print out more information. Text can be enclosed in quotes ("double" or 'single' quotes both work, but it's better to use double quotes as that saves problems with apostrophes.)
print ("The variable 'a' has a value:", a, "and the variable 'b' is currently assigned the value:", b)
If you try to do something which the computer doesn't understand, then it will give you an error: for instance entering
1+1)
will give an error message:
Traceback (most recent call last):
File "python", line 3
1+1)
^
SyntaxError: invalid syntax
SyntaxError just means there's a problem with the way you've typed the command- and Python helpfully points to the problem with the little "^" character: there's a bracket in the wrong place. In this case it's not particularly useful, but in more complicated expressions this can be extremely useful to locate a typo. You get different errors for different things. For instance entering
1/0
will give a 'ZeroDivisionError' (clue is in the name). Then as we move on to more complicated procedures, like defining variables or calling functions, you can get different errors for incorrect usage. Don't be afraid of the error messages! Making errors is part and parcel of programming, and most of the time spent in developing large pieces of software is on debugging- finding and fixing the source of errors. It's better that you know you're getting an error- it's worse if you think the code is working and it's actually doing the wrong thing- so embrace the error messages, and use them to help fix your code.
In particular: the internet is your friend, don't be afraid just to copy the error mesage into your search bar and see what information you can find.
Anyway, back to the code: if you experiment with this idea a little bit you can see we can do all sorts of convoluted calculations. We use brackets both to change the meaning of the calculation, and to make it easy to see what's going on. For instance
print(2**14-14/2*5)
does something different from
print((2**14-14)/(2*5))
and different again from
print((2**(14-14))/(2*5))
If you try these you get different answers. You can add more brackets without changing the meaning- for instance
print((2**14-14)/(2*5))
performs exactly the same operation as
print(((2**14)-14)/(2*5))
but perhaps the second makes it clearer in what order the operations are performed. A good rule of thumb, to practice now is
when it comes to writing code, always err on the side of clarity rather than efficiency.
To complete the exercises, create an account where prompted below. Once you have signed up, you should see the exercises on your dashboard, and you should now be able to attempt exercises 0.0 and 0.1