Info1110 Notes
Info1110 Notes
Contents
INFO1110/COMP9001 Introduction to Programming 1
Week 01 5
Course outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Introduction to computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Using the terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Text editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The print function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Variables and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Shorthand assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Truth table for and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Truth table for or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Week 02 9
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
More operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Naming variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Desk checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Week 03 10
Conditional flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
if keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
elif keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
else keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Ternary operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Control flow diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Week 04 14
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Iterating through a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Modifying a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Filtering elements in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Week 05 17
The type function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Why use functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Ending a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The difference between print and return . . . . . . . . . . . . . . . . . . 19
Best practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
General rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Week 06 20
Streams and files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Input and output streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
File types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
File paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
File modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Opening files in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Reading from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Writing to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Using with to automatically close a file . . . . . . . . . . . . . . . . . . . . 24
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Difference between TypeError and ValueError . . . . . . . . . . . . . . . 24
Using try and except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Using finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Handling multiple exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 26
Week 07 27
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Types of tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
What to test? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Important keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Writing assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Writing in/out tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Comparing floating point numbers . . . . . . . . . . . . . . . . . . . . . . . . . 29
Week 08 30
List idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Reading input until a sentinal value . . . . . . . . . . . . . . . . . . . . . 30
Finding a specific value in a list . . . . . . . . . . . . . . . . . . . . . . . . 30
Counting the number of occurences in a list . . . . . . . . . . . . . . . . . 31
Finding the max/min value in a list . . . . . . . . . . . . . . . . . . . . . 31
Filtering a list in-place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Filtering a list, without mutation . . . . . . . . . . . . . . . . . . . . . . . 33
Week 09 34
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Instance vs class attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Instance vs class methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Week 10 36
Test methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Test-driven development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2
Week 11 37
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Why use recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Why shouldn’t we use recursion? . . . . . . . . . . . . . . . . . . . . . . . 37
Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Writing a recursive function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Cumulative sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Factorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Week 12 40
Lists redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Iterating through a 2D list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Arrays vs lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Why use arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The numpy package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Importing numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Creating an array in numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Iterating through a 2D numpy array . . . . . . . . . . . . . . . . . . . . . . 43
Week 13 44
Revision questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
General programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
The following notes and examples are written to target Python version 3.4 and above.
Code blocks beginning with %%bash contain bash commands which can be run in the
terminal.
3
which you can learn and grow. Chances are that when you make a mistake, it’s
more likely you’ll remember it and then never make the same mistake again.
2. Ask for help when you need it
The staff are there to help you. If you don’t quite understand a concept, don’t
just ignore it. Ask for help! It’s better to be constantly asking for help rather
than letting it pile up and realising before the final that you don’t understand
anything at all. If you’re afraid of asking in person, you can use Ed, or research
online. There are plenty of resources out there: Stackoverflow, YouTube, online
tutorials, etc.
3. Nail the fundamentals
All the concepts build upon each other. Make sure you have a solid grasp of the
fundamentals. Remember that this is an introduction to programming, so you
want to be building a good understanding of the concepts. A good base means
that you are able to learn harder concepts and different languages faster.
4. Keep up to date (this includes lecture content and tutorial work)
The workload builds up, so it’s important to keep up to date. It’s understandable
that uni gets busy, but try at least to be up to date with the previous week’s work
before the next week starts. This way you aren’t cramming right before a quiz or
an exam.
5. Go to the tutorials
The tutorials are a great place for learning and getting help if you need it. Im-
portant concepts are often reviewed and emphasised. There’s also a weekly task
that counts towards your attendance/overall grade. They’re easy marks that
you shouldn’t waste - especially if you don’t feel confident about the final.
6. Do the best that you can throughout the semester and before the finals
A 60% weighted final is a lot of pressure on just one paper, so make sure your
cumulative mark up until that point is the best that it can be. It places a lot less
pressure on you and gives you that extra bit of confidence. Plus, doing well
during the semester is a good indicator that you know your content, so that’s
always good.
7. Practise! Practise! Practise!
If this final point is the only thing you take away from all these tips, I’d be happy.
Remember that coding is something that requires time, work and practise. If
you’re a beginner programmer and you want to do well, you have to put in the
effort. If you’re struggling to understand a particular concept, do more problems
on that topic. You aren’t just limited to tutorial problems, there are so many sites
online. If you want somewhere to get started, I recommend checking out
• CodingBat, https://codingbat.com/python
• HackerRank, https://www.hackerrank.com/domains/python
• ProjectEuler, https://projecteuler.net/archives
4
Week 01
Course outline
Introduction to computers
A program is a set of instructions that a computer can understand, and then execute.
A compiler will translate a source code into machine code.
• Write source code – code which a human can read
• Compile the code to executable code – code which a computer can read
• The computer then runs the executable code
You can tell Python to compile and run your program with the python3 command in
the terminal.
To check which version on Python you are running, enter the following command in
the terminal.
python3 --version
Instead of navigating the folders (directories) on your computer using File Explorer
on Windows or Finder on MacOS, you can navigate your computer using commands
in the terminal. Here are the important ones to know.
Action Command
To show your current folder pwd
To list files in the current folder ls
To make a new folder mkdir [DIRECTORY_NAME]
To move into a folder cd [DIRECTORY_NAME]
To go back to your home folder cd ~
5
• cd: Change directory
Text editors
The print function can be used to print things to the terminal. You need to invoke the
print function with parentheses () and pass inside the parenthesis what you want it
to print.
print("Hello, world!")
print(42)
Hello, world!
42
Everything on your computer is stored as zeros and ones (binary), each zero or one is
known as a bit. 8 bits make 1 byte.
A variable is memory space, reserved by a name (identifier). There are two types of
variables:
1. Primitive: these use a fixed amount of memory
2. Object: memory used for objects can changed over time
Note: In python, everything is an object. Objects are covered in more detail
in week 6.
When working with variables:
1. DECLARE a variable
2. INITIALISE the variable
3. USE the variable
By declaring variables, You can now refer to the reserved memory space by its name.
Give your variable a value by initializing it. If not initialized, the memory space will
be filled by a default value (which differs between machines).
Variables in python are function scoped. A variable declared within a func-
tion can only be used inside that function.
Expressions are a combination of variables and other items which can be evaluated,
and will produce a value. We construct expressions using operators
6
Operators
10
2
24
1.5
We also have
• % modulo/modulus
• // floor divide
print(6 % 4)
print(6 // 4)
2
1
Assignment
= is the assignment operator. Values on the LEFT are assigned values on the RIGHT.
x = 10
print(x)
10
In the code above, the value 10 is being assigned to the variable x. The variable x is
then evaluated before it is passed into the print function.
Shorthand assignment
x += n # is equivalent to x = x + n
x -= n # is equivalent to x = x - n
x *= n # is equivalent to x = x * n
x /= n # is equivalent to n = x / n
x = 10
x += 2
7
print(x)
x = 10
x -= 2
print(x)
x = 10
x *= 2
print(x)
x = 10
x /= 2
print(x)
12
8
20
5.0
Equality
== is the equality operator. It compares the value on the LEFT to the value on the right.
print(10 == 5 + 5)
print(10 == 1 + 5)
True
False
Boolean operators
8
def my_function():
"""This is a docstring."""
pass
def next_hailstone_number(n):
"""
Returns the hailstone number following the number `n`.
This function assumes `n` is an integer. If `n` is not an␣
!→integer,
Week 06
File types
File paths
Files are stored in your file system and can be referenced by a path.
A file path can be written in two ways:
20
1. Absolute path (from root directory, \)
2. Relative path (relative destination from current working directory)
File modes
When you open a file for reading (read mode), the program assumes the file exists and
you have permission to read from the file.
When you open a file for writing (write mode), you always begin with an empty file.
• If file already exists, program will override
• If file does not exist, program will create it
To add to the end of an existing file, you will want to append (append mode).
The following code writes the string contents to a file called sample.txt. After running
the below command in your terminal, you should be able to see a new file with the ls
command.
%%bash
cat << EOF > sample.txt
Apple
Banana
Carrot
EOF
f = open("sample.txt", "r")
print(f)
f.close()
21