Collection Types (& Some Excel) Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 February 10, 2020
To Today 1. Excel 2. Strings • Concatenation 3. Sequence Types (strings, lists, tuples) • Indexing and Functions 4. Immutable vs. Mutable 5. Sets and Dictionaries 6. String Formating 2
Ho How do do Pytho hon n and nd Ex Excel relate? • They are both programming environments. • There are many similar concepts, for example • Every cell in Excel is a variable with a name (e.g., A1) • Cells have associated format 3
If If Exce cel l ce cells lls are lik like varia iable les, do o th they have ty types? A) Yes B) No 4
Ho How do do Pytho hon n and nd Ex Excel relate? • Excel has 4 types: number, string, Boolean, error • Excel tries to automatically infer type • Some times is guesses wrong • Can force things to be string with leading quote: ' • Excel automatically converts type • You can add strings and and concatenate to numbers 5
Ex Excel expr xpressi ssions ns si simi milar to Py Python • Expressions are made up of constants (literals), references to cells (variables), operators, other expressions, and functions • Many math operators the same (+, -, *, /) no (//, %) • Concatenation is the & operator 6
Ex Excel: Re Relative and Absolute Re References • Every cell in Excel has a name: e.g., C7 (column C, row 7) • When written in an Excel expression, this is a relative reference • If moved/copied to another cell, it will change proportionally • If you move = 2 * C7 down two rows, it will become = 2 * C9 • You can make absolute references by adding $ before row and/or column • $C$7 moved anywhere stays $C$7 • $C7 moved two down and two to the right becomes $C9 • C$7 moved two down and two to the right becomes E$7 7
Wh Which of the following will work in pl place of G1 G1 A) $G$1 B) $G$1 and $G1 C) $G$1 and G$1 D) $G$1 and G$1 and $G1 E) G$1 and $G1 8
To Today 1. Excel 2. Strings • Concatenation 3. Sequence Types (strings, lists, tuples) • Indexing and Functions 4. Immutable vs. Mutable 5. Sets and Dictionaries 6. String Formating 9
Bi Big Pictu cture (Muddiest t Poi oints ts) • 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! 10
Col Collecti ction ons (Muddi Muddiest Points) • "In general, what are the differences between the sets, lists, and tuples? How would I use each one in different conditions?" • "All the different functions you can use to alter containers. There are a lot of them and sometimes i mix them up or forget them. " • "How can you remember when to use () vs [] vs {}?" 11
Str Strings • Last time, we showed Python's string representation: Number of Characters Type characters (Stored using Unicode encoding) ‘CS 105’ String 6 001000011 001010011 000100000 000110001 000110000 000110101 C S 1 0 5 • The len() function is just reading the string's length my_str = "CS 105" my_str_length = len(my_str) 12
Wh What is string concatenation and how is th that t different t th than str tring literals? "Champaign" + "Urbana" What is the value of the above expression? A) "Champaign-Urbana" B) "Champaign""Urbana" C) Champaign Urbana D) "ChampaignUrbana" E) "Champaign Urbana" 13
Str String Con Concatenati tion on • string concatenation, why like "New" + "York" results "NewYork", I thought it should be "New York" • What's going on: "New" + "York" 14
string s a s are a e a ki kind o nd of f Seq Sequen ence T ce 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. 15
Indexin In ing • Sequences are 0 indexed: Number of Characters Type characters (Stored using Unicode encoding) ‘CS 105’ String 6 001000011 001010011 000100000 000110001 000110000 000110101 C S 1 0 5 my_str = "CS 105" my_char = my_str[1] What is the value of my_char ? a) 'C' b) 'S' c) ' ' d) '1' 16
Lis Lists ts are als lso o Se Sequence ce Types Sequence Type string list • 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"] 17
Why are commas allowed in lists/adding to a list but not in regular statements or in int/floats? What are lists used for? 18
An Annou ounce cements ts • Exam 0 statistics • Mean score: 88% Median: 91% • (discuss class structure) • Extra credit from going to office hours still available. • Hoping to announce how to get tutoring really soon. • Keep track of how much time you spend on CS 105 in the next week, please! 19
On One big difference: strings vs. lists • Strings are immutable 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 str1 = "hi" just one single character. str2 = str1 str1 += "!" What is the value of str2 after the above code runs? A) "hi" B) "hi!" C) "!" D) an error occured 20
Lis Lists ts are mutable le list1 = ['a', 'b', 'c', 'd'] list2 = list1 list1.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? 21
Lis Lists ts provid ide mod odif ific ication tion function ctions • 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" 22
Com Compos ositi tion on list1 = [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]]] 23
Ch Changing str trings 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 24
Wh 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. 25
Se Sets ts • 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) 26
Di Dictionaries es • 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) 27
Wh What type of data collection is this? {"Craig", "Anant", "Sofia", "Chinny"} A) Dictionary B) List C) Set D) String E) Tuple 28
Str String For ormatti ting • 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. 29
• 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) 30
Py Python Format at Strings • a_string .format( parameters ) • "a {} way".format("better") • "can {1} the {0}".format("order", "control") • "precision {0:.2f}".format(math.pi) 31
Wh What is the value of x af after this code x = "{1} {0} {1}".format("S", "O", "Y") A) "S O Y" B) "Y O Y" C) "S O S" D) "O S O" E) None of the above 32
Ne Next Read ading: User-de defi fine ned d Funct Functions ns using recipes in recipes 33
Recommend
More recommend