CS 105: COLLECTION TYPES Max Fowler (Computer Science) - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

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),


slide-1
SLIDE 1

CS 105: COLLECTION TYPES

Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/

June 21, 2020

slide-2
SLIDE 2

Video Series Three Topics

 Strings and Concatenation, String Formatting  Sequence Types (strings, lists, tuples), Immutable vs

Mutable

 Dictionaries

slide-3
SLIDE 3

Strings (and Concatenation)

slide-4
SLIDE 4

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?

slide-5
SLIDE 5

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!

slide-6
SLIDE 6

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"

slide-7
SLIDE 7

strings are a Sequence Type

 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'

slide-8
SLIDE 8

Strings can be indexed

 Indexing starts at 0  my_str = "CS 105"  print(my_str[3])  It prints 1

1 2 3

slide-9
SLIDE 9

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)

slide-10
SLIDE 10

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/

slide-11
SLIDE 11

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

slide-12
SLIDE 12

And Immutable vs Mutable

Sequence Types in General

slide-13
SLIDE 13

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!

slide-14
SLIDE 14

Like strings, List indices start at 0

index 0 index 1 index 2 index 3 “Blueberry” 53 [10,20] “Strawberry”

slide-15
SLIDE 15

One of the more confusing differences

Strings are immutable:

str1 = "hi" str2 = str1 str1+="!"

"hi"

str1 str2

"hi" "hi!"

https://www.screengeek.net/2019/12/19/avengers-endgame-thanos- flaw/

slide-16
SLIDE 16

Lists are mutable

 list1 = ['a', 'b', 'c', 'd']  list2 = list1  list1.append('e')  What is the value of list2 after these lines?

slide-17
SLIDE 17

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"

slide-18
SLIDE 18

What if you want an immutable list?

 It's called a tuple

tuple1 = ('a', 'b', 'c', 'd')

18

slide-19
SLIDE 19

Video Question – How would you remove "Doughnut" from this list using pop?

Cookie Cheesecake Doughnut Icecream Cake Cotton Candy

dessert_list =

slide-20
SLIDE 20

Dictionaries

slide-21
SLIDE 21

What is a dictionary?

 https://www.merriam-webster.com/words-at-play/an-explanation-of-

why-we-sometimes-truncate-definitions

slide-22
SLIDE 22

Lists vs Dictionaries

A collection of values in order A collection of values with their own labels and no real order!

slide-23
SLIDE 23

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!"

slide-24
SLIDE 24

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"]

slide-25
SLIDE 25

Video Question – Why might you use a dictionary?