Python - Lists, Tuples, and Sets
** Python - Lists, Tuples, and Sets
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[0:2])
---------------------------------
['History', 'Math']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[:2])
---------------------------------
['History', 'Math']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses[2:])
---------------------------------
['Physics', 'CompSci']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.append('Art')
print(courses)
---------------------------------
['History', 'Math', 'Physics', 'CompSci', 'Art']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.insert(0, 'Art')
print(courses)
---------------------------------
['Art', 'History', 'Math', 'Physics', 'CompSci']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.insert(0, courses_2)
print(courses)
print(courses[0])
---------------------------------
[['Art', 'Education'], 'History', 'Math', 'Physics', 'CompSci']
['Art', 'Education']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.extend(courses_2)
print(courses)
print(courses[0])
---------------------------------
['History', 'Math', 'Physics', 'CompSci', 'Art', 'Education']
History
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Art', 'Education']
courses.append(courses_2)
print(courses)
print(courses[4])
---------------------------------
['History', 'Math', 'Physics', 'CompSci', ['Art', 'Education']]
['Art', 'Education']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.remove('Math')
print(courses)
---------------------------------
['History', 'Physics', 'CompSci']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.pop()
print(courses)
---------------------------------
['History', 'Math', 'Physics']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
poped = courses.pop()
print(poped)
print(courses)
---------------------------------
CompSci
['History', 'Math', 'Physics']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.reverse()
print(courses)
---------------------------------
['CompSci', 'Physics', 'Math', 'History']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
courses.sort()
print(courses)
---------------------------------
['CompSci', 'History', 'Math', 'Physics']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
nums = [1, 5, 2, 4, 3]
courses.sort()
nums.sort()
print(courses)
print(nums)
---------------------------------
['CompSci', 'History', 'Math', 'Physics']
[1, 2, 3, 4, 5]
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
nums = [1, 5, 2, 4, 3]
courses.sort(reverse=True)
nums.sort(reverse=True)
print(courses)
print(nums)
---------------------------------
['Physics', 'Math', 'History', 'CompSci']
[5, 4, 3, 2, 1]
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
sorted(courses)
print(courses)
---------------------------------
['History', 'Math', 'Physics', 'CompSci']
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
sorted_courses = sorted(courses)
print(sorted_courses)
---------------------------------
['CompSci', 'History', 'Math', 'Physics']
---------------------------------
==========================================
nums = [1, 5, 2, 4, 3]
print(min(nums))
print(max(nums))
print(sum(nums))
---------------------------------
1
5
15
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses.index('CompSci'))
---------------------------------
3
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print(courses.index('Art'))
---------------------------------
ValueError: 'Art' is not in list
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
print('Art' in courses)
print('Math' in courses)
---------------------------------
False
True
---------------------------------
==========================================
ourses = ['History', 'Math', 'Physics', 'CompSci']
for item in courses:
print(item)
---------------------------------
History
Math
Physics
CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
for course in courses:
print(course)
---------------------------------
History
Math
Physics
CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
for index, course in enumerate(courses):
print(index, course)
---------------------------------
0 History
1 Math
2 Physics
3 CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
for index, course in enumerate(courses, start=1):
print(index, course)
---------------------------------
1 History
2 Math
3 Physics
4 CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
course_str = ', '.join(courses)
print(course_str)
---------------------------------
History, Math, Physics, CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
course_str = ' - '.join(courses)
print(course_str)
---------------------------------
History - Math - Physics - CompSci
---------------------------------
==========================================
courses = ['History', 'Math', 'Physics', 'CompSci']
course_str = ' - '.join(courses)
new_list = course_str.split(' - ')
print(course_str)
print(new_list)
---------------------------------
History - Math - Physics - CompSci
['History', 'Math', 'Physics', 'CompSci']
---------------------------------
==========================================
list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1
print(list_1)
print(list_2)
list_1[0] = 'Art'
print(list_1)
print(list_2)
---------------------------------
['History', 'Math', 'Physics', 'CompSci']
['History', 'Math', 'Physics', 'CompSci']
['Art', 'Math', 'Physics', 'CompSci']
['Art', 'Math', 'Physics', 'CompSci']
---------------------------------
==========================================
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1
print(tuple_1)
print(tuple_2)
---------------------------------
('History', 'Math', 'Physics', 'CompSci')
('History', 'Math', 'Physics', 'CompSci')
---------------------------------
==========================================
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1
print(tuple_1)
print(tuple_2)
tuple_1[0] = 'Art'
print(tuple_1)
print(tuple_2)
---------------------------------
('History', 'Math', 'Physics', 'CompSci')
tuple_1[0] = 'Art'
TypeError: 'tuple' object does not support item assignment
---------------------------------
==========================================
#Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
print(cs_courses)
---------------------------------
{'Physics', 'CompSci', 'History', 'Math'}
---------------------------------
{'Physics', 'Math', 'CompSci', 'History'}
---------------------------------
{'Math', 'History', 'Physics', 'CompSci'}
---------------------------------
... 실행될 때 마다` 원소의 위치가 바뀜.
---------------------------------
==========================================
#Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print(cs_courses)
---------------------------------
{'Physics', 'History', 'CompSci', 'Math'}
---------------------------------
` 중복된 원소는 한 번 만 출력
---------------------------------
==========================================
#Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print('Math' in cs_courses)
---------------------------------
True
---------------------------------
==========================================
#Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.intersection(art_courses))
print(cs_courses.difference(art_courses))
print(art_courses.difference(cs_courses))
print(cs_courses.union(art_courses))
---------------------------------
{'History', 'Math'}
{'Physics', 'CompSci'}
{'Art', 'Design'}
{'History', 'Math', 'Design', 'Physics', 'CompSci', 'Art'}
---------------------------------
==========================================
# Empty Lists
empty_list = []
empty_list = list()
# Empty Tuples
empty_tuple = ()
empty_tuple = tuple()
# Empty Sets
empty_set = {} # This isn't right! It's a dict
empty_set = set()
==========================================