Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Quiz 2 this week.

Similar presentations


Presentation on theme: "CISC101 Reminders Quiz 2 this week."— Presentation transcript:

1 CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/12/2018 CISC101 Reminders Quiz 2 this week. Next Assignment (Assn 2) now due Monday, the 19th. New Exercise 6 – uses the Turtle and encourages you to use functions. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Queen’s Data Day Please see more information about this activity linked to the main page of the course web site: Winter 2018 CISC101 - Prof. McLeod

3 Exercises You are now ready to work on Exercise 4.
Note – for turtle input use the turtle methods: and: See numinput used in the sample solutions for the Turtle exercises in Exercise 4. Winter 2018 CISC101 - Prof. McLeod

4 Today Start Collections Winter 2018 CISC101 - Prof. McLeod

5 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 2018 CISC101 - Prof. McLeod

6 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 2018 CISC101 - Prof. McLeod

7 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 Section 4.6 in the Python Library Reference. Winter 2018 CISC101 - Prof. McLeod

8 Examples of Collection Literals
CISC101 Examples of Collection Literals [2, 3, 4, 6.78, 'abcd', -10] (4, 5, 'hi', 6.6) {4, 5, 7, 9, 11} 'hello class!' {'first':'Alan', 'age':25, 'last':'McLeod'} Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

9 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 2018 CISC101 - Prof. McLeod

10 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 2018 CISC101 - Prof. McLeod

11 aDict = {'first' : 'Alan', 'last' : 'McLeod'}
Dictionaries “Dicts” are enclosed in { }. They consist of key : value associations. For example: aDict = {'first' : 'Alan', 'last' : 'McLeod'} We will look at these more closely later… Winter 2018 CISC101 - Prof. McLeod

12 Sets Are 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 2018 CISC101 - Prof. McLeod

13 Lists They can hold items of all the same type: [3, 2, -1, 10]
Or can hold a mixture of types: [4.2, ‘7abc’, 3, aVar] Yes, they can hold variables as well as literals! Winter 2018 CISC101 - Prof. McLeod

14 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 2018 CISC101 - Prof. McLeod

15 Empty Lists You can create an empty list as in: mtList = []
Probably, you will be using append() or the + operator with variables like these… You can create an empty tuple as in: mtTuple = () You can use + to add tuples, but not append(). Winter 2018 CISC101 - Prof. McLeod

16 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! 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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

17 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 2018 CISC101 - Prof. McLeod

18 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 2018 CISC101 - Prof. McLeod

19 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 2018 CISC101 - Prof. McLeod

20 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 2018 CISC101 - Prof. McLeod

21 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 2018 CISC101 - Prof. McLeod

22 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 2018 CISC101 - Prof. McLeod

23 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 2018 CISC101 - Prof. McLeod

24 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 2018 CISC101 - Prof. McLeod

25 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 2018 CISC101 - Prof. McLeod

26 for Loop Example >>> test [2, 4, 600]
>>> for aNum in test : print(aNum, end=', ') 2, 4, 600, Winter 2018 CISC101 - Prof. McLeod

27 Summary Operators used with lists: [ # : # ], +, *
Keywords used with lists: del, in, not in, for Next: BIFs used with lists and list methods. Winter 2018 CISC101 - Prof. McLeod

28 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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

29 The range() BIF It returns an iterable, not a list.
Is it possible to create a list using the range() BIF? Winter 2018 CISC101 - Prof. McLeod

30 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 2018 CISC101 - Prof. McLeod

31 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 2018 CISC101 - Prof. McLeod

32 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 2018 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Quiz 2 this week."

Similar presentations


Ads by Google