CS 105: COLLECTION TYPES
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
June 21, 2020
CS 105: COLLECTION TYPES Max Fowler (Computer Science) - - PowerPoint PPT Presentation
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),
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
June 21, 2020
Strings and Concatenation, String Formatting Sequence Types (strings, lists, tuples), Immutable vs
Dictionaries
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?
If we have two inputs… num1 = input() #Let's say 4 num2 = input() #Let's say 2 What is num1 + num2? …42, of course!
We would like "New" + "York" to be "New York" Instead, it is "NewYork" Concatenation directly puts two sequences together –
Sequences are ordered collections of things
Strings are specifically ordered collections of characters
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
'C' 'S' ' ' '1' '0' '5'
Indexing starts at 0 my_str = "CS 105" print(my_str[3]) It prints 1
1 2 3
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)
a_string.format(parameters) "We can {1} the {0}".format("order", "control") "We can {0} info in our format. {0} as much as we
"Format our money ${0:.2f}".format(25.223112) More detail (and nicer) than book: https://pyformat.info/
"You bought 3 cheesecakes for $25.25."
Lists are different from strings in that they
Sequence operations still apply!
index 0 index 1 index 2 index 3 “Blueberry” 53 [10,20] “Strawberry”
Strings are immutable:
https://www.screengeek.net/2019/12/19/avengers-endgame-thanos- flaw/
list1 = ['a', 'b', 'c', 'd'] list2 = list1 list1.append('e') What is the value of list2 after these lines?
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"
It's called a tuple
18
Cookie Cheesecake Doughnut Icecream Cake Cotton Candy
https://www.merriam-webster.com/words-at-play/an-explanation-of-
A collection of values in order A collection of values with their own labels and no real order!
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!"
We don't append, we just use [] We don't pop or remove. In order to get rid of the key-value pair with
sample_dict = {"gold": 25, "silver":35, "jade":10}
del sample_dict["gold"]