![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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/1.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/2.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/3.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/4.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/5.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/6.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/7.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/8.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/9.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/10.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/11.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/12.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/13.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/14.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/15.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/16.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/17.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/18.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/19.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/20.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/21.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/22.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/23.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/24.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/25.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/26.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/27.jpg)
!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](https://reader031.vdocument.in/reader031/viewer/2022020217/5e18589501d4065912204ec8/html5/thumbnails/28.jpg)
LAB TIME