28
Introduction to Computation for the Humanities and Social Sciences CS 3 Chris Tanner

Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

Introduction to Computation for the Humanities and Social

Sciences

CS 3 Chris Tanner

Page 2: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

Lecture 12

Ready, Set, Go!

Page 3: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

Lecture 12

!3

My Project Demo

New Data Structure: Set()

Scope

Page 4: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!4

New Data Structure!• very similar to Lists

• A Set is an unordered collection of items

• Collections which don’t preserve the order in which elements are added are called unordered

• Doesn’t support indexinga = {1,2,3}a.add(4)if 2 in a: print(a)

Sets

Page 5: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!5

New Data Structure!• very similar to Lists, but

• is incapable of storing duplicate items (maintains just 1 copy)

students = set()

students.add(“jackie”)

students.add(“emily”)

students.add(“hank”)

students.add(“emily”)

print(students)

>>> students = {‘emily’, ’jackie’, ‘hank’}

Sets

Page 6: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!6

New Data Structure!• checking if an item is contained in the Set happens instantly

(doesn’t need to check each item one by one)

students = set()

students.add(“jackie”)

students.add(“emily”)

students.add(“hank”)

students.add(“emily”)

if cur_student in students:

print(student + “is present!”)

else:

print(student + “is NOT present!”)

Sets

Page 7: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!7

New Data Structure!

• The downside: Sets have no indices and no order!

• So, you can’t access specific items. If you need to maintain an order for your items, use a List.

students[0] CRASHES

students = set()

students.add(“jackie”)

students.add(“emily”)

students.add(“hank”)

students.add(“emily”)

Sets

Page 8: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!8

When to use a Set()?

• Use a Set() when you only care about storing a collection of unique values (i.e., don’t care about duplicates) and the order doesn’t matter to you.

• e.g., store license plate #s or list of senator names.

Sets

Page 9: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!9

What is Scope?

• Each variable has its own scope.

• Scope refers to the places in your code which has access to the variable.

• You’ve encountered scope before. Where?

Scope

Page 10: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!10

What is Scope?

• A variable’s scope starts when it’s first declared/initialized.

• A variable’s scope terminates once your code has less indentation than that of when it was declared.

Scope

Page 11: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!11

What is Scope?

Scope

• The concept we saw with the scope within functions continues on a smaller scale, too: if we define a variable within a for-loop or if-statement, it’s like its own private room, and things outside the room shouldn’t try to access these variables.

• Variables inside the room (i.e., inside the for-loop or if-statement) can access variables that were declared outside of it — as long as they were executed earlier

Page 12: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!12

In Summary

Scope

• It’s okay to access/use/update a variable if it’s:

• your code already executed the line that declares/initializes it

• the variable was declared with indentation that’s either (1) equal or (2) less than the line in question.

Otherwise, you can’t rely on the variable’s value, and often Python won’t even let you run such code.

Page 13: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!13

Scope

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

1

2

3

4

5

6

7

8

9

10

Page 14: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!14

Scope

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

1

2

3

4

5

6

7

8

9

10

Page 15: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!15

Scope

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

1

2

3

4

5

6

7

8

9

10

Page 16: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!16

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 17: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!17

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 18: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!18

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 19: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!19

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 20: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!20

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 21: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!21

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 22: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!22

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 23: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!23

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 24: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!24

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 25: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!25

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 26: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!26

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 27: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

!27

Scope

1

2

3

4

5

6

7

8

9

10

names = ["malik", "stephanie", "ellie", "rico"]

for name in names:

print(“list’s length:” + str(len(names)))

if name[0] == “e”:

starts_with_e = True

else:

starts_with_e = False

print(starts_with_e)

print(names)

Page 28: Introduction to Computation for the Humanities and Social ...cs.brown.edu/courses/cs0030/lectures/lecture12.pdfIntroduction to Computation for the Humanities and Social Sciences CS

LAB TIME