CMPUT 175 Lecture #1
CMPUT 175 Lecture #1
Introduction to Foundations
of Computing
Crash course on Python
Quick revision of CMPUT 174 material
Byte code or
machine code
January 10, 2024 © Osmar R. Zaïane : University of Alberta 3
Programming
A program is a sequence of instructions that specifies
how to perform a computation. Instructions are in a
programming language or machine code.
Access data (Input); Process data; Provide results
(Output)
Process data: basic operations; conditional operations,
repeated operations
Kind of Input
variable
Objects
Class
One
Methods of Invoking a method for Object
a class an Object (instance) instance
(behaviour) from the
Class
A value is
assigned to a What happens to the first
variable value "CMPUT 174"? Garbage
collector
January 10, 2024 © Osmar R. Zaïane : University of Alberta 11
Aliasing
x=y does not make a copy of y
x=y makes x reference the same object y references
a a
>>>a=[1,2,3] >>>a=[1,2,3]
>>>b=[1,2,3] [1,2,3]
>>>b=a
>>>a is b [1,2,3]
b >>>a is b b
False [1,2,3] True
Reference
lost after
assignment
a
Beware >>> a=1; b=a
1
>>> a=[1,2,3]; b=a >>> print(b)
>>> a.append(4); 1 b
>>> print(b) >>> a=a+1; print(b) 2
[1,2,3,4] 1 New object
created
January 10, 2024 © Osmar R. Zaïane : University of Alberta 12
Aliasing can cause problems
Use aliasing
firstVar only as a second
firstVar= “CMPUT” name for a
secondVar=firstVar “CMPUT” mutable object.
secondVar
“CMPUT”
secondVar
15
January 10, 2024 © Osmar R. Zaïane : University of Alberta
Types and Operators
String is a sequence of characters [immutable]
Values of strings are written using quotations
"CMPUT"
Characters are indexed starting from 0.
myVar="CMPUT"; myVar[2] returns 'P'
Operator + performs concatenation
"ABC"+"CDE" results into "ABCCDE" >>> myVar="CMPUT"
>>> myVar.lower()
Sequence operations 'cmput'
count(item) center(w,char) >>> myVar.find('P')
2
find(item) ljust(w) >>> myVar.split('P')
split(char) rjust(w) ['CM', 'UT']
lower() strip() >>> " CMPUT ".strip()
'CMPUT'
upper() replace(old,new,max)
16
January 10, 2024 © Osmar R. Zaïane : University of Alberta
Types and Operators
List is a sequence of values of any type [mutable]
Elements in a list are numbered starting from 0
Accessing an element of a list via its index k[index]
Operators + and * concatenate and repeat sequences
respectively
[1,2,3] + [4,5,6] results into [1,2,3,4,5,6]
[1,2,3] * 3 results into [1,2,3,1,2,3,1,2,3]
Operator : slices in a list
k=[1,2,3,4,5,6]; k[2:4] results into [3,4]
k=[1,2,3,4,5,6]; k[2:] results into [3,4,5,6]
k=[1,2,3,4,5,6]; k[:4] results into [1,2,3,4]
Membership operator in asks whether an item is in a list
3 in [1,2,3,4,5,6] returns True
Length of a list with operator len
len([1,2,3,4,5,6]) returns 6 17
January 10, 2024 © Osmar R. Zaïane : University of Alberta
Methods on Lists
append(item) adds an item at end of list
L.remove(4)
insert(i,item) inserts item at ith position of list print(L)
pop() removes and returns last item in list [1,3]
pop(i) removes and returns ith element in list L.reverse()
print(L)
del(i) removes i element in list
th
[3,1]
remove(item) removes 1st occurrence of item
L=[1,2,3,4] L.insert(3,65) L.pop()
sort() modifies listL.append(175)
to be sorted [1,2,3,65,4,175] 175
reverse() modifies[1,2,3,4,175]
list to be in reverse order
del L[2]
count(item) returns the number ofprint(L)
L.pop(1) occurrences ofprint(L)
item in list
2 [1,3,65,4]
index(item) returns the index of 1st occurrence of[1,3,4]item
January 10, 2024 © Osmar R. Zaïane : University of Alberta 18
Lists and Strings
list >>> list("CMPUT")
['C', 'M', 'P', 'U', 'T']
0 1 2 3
Array Victoria Edmonton Quebec Toronto
D[1] Index
BC AB QC ON
Dictionary Victoria Edmonton Quebec Toronto
D["AB"]
number=175; variable=175.2792
print(f"{number:10d}") >>> user = 'username'
'username@host'
print(f"{variable:+.3f}")
January 10, 2024 © Osmar R. Zaïane : University of Alberta 28
Control structures:
Conditionals
if condition: if condition:
statements statements
elif condition:
statements
if condition: elif condition:
statements statements …
else: else:
statements statements
continue
break
January 10, 2024 © Osmar R. Zaïane : University of Alberta 30
with open(filename, "r") as f:
Files f.read()
File closed automatically at the end of “with”
w=
Read only "r" r=
Read and write "r+"
Write only "w" a=
Append to the end of file "a"
Append a "b" for binary file
January 10, 2024 © Osmar R. Zaïane : University of Alberta 31
Functions and Procedures
def name(arg1,arg2,…):
statements