Download presentation
Presentation is loading. Please wait.
1
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/11/2019 CISC101 Reminders Assignment 2 due today. Next assignment due March 8. Next quiz the week after this. Exercise 4 is fair game now. Have a great Reading Week! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Start Collections:
Winter 2019 CISC101 4/11/2019 Today Start Collections: These collections are built into Python and one big reason why this language is so popular. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
3
Collections Allows you to access a whole bunch of “things”, one at a time or in groups through a single variable name. Collections can be huge – the limit is the amount of available RAM. A typical scenario: Read data from a file. Carry out whatever analysis you are interested in, accessing all the data in RAM. Save the results in another collection in RAM. Save the results collection to another file. Winter 2019 CISC101 - Prof. McLeod
4
Python’s Collections First, an overview of the popular collection types, then: Focus on what is used with lists (and to some extent with other collections): Operators Keywords BIFs Methods A list is the most versatile collection type. Winter 2019 CISC101 - Prof. McLeod
5
Built-in Collection Types
Python has: Lists Tuples Sets Dictionaries (Strings are really just a kind of Tuple…) See Chapter 5 in the Python Tutorial and in parts of “Built-in Types” in the Python Standard Library. Winter 2019 CISC101 - Prof. McLeod
6
Collection Conversion & Creation BIFs
You can use these BIFs to create empty collections, or in some cases, to convert between collections and iterables: List – list() Tuple – tuple() Set – set() Dictionary – dict() String – str() Or, you can create a collection using literals: Winter 2019 CISC101 - Prof. McLeod
7
Examples of Collection Literals
CISC101 Examples of Collection Literals list - [2, 3, 4, 6.78, 'abcd', -10] tuple - (4, 5, 'hi', 6.6) set - {4, 5, 7, 9, 11} dictionary - {'first':'Alan', 'age':25, 'last':'McLeod’} string - 'hello class!' Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
8
Lists vs. Tuples A list literal is a set of items enclosed in [ ]
A tuple literal is a set of items enclosed in ( ) Items are separated by commas. You can change the items within a list and its length at any time after you have created it. A list is mutable. Winter 2019 CISC101 - Prof. McLeod
9
Lists vs. Tuples, Cont. You cannot change the items in a tuple or change its length after you have created it - it is “immutable” (like “read-only”). (Numbers and strings are also immutable. You cannot mess with the individual digits of a number or the individual characters of a string after you have created them. You can only re-assign variables that are numeric or string types.) Winter 2019 CISC101 - Prof. McLeod
10
aDict = {'first' : 'Alan', 'last' : 'McLeod'}
Dictionaries “Dicts” are enclosed in { }. They consist of key : value associations, or “pairs”. For example: aDict = {'first' : 'Alan', 'last' : 'McLeod'} We will look at these more closely later… Winter 2019 CISC101 - Prof. McLeod
11
Sets Are relatively new to Python 3. Items enclosed in { }.
Each item must be unique. If you try to create a set with duplicate items, the duplicates will be discarded. We will look at these more closely later too… Winter 2019 CISC101 - Prof. McLeod
12
Lists They can hold items of all the same type: [3, 2, -1, 10]
Or can hold a mixture of types, including other collections: [4.2, ‘7abc’, 3, aVar] Yes, they can hold variables as well as literals! Winter 2019 CISC101 - Prof. McLeod
13
atuple = (4, 3.2, ‘abc’, 7, -3, ‘ding’)
Tuples Can be a mixture of types, just like lists: atuple = (4, 3.2, ‘abc’, 7, -3, ‘ding’) Since a tuple is immutable, you cannot do something like: atuple[1] = 7 Use code like (‘abc’,) to create a single element tuple. Why the comma? Winter 2019 CISC101 - Prof. McLeod
14
Empty Lists You can create an empty list as in: mtList = []
Probably, you will be using the append() method or the + operator with variables like these… You can create an empty tuple as in: mtTuple = () Tuples can be combined using +, but not append(). Winter 2019 CISC101 - Prof. McLeod
15
Operators Used with Lists
CISC101 Operators Used with Lists Slice operator [ : ] + can be used to concatenate lists. * is used to generate multiples of lists. The slice operator can be used on either side of an assignment operator when used with a list. For +, must have a list on both sides or a tuple on both sides, you cannot mix types. For *, must have an int after the *. Works with tuples or lists. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
16
Reminder: The Slice Operator
You can extract single elements or a set of elements from a collection using the “slice” operator: [ # ] or [ # : # ] Where # is an int number. Locations are numbered from zero. The slice operator without the “:” gets a single element. The other one can get a range of elements. Winter 2019 CISC101 - Prof. McLeod
17
The Slice Operator, Cont.
When using [ : ], you can supply one or two numbers. If you omit the first number (but still have the colon) the slice starts at the start of the collection. If you omit the second number (but still have the colon) the slice ends at the end of the collection. The second number is one past the element you want. Winter 2019 CISC101 - Prof. McLeod
18
Slice Operator, Cont. If the second number is too large, then the slice defaults to the end of the list. You can also number the elements backwards, where -1 is the last number in the list… Winter 2019 CISC101 - Prof. McLeod
19
Slice Operator on a List, Examples
>>> test = [2, 1, 3, -1, 4, 6] >>> test[3] -1 >>> test[-1] 6 >>> test[4 :] [4, 6] >>> test[ : 3] [2, 1, 3] >>> test[1 : 3] [1, 3] Winter 2019 CISC101 - Prof. McLeod
20
Slice Operator on a List, Examples, Cont.
>>> test[1 : 3] = [10, 30] >>> test [2, 10, 30, -1, 4, 6] >>> test[-1] = 600 [2, 10, 30, -1, 4, 600] >>> test[1 : 3] = [10, 20, 30, 40, 50] [2, 10, 20, 30, 40, 50, -1, 4, 600] >>> test[2] = [-10, -5, -3] [2, 10, [-10, -5, -3], 30, 40, 50, -1, 4, 600] Winter 2019 CISC101 - Prof. McLeod
21
Other Operators, Examples
>>> test [2, 10, 30, -1, 4, 600] >>> testTwo = [5, 10, 15] >>> test + testTwo [2, 10, 30, -1, 4, 600, 5, 10, 15] >>> testTwo * 3 [5, 10, 15, 5, 10, 15, 5, 10, 15] Winter 2019 CISC101 - Prof. McLeod
22
List Questions When re-assigning a range of a list using the slice operator, does the new list have to have the same size as the range specified? Can a list contain a list as a member? Can a list contain a tuple as a member? Winter 2019 CISC101 - Prof. McLeod
23
Keywords Used with Lists
del can be used to delete an element or a range of elements in a list. Use the slice operator to specify what you want deleted. in and not in can be used to search for an element in a list. They provide a True or False. for loop. Winter 2019 CISC101 - Prof. McLeod
24
Keyword Examples >>> test [2, 10, 30, -1, 4, 600]
>>> del test[3] [2, 10, 30, 4, 600] >>> del test[1 : 3] [2, 4, 600] >>> 4 in test True >>> 100 in test False >>> 100 not in test Winter 2019 CISC101 - Prof. McLeod
25
for Loop Example >>> test [2, 4, 600]
>>> for aNum in test : print(aNum, end=', ') 2, 4, 600, Winter 2019 CISC101 - Prof. McLeod
26
Summary Operators used with lists: [ # : # ], +, *
Keywords used with lists: del, in, not in, for Next: BIFs used with lists and list methods. Winter 2019 CISC101 - Prof. McLeod
27
Some Built-In Functions Applied to Lists
CISC101 Some Built-In Functions Applied to Lists Use the len() BIF to get the number of elements in any list. Use list() and tuple() to change the types. The range([start,] stop [,step]) BIF returns an “iterable”: Starting from start Stopping at stop - 1 Using step Often used with a for loop… Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod
28
The range() BIF It returns an iterable, not a list.
Is it possible to create a list using the range() BIF? Winter 2019 CISC101 - Prof. McLeod
29
sorted(), reversed() BIFs
The sorted() BIF returns a sorted version of the list supplied as an argument to the function, without changing the supplied list. (The sort() method of a list, sorts that list in situ, changing it.) reversed() is to be used only with a for loop. It reverses the current direction of iteration of an iterable. Winter 2019 CISC101 - Prof. McLeod
30
More Example Code >>> test = [10, 2, 4, 7, 1, 6] >>> sorted(test) [1, 2, 4, 6, 7, 10] >>> test [10, 2, 4, 7, 1, 6] >>> test.sort() >>> test = list(range(10, 101, 10)) [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Winter 2019 CISC101 - Prof. McLeod
31
More Example Code, Cont. >>> test [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] >>> for val in reversed(test) : print(val, end=', ') 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, Winter 2019 CISC101 - Prof. McLeod
32
enumerate() and zip() BIFs
enumerate(): An easy way of generating an index number for list items. zip(): A way to loop through any number of lists at the same time. Winter 2019 CISC101 - Prof. McLeod
33
Other BIFs to Use With Lists
min() max() sum() What do you think these do? Could you have written these functions yourself? See ListBIFDemos.py Winter 2019 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.