CS 1111 Introduction to Programming Spring 2019

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Course A201: Introduction to Programming 10/28/2010.
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?
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Lists in Python.
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.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
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.
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.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
Data types: Complex types (List)
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 1110 Introduction to Programming Spring 2017
CS 100: Roadmap to Computing
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Data types: Complex types (Dictionaries)
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Perl Variables: Array Web Programming.
Lists in Python.
Bryan Burlingame Halloween 2018
Guide to Programming with Python
6. Lists Let's Learn Python and Pygame
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Object Oriented Programming in java
CS 1111 Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
CS 1111 Introduction to Programming Fall 2018
Introduction to Strings
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CS1110 Today: collections.
CS 1111 Introduction to Programming Spring 2019
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Lists Copying Lists Processing Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Introduction to Strings
CS 100: Roadmap to Computing
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Introduction to Computer Science
Presentation transcript:

CS 1111 Introduction to Programming Spring 2019 Lists CS 1111 Introduction to Programming Spring 2019 [The Coder’s Apprentice, §12]

Overview: Lists List = ordered sequence of values Mutable data type Because of the ordering, an element in a list can be referred by its index. Indices start at zero

Ordered Collection Collection What it can hold Syntax to create Access element Mutable? string characters “…..” [index] Immutable range int range(start, stop, step) Note: start, stop, step must be int list anything, any type [ e1, e2, … ] Mutable

Creating Lists animals = ['cow', 'dog', 'horse'] # create a new list print(animals) animals1 = [] # create an empty list print(animals) animals1 = ['cow', 'horse'] animals2 = ['dog'] animals3 = animals1 + animals2 # concatenate lists print(animals3)

in list = [5, 7, 9, 11, 15] print(7 in list) print(3 in list) print(3 not in list) in is a keyword and can be used to check if the element is in the list or string before trying to get its index

Accessing Items in Lists animals = ['cow', 'dog', 'horse'] # create a new list print(animals[2]) # access a particular item animals[2] = 'duck' # update a particular item print(animals[0]) # indices start from zero print(animals[-1]) # negative numbers start from the end of the list print('The ' + animals[0] + ' and the ' + animals[2] + ' sleep in the barn.')

Length of Lists animals = ['dog', 'cat', 'bird'] counter = 0 while counter < len(animals): print(animals[counter]) counter = counter + 1 print(animals) len() returns the length of a list (i.e., the number of items in a list)

Adding Items to Lists animals = ['cow', 'dog', 'horse'] # create a new list animals.append('deer') # add item to a list print(animals[2]) # access a particular item animals[2] = 'duck' # update a particular item print(animals[0]) # indices start from zero print(animals[-1]) # negative numbers start from the end of the list print('The ' + animals[0] + ' and the ' + animals[2] + ' sleep in the barn.') animals.insert(2, 'pig') print(animals) append(element) adds an element to the end of a list, return None insert(index, element) adds an element to a particular position of a list, return None

Removing Items from Lists animals = ['cow', 'dog', 'horse', 'sheep', 'pig' ] print(animals) del animals[3] # remove by index print(animals) print(animals.pop()) # remove the last element, and return its value print(animals.pop(1)) # remove by index, and return its value print(animals) animals.remove('horse') # remove by item / element print(animals) del deletes an element at a particular position pop() removes the last element from the list and return its value pop(index) removes an element at a particular position and return its value; raise IndexError if an index is out of range remove(element) removes a particular element, return None; raise ValueError if an element does not exist

Sorting and Reversing animals = ['cow', 'dog', 'horse', 'sheep', 'pig'] animals.sort() print('sorted animals =', animals) # another way to print (notice a space after "=") print('sorted animals = ' + str(animals)) animals.reverse() print('reversed animals = ', animals) sort() rearranges the items of a list (in ascending order), return None reverse() reverses the order of the items in the list, return None

index(element) small = [1, 2, 3] print(small.index(2)) index(element) returns an index of an element, raise ValueError if an element is not found Note: You’ll need to check if the element is in the list before trying to get its index

list(collection) letters = 'ABCDEFG' print(list(letters)) list(collection) converts a given collection into a list, return a list

Slicing and Returning Part of a List with [ : ] list = [5, 7, 9, 11, 15] print(list) print(list[1:4]) print(list[1:]) print(list[:4]) print(list[:-1]) print(type(list[2:4]))

How are Lists Represented in Memory ? Primitive types are stored directly Complex types (such as lists) are stored indirectly Trace through code What happens when we assign a variable to a list? (in memory) Only the memory address is assigned; the list is not copied num = 5 grades = [97, 86, 91, num, 88] num = 33 big = [23, grades, num, 7] print(big) grades[1] = 87 grades.append(6) big[2] = grades

Tracing through Code with Lists Rule 1 Variables and items on the heap are stored in separate locations. Rule 2 A primitive type is stored directly with its variable. A complex type has its variable store a memory address. A memory address refers to a location on the heap where the actual data is stored. Rule 3 Every assignment begins by either creating a variable space (and heap location, if necessary), or emptying out the existing contents of a variable space (but not the heap!). Copying either a value or memory address from one box into the other. A variable or memory location must only store either numbers/booleans, or a memory address, never the name of a variable.

Tracing through Code with Lists num = 5 grades = [97, 86, 91, num, 88] num = 33 big = [23, grades, num, 7] print(big) grades[1] = 87 grades.append(6) big[2] = grades print(grades) Heap Output A100 [23, [97, 86, 91, 5, 88], 33, 7] 1 2 3 97 86 87 A100 of 1 91 [23, [97, 87, 91, 5, 88, 6], [97, 87, 91, 5, 88, 6], 7] A200 of 2 num 5 4 88 [97, 87, 91, 5, 88, 6] 5 6 Variables A200 num 5 33 1 2 3 23 grades A100 grades A100 num 33 A100 big 7 A200

Two Dimensional List (List of Lists) list1 = [5, 7, 9, 11, 15] list_of_lists = [['cow', 'horse'], [list], [4, 5, 6]] print (list_of_lists[0]) # access a particular list print (list_of_lists[0][1]) # access a particular item print(len(list_of_lists)) print(len(list_of_lists[1]))

Summary Must know (based on exam2 topic list, as of 03/04/2019) element in lst lst.append(value) lst.insert(index, value) lst.remove(value) lst.pop(index) lst.sort() lst.index(element) lst[start:end] list(collection)