Abstract Data Types Applied Arrays: Lists and Strings Chapter 12 - 13.
Lists and Strings
description
Transcript of Lists and Strings
![Page 1: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/1.jpg)
Lists and Strings
George Mason University
![Page 2: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/2.jpg)
Today’s topics
• Review of Chapter 5: Lists and Strings• Go over examples and questions• lists and strings in Python
![Page 3: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/3.jpg)
List (and strings) review
• How do we declare a list? An empty list?• What operations can we apply to lists?• How are lists represented in memory?– primitive types– complex types
• What happens when we assign a variable to a list? (in memory)
• What is a character?• How is a string similar to a list? Different?• What is the substring operation?
![Page 4: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/4.jpg)
Let’s go over the exercises
![Page 5: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/5.jpg)
Lists in python
• .append( ) is a way to add a single element to the end of a list
![Page 6: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/6.jpg)
length in python
• len( ) is a function to return the length of a list or string
![Page 7: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/7.jpg)
python memory visualizer
• let’s use the python memory visualizer to trace through an example:
http://pythontutor.com/visualize.html#code=x+%3D+1%0Asmall+%3D+%5Bx,+2,+3%5D%0Amedium+%3D+%5B43,+76,+180,+57%5D%0Alarge+%3D+%5B234%5D%0AbigList+%3D+%5Bmedium,+small,+large%5D%0Aprint+small%0Aprint+bigList%0Asmall%5B0%5D+%3D+-1%0Aprint+small%0Aprint+bigList%0Ax+%3D+7%0Aprint+small&mode=display&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=2&rawInputLstJSON=%5B%5D&curInstr=0
Note the difference between the storage of x, a primitive, and the lists.
![Page 8: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/8.jpg)
Debugging with id( )
• id( ) can be used to print out the memory address of a complex type
• str( ) is needed to add non-strings to strings
![Page 9: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/9.jpg)
Debugging with id( )
![Page 10: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/10.jpg)
index( )
• Note you’ll need to check if the element is in the list or string before trying to get its index
![Page 11: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/11.jpg)
in
• 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
![Page 12: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/12.jpg)
substrings and sub-lists with [:]
![Page 13: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/13.jpg)
newlines and tabs
![Page 14: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/14.jpg)
Other useful functions
• min(list)• max(list)• list.remove(1.5)• sortedList = sorted(list)
![Page 15: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/15.jpg)
Strings in Python
• ‘cat’ and “cat” are the same– can use single or double quote– default is single quote– useful for “’” versus ‘”’
![Page 16: Lists and Strings](https://reader036.fdocuments.us/reader036/viewer/2022062501/56816456550346895dd62321/html5/thumbnails/16.jpg)
Questions?