CISC101 Reminders Assignment 2 due today.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Container Types in Python
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
String and Lists Dr. José M. Reyes Álamo.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Containers and Lists CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
4. sequence data type Rocky K. C. Chang 16 September 2018
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Python Primer 1: Types and Operators
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
Bryan Burlingame Halloween 2018
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Topics Sequences Introduction to Lists List Slicing
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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