CS 105: COLLECTION TYPES Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 21, 2020
Video Series Three Topics Strings and Concatenation, String Formatting Sequence Types (strings, lists, tuples), Immutable vs Mutable Dictionaries
Strings (and Concatenation)
Remember Python's string representation len() function let's us get a string's length my_str = "CS 105" my_str_len = len(my_str) …What is the value in my_str_len? Did you guess 6?
Reminder – string concatenation If we have two inputs… num1 = input() #Let's say 4 num2 = input() #Let's say 2 What is num1 + num2? …42, of course!
Perils of string concatenation We would like "New" + "York" to be "New York" Instead, it is "NewYork" Concatenation directly puts two sequences together – and there is no white space in "New" or "York"
strings are a Sequence Type Sequences are ordered collections of things Strings are specifically ordered collections of characters 'C' 'S' ' ' '1' '0' '5' All Sequences Can use len() operator Can be concatenated with + Can have multiple concatenation with * "Max" * 5 = "MaxMaxMaxMaxMax" Can be index with [] string variables are used to hold text information
Strings can be indexed Indexing starts at 0 my_str = "CS 105" print(my_str[3]) 0 1 2 3 It prints 1
Strings can also be formatted Why format strings? Image trying to make a multi-line message: message = "Welcome " + first_name + " " + last_name message += " to your first day of work at " + company + "." Formatting gives us 'holes' we can fill in with data! (and reuse) format_string = "Welcome {} {} to your first day of work at {}." message = format_string.format(first_name, last_name, company)
Format Strings a_string.format(parameters) "We can {1} the {0}".format("order", "control") "We can {0} info in our format. {0} as much as we want.".format("repeat") "Format our money ${0:.2f}".format(25.223112) More detail (and nicer) than book: https://pyformat.info/
Video Question – What use of format produces the following string using the variable price and num? "You bought 3 cheesecakes for $25.25." price = 25.2432123 num = 3
Sequence Types in General And Immutable vs Mutable
Like strings, Lists are Sequence Types Lists are different from strings in that they can contain ANY kind of objects as elements my_list = ["a string", 100, 3.1415] Sequence operations still apply! len(my_list) my_list[2] my_list + [72, "Bob"] #Even concatenation!
Like strings, List indices start at 0 index 0 index 1 index 2 index 3 [10,20] “Strawberry” “Blueberry” 53
One of the more confusing differences Strings are immutable: str1 str2 "hi" "hi" str1 = "hi" str2 = str1 "hi!" str1+="!" https://www.screengeek.net/2019/12/19/avengers-endgame-thanos- flaw/
Lists are mutable list1 = ['a', 'b', 'c', 'd'] list2 = list1 list1.append('e') What is the value of list2 after these lines?
Lists provide a number of modification functions list1.append('f') list1.insert(2, 'z') list1.remove('c') list1.pop(0) list1.clear() Change the value of existing elements list1[1] = "an existing value changed"
What if you want an immutable list? 18 It's called a tuple tuple1 = ('a', 'b', 'c', 'd')
Video Question – How would you remove "Doughnut" from this list using pop? dessert_list = Cookie Cheesecake Doughnut Icecream Cake Cotton Candy
Dictionaries
What is a dictionary? https://www.merriam-webster.com/words-at-play/an-explanation-of- why-we-sometimes-truncate-definitions
Lists vs Dictionaries A collection of values with their own labels and no A collection of real order! values in order
Dictionaries – key value pairs sample_dict = {1: "Value one", "two": 2} To access a value… sample_dict[one] To change a value sample_dict["two"] = "New value" To add a new key- value pair… sample_dict[3] = "Like this!"
Dictionaries can be changed, but they don't use the same methods… We don't append, we just use [] We don't pop or remove. In order to get rid of the key-value pair with the key "gold" below, what do we do? sample_dict = {"gold": 25, "silver":35, "jade":10} del sample_dict["gold"]
Video Question – Why might you use a dictionary?
Recommend
More recommend