CSC1034: Lecture 1
Module Organisation
• There are three assessments, each based around a practical
• Lectures
• Practicals
• Online Material
• Lectures: Will be available on recap
Material
All the material is available in Canvas.
The python sources used in these notes are all available, also linked from canvas.
• If the script has the line ## Status: Crash, then I am expecting it to crash
• A few have the line ## Status: Shell, then it is designed to be typed into a python shell
• If it has neither, then it is a runnable python script.
Expectations
• The in person lectures provide the broad outline for your work
• The online material covers the whole syllabus
• You should expect to spent 3hrs a week on the online material
• You should spent four hours a week on practicals and the assessment
Lectures/Online
• There are three weeks of material on canvas
• In the last week, we will recap, do some exercises
• And give you time to finish the coursework
Practicals
• There are four hours of practicals a week
• The practicals are supported by a number of demonstrators
• They are the best opportunity to get one to one support
Other forms of Support
• I will be available during most practicals
• Open for questions at the end of in person lectures
• You can email
• But I am one person, so may be slow in replying
• Teams site (instructions on canvas)
Questions
• Any Questions
What we teach
• This is an introductory module
• Has a very wide mix of backgrounds
• We will touch on all aspects of programming
– but will not go deep on any parts
• The written material describes what you should know
• But for many it will not be enough
How to learn
• Programming requires theoretical understanding
• But it is also a skill
• You must practice
• And you must use other sources of information besides the material here
Tutorials
• Programming books come in different styles
• Many designed for existing programmers, learning a new language
• https://docs.python.org/3/tutorial/
• Can be good, but hard to read
• Often explain what you can do, but not why
Tutorials
• Some are designed to do specific things
• https://automatetheboringstuff.com/
• Can be rewarding because you can “scratch your own itch”
• https://inventwithpython.com/pygame/
• Or leave you with a good knowledge of one area but not another
Tutorials
• Some are designed to teaching programming
• http://openbookproject.net/thinkcs/python/english3e/
• Gives an overview of everything
• But can leave you struggling to apply things to your own problems
Tutorials
• Are lots of varieties of learning styles
• Book like websites (start at the beginning)
• Interactive workthroughs
• Videos
• Daily Puzzles
How to Learn
• You must engage with the language and with programming
• Look at several different resources at once
• Work through materials
– Don’t stop if you don’t understand something
– Do stop if you don’t understand anything
How to Learn
• Programming is like learning a human language
• Be a parrot
• Fiddle with things
• Type (NOT cut-and-paste) code in
• Try changing it a bit
How to Learn
• We will also teach professional tooling
• These will be new to many
• Adds complexity and can be confusing
• But these tools are also there to help
• Learning these will help you to learn
– Hopefully like a pole, not buckets on your feet
How to Learn
• For a developer, learning new langauges is a lifetime occupation
• Much, much easier than human languages
• You will see the same ideas again and again
• Although every language has a head scratcher
• I can usually become productive in a week
• To get good at a language takes quite a bit longer
Programming vs Development
• Have split the material into:
– Programming
– Software Development
• This is somewhat artifical but useful
Programming
• Two key points to learning to program
– You need to learn what you want to say
– And how to say it
• With normal language, first is easy and second is hard
• With computers, it is the other way around
Programming
• Programming is rich in best practices
– Do it this way and it will work better
• Programming is rich in conventions
– Do it this way because everyone else does it this way
• Can be hard to distinguish the two
Questions
Polling! (by shouting)
Language (Python)
• Interpreted language, with (largely useless) shell
• Somewhat OO, more multi paradigm these days
• Started off as a system admin language
• Slowly took over in science because everyone hated Perl
• With the advent of NumPy and pandas hit the data science wave
Why Python
• Commonly used, very popular
• Many of you will have seen it before
• Different semantically from Java
Why Not Python
• Many of you will have seen it before
• Syntactically rather similar to Java
• Whitespace
Programming Languages
• Are primarily written for humans to understand
• Computers come second
• Most languages use (one or more) idioms to support this
Demo: Hello World and Calculating
• Hello World in Python
• And in PyCharm
• And calculating
Exercises
• There are lots of exercises
• “Look up the formula for celsius to farenheit conversion and work out temperature that water freezes and boils at in F.”
• Formula F = C * 9/5 + 32, C = F - 32 * 5 / 9
Python
• Will now through the key points that you should be learning this week
Python: Types
• Values have types
• So far, we have created two numeric types
– Integer, Float
• Also, can deal with complex number
• Python will coerce between numeric types
>>> ## Status: Shell
>>> ## Simple Addition of integers >>> 2 + 2
4
>>> ## Addition of floats
>>> 2 .0 + 2 .0
4.0
>>> ## Addition of mixed types >>> 2 + 2 .5
4.5
>>> ## "True" Division
>>> 5 / 2
2.5
>>> ## "Int" Division
>>> 5 // 2
2
>>> ## Pointless "Int" Division of floats
>>> 5 // 2 .0
2.0
>>> ## And complex >>> 3j + 4j
7j
>>> ## And combined
>>> 1 + 3j (1+3j)
Python and Logic
• Values can also represent Boolean values
• True or False in python
• Note the upper case!
• Also, with a range of logical operators
>>> ## Status: Shell
>>> True
True
>>> False
False
>>> True and True
True
>>> True and False
False
>>> True or False
True
>>> True and not False
True
Python and strings
• Python will coerce between numeric types
• But not strings and numerics
## Status: Crash
2 + "2"
Traceback (most recent call last):
File "/home/phillord/documents/teaching/2023-24/csc1034/dev-repo/lectures-1/python/type_m
2 + "2"
TypeError: unsupported operand type(s) for +: ' int ' and ' str '
Variables
• We can assign a value to a variable
• In python a variable is defined using a name
• Aside: most examples are now not using the shell
• If we wish to see things in the output, we need to print them
Variables
x = 2
y = 2
print (x + y) 4
Variables
x = 200
• x – the identifier
• = – the assignment
• 200 – the value
Variables: identifiers
• Getting the naming wrong is a common mistake
• Python normally signals with a NameError
• Coming up with good names is hard
• Follow Python coding conventions
• Do not favour short names just because they are less typing
Lists
• What if we want to store many values?
students_in_room = 100
student1 = "John" student2 = "Paul" student3 = "George" student4 = "Ringo"
• But what if we do not know how many values? ## Lists
• Think of it like a set of pigeon holes
• Lists are values, so can be held by variables
• We can access individual values by index
• Lists start from 0
## Status: Shell
students_in_room = 100
student_names = [ "John" , "Paul" , "George" , "Ringo"]
student_names[0] student_names[3]
>>> ## Status: Shell
>>> students_in_room = 100
>>> student_names = ["John", "Paul", "George", "Ringo"]
>>> student_names[0]
' John '
>>> student_names[3]
' Ringo '
Dictionaries
• Sometimes, however, numbers are just not convenient
• Say, we need to store some attributes of a train
• We could do this:
## 0 - Name
## 1 - Colour
## 2 - Engine Type ## 3 - Train Type ## 4 - Role
wilson = [ "wilson" , "red" , "diesel" , "EMD F3" , "Trainee"]
print (wilson)
[ ' wilson ' , ' red ' , ' diesel ' , ' EMD F3 ' , ' Trainee ']
• But this is painful. Hard to remember, easy to get wrong
Dictionaries
• It is much better to use a dictionary
• Very similar to a list but with names
• The map between keys and values
wilson = {
"name" : "wilson" ,
"colour" : "red" ,
"engine" : "diesel" , "type" : "EMD F3" ,
"role" : "Trainee"
}
print (wilson)
{ ' name ' : ' wilson ' , ' colour ' : ' red ' , ' engine ' : ' diesel ' , ' type ' : ' EMD F3 ' , ' role ' : ' Trainee '}
Operations
• Now that we have our collections, what can we do with them?
• Mostly, we want to do something to everything
• Python has a number of ways of achieving this
• The list comprehension is probably the most succinct
## Status: Shell
y = [1 ,2 ,3 ,4 ,5]
## square every element in y
[x**2 for x in y]
## or in the given list
[x**2 for x in [1 ,2 ,3 ,4 ,5]]
## we can use any variable for the iterator variable
[n**2 for n in y]
## even this!
[y**2 for y in y]
>>> ## Status: Shell
>>> y = [1,2,3,4,5]
>>> ## square every element in y >>> [x**2 for x in y]
[1, 4, 9, 16, 25]
>>> ## or in the given list
>>> [x**2 for x in [1,2,3,4,5]] [1, 4, 9, 16, 25]
>>> ## we can use any variable for the iterator variable
>>> [n**2 for n in y] [1, 4, 9, 16, 25]
>>> ## even this!
>>> [y**2 for y in y] [1, 4, 9, 16, 25]
Operations
• Combined with the range function can be very powerful
## Status: Shell
[x**2 for x in range (100)] >>> ## Status: Shell
>>> [x**2 for x in range(100)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400,
Conditions
• Python also uses blocks for conditions
• Sometimes, want to do different things at different times
• We have a condition “if this then do that”
x=2
if ( x > 10 ): print (True)
else :
print (False)
x=20
if ( x > 10 ): print (True)
else :
print (False)
False True
Functions
• Functions are a critical part of programming
• Most programs consist of a large number of functions
• When it was created, Python was largely procedural
• Over the years, it has gained many more functional features
• We have already seen many functions: print, list and sorted are all example
Functions
• A function takes a number of parameters and returns a value
def add(x,y):
return x+y print (add(2 , 3)) 5
Functions
def add(x,y):
return x + y
• def – function definition coming up
• add – an identifier or name for the function
• () – here come the parameters
• x,y – the parameters of the function
• : – here comes a block
• return – return the result of this statement
• x+y add x and y
Programming Conclusion
• I have taken you rapidly through all the key elements of Python
• I have touched very briefly on each item
• There is more material on canvas
• And a variety of exercises
• Try them!
Development
• Writing good software is many things
• It’s not just programming
• Here is my list with arbitrary definitions
– Programming: Writing clean, efficient, well-documented code that makes sensible use of the language of choice
– Algorithms: Understand (enough) of the mathematics of computation
– User Design: Making sure your software does something people actu- ally want
– Testing: Making sure that the software does what you think it does
– Architecture: Defining the interfaces of software modules so they fit together
Software Development
• These slides are about modern software development
• Partly about tools, Partly about practices
• Some of these you will have seen before
• Some will be unfamiliar
• Some are hard to motivate and understand
– Mostly, until you have hit the problem they solve
Versioning
• Versioning tools are, I think, the single most important tool
• But why do we want it?
Motivation
• “Well, it was working last night”
• Change code to add feature
• Find it breaks something
• Change code back, to find it’s still broken
Motivation
• “Which idiot did that????”
• A weird bug shows up
• You need to find who caused it
• Alas, it often turns out to be you
Motivation
• “What did the idiot do?”
• A weird bug shows up
• You need to know exactly what the change was
• Again, you may well be the idiot
• Programming involves a lot of short-term memory
• You forget very quickly what you did!
Motivation
• “I am using the software you released last year and it’s not working”
• Updating software can be slow and painful
• Particularly if many other things depend on it
• So, there may be several versions of the software around
Motivation
• “We should work together on this”
• Working on code together can be difficult
• One person can overwrite changes made by the other
• The time spent coordinating can outweigh the benefit
• “The Mythical Man Month”
Motivation
• “We should work together, but I am in a different time zone”
• The situation is worse if you can only email people
• And worse, worse if you are never awake at the same time
Version Control
• The solution to all these turns out to be version control
• “How do I control all the versions I have of a file
Versioning
• We are using git in the school
• It has become a very widely used standard
• It British slang also means an irritating, contemptable or nasty person
• Git can be a bit quirky, so the name is not inappropriate
Versioning
• In the broadest sense, it is very simple
– Write or change a bit of code
– Write a comment saying what you did
– “Save” a version
Demonstration
Writing the alphabet
Generation 0
• Pre-1982
• Change a few files
• Copy all the files
Generation 1: RCS
• RCS – Revision Control System, by Walter Tichy
• Introduced “Reverse Delta” and “Locking”
Generation 1: Reverse Delta
• Imagine we have this file:
a() b() c()
d()
Generation 1: Reverse Delta
• And we change it to this file: a() b() c() d()
e()
Generation 1: Reverse Delta
• We can say this instead:
diff 1.txt 2.txt *** 2,4 **** — 2,5 —-
b()
c()
d()
+ e()
Generation 1: Reverse Delta
• Or in RCS format which is terser:
diff -n 1.txt 2.txt
a4 1 e()
Generation 1: Reverse Delta
• But to work out the current version, I need to apply many deltas
• Store the latest version
• And delta’s backward
Generation 2: CVS
• CVS took a different approach than RCS
• Allow concurrent editing.
• CVS == Concurrent Version System
• Also, the name of a US chemist
Generation 2: Modify-Merge
• Let people do what they want
• Then fix it at the end
• Clever merging – not single file based
• Sounds terrible, but in practice works well
• Tools like Dropbox, work like this
• Except without the clever merging
Generation 2: SVN
• CVS became THE versioning system for a long time
• First release 1990
• Eventually largely replaced by Subversion (SVN)
• SVN is the same as CVS (but better)
Generation 3: DVCS
• DVCS work like a combination of RCS and SVN
• Each developer works on their own workspace
• Then there are systems for sending changes between these workspaces
• Allows many different works flows