Collection Types (& Some Excel) Craig Zilles (Computer Science) - - PowerPoint PPT Presentation

collection types some excel
SMART_READER_LITE
LIVE PREVIEW

Collection Types (& Some Excel) Craig Zilles (Computer Science) - - PowerPoint PPT Presentation

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.


slide-1
SLIDE 1

Collection Types (& Some Excel)

Craig Zilles (Computer Science) February 10, 2020 https://go.illinois.edu/cs105sp20

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

If If Exce cel l ce cells lls are lik like varia iable les, do

  • th

they have ty types?

A) Yes B) No

4

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Bi Big Pictu cture (Muddiest t Poi

  • ints

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

slide-11
SLIDE 11

Col Collecti ction

  • ns (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

slide-12
SLIDE 12

Str Strings

  • Last time, we showed Python's string representation:
  • The len() function is just reading the string's length

my_str = "CS 105" my_str_length = len(my_str)

12

String 6 001000011 001010011

Type Number of characters Characters (Stored using Unicode encoding)

000100000 000110001 000110000 000110101 ‘CS 105’ C S 1 5

slide-13
SLIDE 13

Wh What is string concatenation and how is th that t different t th than str tring literals?

What is the value of the above expression? A) "Champaign-Urbana" B) "Champaign""Urbana" C) Champaign Urbana D) "ChampaignUrbana" E) "Champaign Urbana"

13

"Champaign" + "Urbana"

slide-14
SLIDE 14

Str String Con Concatenati tion

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

thought it should be "New York"

  • What's going on:

"New" + "York"

14

slide-15
SLIDE 15

strings 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

slide-16
SLIDE 16

In Indexin ing

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

16

String 6 001000011 001010011

Type Number of characters Characters (Stored using Unicode encoding)

000100000 000110001 000110000 000110101 ‘CS 105’ C S 1 5

slide-17
SLIDE 17

Lis Lists ts are als lso

  • Se

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

17

Sequence Type string list

slide-18
SLIDE 18

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

slide-19
SLIDE 19

An Annou

  • unce

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

slide-20
SLIDE 20

On One big difference: strings vs. lists

  • Strings are immutable

str1 = "hi" str2 = str1 str1 += "!" What is the value of str2 after the above code runs? A) "hi" B) "hi!" C) "!" D) an error occured

20

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.

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Lis Lists ts provid ide mod

  • dif

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

slide-23
SLIDE 23

Com Compos

  • siti

tion

  • n

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Wh What type of data collection is this?

{"Craig", "Anant", "Sofia", "Chinny"} A) Dictionary B) List C) Set D) String E) Tuple

28

slide-29
SLIDE 29

Str String For

  • rmatti

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

slide-30
SLIDE 30
  • 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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

Ne Next Read ading: User-de defi fine ned d Funct Functions ns

using recipes in recipes

33

slide-34
SLIDE 34

Us User-de define ned d Func unctions ns

  • 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()

34