Lecture02 Slides
Lecture02 Slides
Attendance
Register at: http://go.qub.ac.uk/finattend4
Python Basics
Content Outline
Data Structures
Functions
Developer Practices
27/09/2023, 12:14 Lecture02 slides
Data structures
Container variables are structures that can hold multiple values or
collections of values
Sequence type containers are like arrays
list , tuple , range
their elements can be enumerated/indexed
this allows their values to be accessed by their position
Unordered types include:
set which behaves like a mathematical set (no duplicates
allowed)
dict which contains key/value pairs with element values
accessed by their key
27/09/2023, 12:14 Lecture02 slides
Lists
A list is an ordered array
Stores mixed data types (e.g. integers and strings in the same list)
Allow duplicates
Created using square brackets [] and accessed via square brackets
[]
Mutable - values can be modifed after creation
Can be used to replicate stacks (LIFO) and queues (FIFO)
Have various methods to help manipulate them (e.g. insert ,
append )
27/09/2023, 12:14 Lecture02 slides
print(list5)
Empty lists
If you know in advance that you need to store n values, create a list of
the appropriate size with null values
Can use the keyword None for this
More efficient than extending a list one element at a time
In [2]: #Create a list of size 10, without populating any values
A = [None] * 10
print(A)
[None, None, None, None, None, None, None, None, None, None]
27/09/2023, 12:14 Lecture02 slides
A has 5 elements
3rd element is 5
After modifying first element [1, 3, 5, 7, 11]
27/09/2023, 12:14 Lecture02 slides
Tuple
A tuple is an ordered array
Stores mixed data types
Allow duplicates
Created using round brackets () but accessed via square brackets
[]
Immutable - values cannot be modifed after creation
27/09/2023, 12:14 Lecture02 slides
3rd element is 5
something went wrong!
27/09/2023, 12:14 Lecture02 slides
Range
A range contains integer sequences
Created using function range(start, stop, step)
start defaults to 0
step defaults to 1
includes start but excludes stop
Immutable - values cannot be modifed after creation
Ideal for iteration ( for loops)
27/09/2023, 12:14 Lecture02 slides
#Sequence of integers 1 to 10
r2 = range(1,11)
#Convert to list
a = list(r1)
#Sequence of integers 10 to 1
r3 = range(10,0,-1)
10 9 8 7 6 5 4 3 2 1
27/09/2023, 12:14 Lecture02 slides
In [8]: A = list(range(10))
In [9]: A = list(range(10))
Sets
A set is unordered and unindexed
Prohibits duplicates
Can be created using curly brackets {} or set()
An immutable version is the frozenset
27/09/2023, 12:14 Lecture02 slides
#create set
myset = {"alpha", "beta", "gamma"}
print(myset)
#check membership
print("alpha" in myset)
Dictionary
A dict is mutable and indexed
It is ordered (since python v3.6) - keys are stored in insertion order.
Created using curly brackets {} but their elements are not single
values
Each element is key/value pair
Keys must be unique and are typically strings (must be immutable)
They are used to 'look up' their associated values
Values can be any data type (including a structured one)
27/09/2023, 12:14 Lecture02 slides
Coding in Python
A code module can contain statements, functions and classes
where do functions end and statements begin?
Many languages use parenthesis
statements inside brackets are treated as a block of code
Python uses indentation
normally 1 tab stop = 4 spaces
Indentation defines flow control
Changing indentation changes what your code will do!
27/09/2023, 12:14 Lecture02 slides
Functions
Python comes with built-in functions and a range of packages
Functions within packages must be imported before use
Can import entire libraries or single functions
Packages can be structured into sub-packages and modules
How you import something will determine how it can be used (called)
Can assign an alias and reference by a more convenient name
27/09/2023, 12:14 Lecture02 slides
print(exp(1))
2.718281828459045
[0. 0. 0.]
27/09/2023, 12:14 Lecture02 slides
help(powerof)
powerof(a, n)
this is a multiline docstring
it provides help for the function, or does it?
Out[18]:
8
27/09/2023, 12:14 Lecture02 slides
Input parameters
Out[19]:
16
27/09/2023, 12:14 Lecture02 slides
describeme('Alan', position='lecturer')
Out[20]:
'My name is Alan; I am a finance lecturer'
27/09/2023, 12:14 Lecture02 slides
t is a <class 'tuple'>
a is a <class 'str'>
b is a <class 'int'>
27/09/2023, 12:14 Lecture02 slides
Lambda Expressions
B = [sqme(x) for x in A]
print("B =",B)
output = 25
Appendix
Check list
Can you:
[ ] describe the salient features of a list, tuple, range, set,
dict ?
[ ] create and access the elements of these structures?
[ ] import and call a function from a built-in libary?
[ ] create and call a custom function with:
[ ] optional input parameters?
[ ] multiple input parameters?
[ ] multiple output parameters?
27/09/2023, 12:14 Lecture02 slides
Resources
https://docs.python.org/3/tutorial/datastructures.html
https://docs.python.org/3/reference/import.html
https://www.w3schools.com/python/python_lists.asp
https://www.w3schools.com/python/python_functions.asp
27/09/2023, 12:14 Lecture02 slides