Class Notes (1,100,000)
CA (620,000)
UTSC (30,000)
All (10)
Lecture 5

# CSCA08 Lecture 5: CSC108 - Week 5 Notes

Department
Computer Science
Course Code
CSCA08H3
Professor
All
Lecture
5

This preview shows pages 1-2. to view the full 6 pages of the document.
Alexander Magony
CSC108H1F 2014
Week 5
-Comments can be left with a # symbol. Python ignores the rest of this line, allowing us to
leave comments and explanations of our code without it being executed or considered by
Python. That’s pretty much it, he talks about a lot about a loop example for some reason but it
all seems pretty intuitive to me. Check out the video if you’d like. https://
teach.cdf.toronto.edu/StG108/content/challenges/74/1
Comments are intended for programmers so they shouldn’t explain how the code does
something, just the effect, relationships of variables, purpose, etc.
Useful for explaining complicated bits of code.
Type list
-Type list is a type of value that can contain multiple values in square brackets.
eg. grades = [ 80, 90, 70] refers to a value of type list.
Indexing and slicing can be used for lists, like with strings.
in statements can also be used with lists.
-eg. 80 in grades returns True.
len(list) returns the number of items in list.
max(list) returns the largest item in list (in the case of grades it’s 90).
min(list) returns the smallest item in list (in the case of grades it’s 90).
sum(list) returns the sum of items in lists (as long as all items are numeric).
When a list contains strings: len(list) still returns the number of items, max(list) and
min(list) both return the greatest and least values respectively (remember, a string is
greater if the ﬁrst letter is later in the alphabet. So z > a), and sum(list) returns a TypeError.
Lists can contain both numbers and strings.
for loops work for lists by considering one item at a time. For
our grades example, the following code returns this:
list methods
-Remember methods are functions that belong to objects.
-List methods that modify the list:
list.append(object) appends a function to the end of the list. If colour = ‘brown’ and
colours = [‘blue’, ‘yellow’], colours.append(colour) will cause colours to now refer to [‘blue’,
‘yellow’, ‘brown’]. Notice that the original list has changed and now includes the
1

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Alexander Magony
CSC108H1F 2014
appended function. This is completely different to strings where you must store modiﬁed
strings in new variables.
list.extend(list) append the items in the list parameter to the list.
- Eg. colours.extend([‘hot pink’, ‘neon green’]) causes colours to now refer to [‘blue’,
‘yellow’, ‘brown’, ‘hot pink’, ‘neon green’].
-REMEMBER extend requires a list, not multiple objects.
The important difference is that extend essentially combines the lists while append adds an
object INTO a list. So if you appended a list into a list, you would have a list added as the
last item in a list (a nested list, which we’ll get to later) rather than one list created from the
items of two lists. Be careful about when to use either method.
Also remember that you don’t have to update the variable referring to the list. Modifying it
will change it automatically.
list.pop() remove and return the item at the end of the list (optional argument can include
index to remove from speciﬁc index). Since it modiﬁes AND returns the list, it is said to
have side effects.
-Continuing with our colours example: colours.pop() returns ‘neon green’, the removed
item. colours now refers to [‘blue’, ‘yellow’, ‘brown’, ‘hot pink’]
-If we now want to remove the brown speciﬁcally (which is at index 2), colours.pop(2) will
return ‘brown’ and colours will not refer to [‘blue’, ‘yellow’, ‘hot pink’]
list.remove(object) removes the ﬁrst occurrence of the object. There is a ValueError if it is
not there.
-eg. colours.remove(‘black’) returns a ValueError since it does not exist in the list.
-colours.remove(‘yellow’) will cause colours to return [‘blue’, ‘hot pink’].
list.sort() will sort the list from smallest to largest (with alphabetic items, early letters are
smaller. So ‘apple’ < ‘boo’). list.reverse() reverses the list.
list.insert(int, object) inserts the object at a given index, moving items to make room.
-So with colours = [‘blue’, ‘hot pink’], if we execute colours.insert(-1, ‘brown’), colours now
refers to [‘blue’, ‘brown’, ‘hot pink’]. Notice that ‘brown’ does not exist in [-1] right now, it
was just inserted at that point and ‘hot pink’ was moved to after ‘brown’, making it the
new [-1].
-List methods that do not modify the list:
list.count(object) return the number of times object occurs in list.
list.index(object) returns the index of the ﬁrst occurrence of the object. There is a
ValueError if it is not there.
-Since index returns an error if the object does not exist, it may be better to put it under an if
statement in a function to prevent errors
If we want to remove ‘hot pink’, we can
write the following code (to the right).
If ‘hot pink’ does not exist, the if statement
does not occur, nothing returns, and there
is no error statement.
Alternatively, the body can simply state
colours.remove(‘hot pink’). The if statement still prevents errors since remove can cause
errors if the item is not in the list.
2