28
Lecture 3: Collection Types and Functions Craig Zilles (Computer Science) Al Harris (Computer Science) September 13, 2019 https://go.illinois.edu/cs105fa19 CS 105

L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Lecture 3: Collection Types and Functions

Craig Zilles (Computer Science)Al Harris (Computer Science)

September 13, 2019

https://go.illinois.edu/cs105fa19

CS 105

Page 2: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Are you sitting next to someone to talk to for the clicker

questions?

Today I'm using: pythontutor.com

2

Page 3: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Big Picture (Muddiest Points)• Do we have to memorize all the functions of the lists,

dictionaries, removing an element, adding an element, etc. for exams and such?

• Is there a tip or way to remember what to use at specific times?

• I do not understand what is happening in each line of code and why, it is so confusing!

3

Page 4: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Collections• I mainly had trouble differentiating between the

sequence types and what each one does.

• The most confusing part of the material for me was keeping straight the differences between lists, sets, and dictionaries. It would be helpful to have a more concise review of the different properties and applications of each one.

• What is the purpose of a dictionary? Is it permanently stored until instructed otherwise?

4

Page 5: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Today1. Strings

• Concatenation2. Sequence Types (strings, lists, tuples)

• Indexing and Functions3. Immutable vs. Mutable4. Sets and Dictionaries5. Functions

• Parameters• Return Values

6. String Formating

5

Page 6: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Strings• Last time, we showed Python's string representation:

• The len() function is just reading the string's lengthmy_str = "CS 105"my_str_length = len(my_str)

7

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 7: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

What is string concatenation and how is that different than string literals?

What is the value of the above expression?

A) "Champaign-Urbana"B) "Champaign""Urbana"C) Champaign UrbanaD) "ChampaignUrbana"E) "Champaign Urbana"

8

"Champaign" + "Urbana"

Page 8: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

String Concatenation• string concatenation, why like "New" + "York" results "NewYork", I

thought it should be "New York"

• What's going on: "New" + "York"

9

Page 9: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

strings are a kind of Sequence Type

• Sequences are ordered collections • Strings are ordered collections of characters

• Sequences Types:• Have len() operator• Can be concatenated with +• Multiple concatenation with *

• "Craig" * 3 is "CraigCraigCraig"• Can be indexed with []

• Why used? To hold text information.10

Page 10: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Indexing• Sequences are 0 indexed:

my_str = "CS 105"my_char = my_str[1]

What is the value of my_char?a) 'C' b) 'S' c) ' ' d) '1'

11

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 11: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Lists are also Sequence Types

• Lists are sequences of any kind of objects ("elements")my_list = ["a string", 100, 3.14159]

len(my_list)my_list[2]my_list + [72, "Fred"]

12

Sequence Type

string list

Page 12: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Why are commas allowed in lists/adding to a list but not in regular statements or in

int/floats?

What are lists used for?

13

Page 13: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

One big difference: strings vs. lists• Strings are immutable

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

What is the value of str2 after the above code runs?

A) "hi" B) "hi!" C) "!" D) an error occured

14

A thing that i felt was most confusing in this current week's reading assignment was what variables you can change and which you can't. and whether it is a lot of things to change or just one single character.

Page 14: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Lists are mutablelist1 = ['a', 'b', 'c', 'd']list2 = list1list1.append('e')

What is the value of len(list2) after the above code runs?

A) 1 B) 3 C) 4 D) 5 E) an error occurred

Why are lists mutable?

15

Page 15: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Lists provide 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"

16

Page 16: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Announcements• Exam 0 ongoing

• Take the practice exam!

• Piazza etiquette• Use office hours if you are really confused

• Keep track of how much time you spend on CS 105 in the next week, please!

17

Page 17: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Compositionlist1 = [1, 2]list1.append([3, 4])

What does list1 contain after the code above executes?A) [1, 2]B) [1, 2, 3]C) [1, 2, 3, 4]D) [1, 2, [3, 4]]E) [1, 2, [3, [4]]]

18

Page 18: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Changing strings

str1 = "urbana"str1[5] = "e"

• What is the value of str1 after the code executes?A) "urbaea"B) "urbane"C) "urbanae"D) "ueeeee"E) an error occurred

19

Page 19: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

What if you want an immutable list?• It's called a tuple

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

• Also tuples just seem like lists with extra restrictions.

• I am confused of what a tuple is and what the purpose of using it is.

• I was very confused with namedtuple(). I did not understand where to use it, how to use it, or why it is even used.

20

Page 20: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Sets• How does a set differ from a list?• Set basics. "a set is an unordered collection of unique

elements", this is a definition from zyBooks- what does it mean by elements? Are those like variables? Values? I'm having a really hard time keeping all of the definitions for the terms straight.

• When to use: • You have a collection of similar things• You want to know if something is in the set or not• You want to do set operations (e.g., intersection)

21

Page 21: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Dictionaries• A mapping type: given a piece of info, find related info

• Key-Value pairs• Keys must be unique, values can be repeated

• What is it used for?

• UIN -> student record• Cell phone number -> cell tower (service provider)

22

Page 22: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

What type of data collection is this?

{"Craig", "Anant", "Sofia", "Chinny"}

A) DictionaryB) ListC) SetD) StringE) Tuple

23

Page 23: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

User-defined Functionsusing recipes in recipes

24

Page 24: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

User-defined Functions• Sequences of operations for use elsewhere in program• Function definition says what to do

def get_input_and_print():name = input("Your name?\n")print("Hello " + name + "!")

• Function calls/invocations actually run the function

get_input_and_print()

25

Page 25: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

String Formatting• I don't fully understand how to use string formatting and

why it is used?• The uses of conversion specifiers. Why and when do we

need to use those specifiers? The formatting is also confusing.

• Let's first tackle the why of string formatting.

• Next week's lab is about MadLibs.

26

Page 26: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

27

Page 27: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

• Things like this are ugly and tedious:message = "Welcome " + first + " " + last

message += " to CS 105!"

• We'd rather make string with holes, and then say how to fill in the holes

base = "Welcome {} {} to CS 105!"

message = base.format(first, last)

28

Page 28: L3 Types and Functions - University Of Illinois · 1.Strings •Concatenation 2.Sequence Types (strings, lists, tuples) •Indexing and Functions 3.Immutable vs. Mutable 4.Sets and

Python Format Strings• a_string.format(parameters)

• "a {} way".format("better")

• "can {1} the {0}".format("order", "control")

• "precision {0:.2f}".format(math.pi)

29