Week 2
Programming Languages and Python
Recap
Last time we discussed how we can use pseudocode to break down complex tasks into manageable chunks, before starting to issue exact instructions. We saw this in action during the jam sandwich demonstration.
And while pseudocode is nice, we ultimately need a consistent way of telling computers what to do - To this end, programming languages have been developed which allow allow programmers to write fairly readable code which is then converted into in machine-level binary code by a compiler (luckily, we don't need to worry about this).
Python
Why Python?
Easy to learn Python reads almost like English, just with a few extra rules thrown in - at least when compared to some other popular programming languages!
Fairly fast As a rule of thumb, one can usually assume that the more readable a language is, the slower and less customization it is. However, python manages can still be very fast when used sensibly, especially for routine data science and machine learning tasks.
Popular Python's popularity means that many libraries have been written for it - these allow us to do everything from data science through to interacting with spotify and twitter.
Additionally, python is an increasingly popular language in industry. It is used extensively for data science and machine learning work (especially at Google) as well as for prototyping new technologies (because it's quick and easy to get stuff working in it).
How Python?
For the purposes of this course we'll be using the Anaconda python distribution, with python version 3.7
This is because the Anaconda distribution comes with many packages pre-installed which will come in handy later, and because it comes with the Spyder IDE (integrated-development-environment), which makes writing and running python code a breeze.
Python - Baby Steps
Syntax and Comments
The syntax of a language describes the rules and instructions which can be used when coding.
All programming languages have specific ways of including normal English inside your scripts (i.e. files filled with code) which can be used to explain the function of the code. These are referred to as comments, because they allow us to comment on the code.
In python, single-line comments are started with hashtag (#) symbols and cause following text on the same line to be ignored by python. This allows the programmer to add explanations / notes to their code such that other programmers can understand it more easily. (Comments also serve as useful reminders for the programmers themselves)
Additionally, and text enclosed between a pair of triple-double (""") quotes forms a comment (which can be multi-line):
Hello World
In Python, the print statement allows us to output text to the console:
When specifying the string which is being printed, we need to be careful to ensure that the enclosing quotes are identical - otherwise python gets confused and doesn't think the string has ended.
Variables
Variables are what we use to store information within our code, and can be thought of as labelled boxes containing data.
Declaration
pascal case vs camelcase
Maths
Operator | Description | Example | Output |
+ | Addition | 17 + 3 | 20 |
- | Subtraction | 14 - 21 | -7 |
* | Multiplication | 3 * 24 | 72 |
/ | Division | 10 / 4 | 2.5 |
** | Exponentiation | 2 ** 4 | 16 |
% | Modulo | 22 % 3 | 1 |
In the case of division, the output has a decimal point even though the operands are both integers - python is nice like that.
You will often need to be careful to not mix decimal and integer values when performing certain operations, as you may incur rounding error (we will discuss this more later on when we cover data types, and the difference between "int"s and "float"s).
Boolean Logic and Conditions
Mathematical logic essentially allows us to compare things in a well-defined, consistent way. The ability to do this is essential for allowing us to build useful computer programmes. The types of comparison operations that we can perform in python are outlines in the following table:
Operator | Description | Example | Output |
< | less than | 5 < 8 | False |
> | greater than | 4.3 > 2 | True |
== | equals | "Hello" == "there" | False |
<= | less than or equal to | 5 <= 5 | True |
>= | greater than or equal to | 8 >= 110 | False |
!= | not equal | "General" != "Kenobi" | True |
For example
If and Else
If statements execute code provided that the specified condition is satisfied
We can use conditional operators to determine when an if statement should trigger.
Coupled with variables, this allows us to control the flow of our code.
Else if
In the script above, we used an "if-else" statement, but in some cases we might want to multiple possible specific answers differently. For this we can use "elif" (short for else if) :
Input
Sometimes we want the user to give us some information, which we then use somehow in our code. Luckily, python has a built in function (aka method) which easily allows us to do this:
Now we are ready to create programs which respond differently based on the user's input
Finally, it is worth noting that we can concatenate string by using the + operator:
Last updated