Python Programming with Corey Schafer_Notes_4
Python Programming with Corey Schafer_Notes_4
• Making a list
courses = ['Maths, 'History', 'Physics']
print(courses)
→ ['Maths, 'History', 'Physics']
• List index out of range error for an index that doesn’t exist
Alternatively
courses = ['Maths', 'History', 'Physics']
courses.pop()
print(courses)
→ ['Maths', 'History']
Last item was removed from the list
• Other functions
nums = [1, 5, 2, 4]
print(min(nums))
→1
It gives the minimum number
nums = [1, 5, 2, 4]
print(max(nums))
→5
It gives the maximum number
nums = [1, 5, 2, 4]
print(sum(nums))
→ 12
It gives the sum of all the numbers
• Modification of list
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']
Changes both lists are they are modifiable
• Sets use {}
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
print(cs_courses)
→ {'History', 'Math', 'Physics', 'CompSci'}
→ {'Math', 'CompSci', 'History', 'Physics'}
This will print different value each time as sets do not care about the order of values
• Sets are used to determine values they share with other sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.intersection(art_courses))
→ {'History', 'Math'}
It gives back common values
• Sets are used to determine values they do not share with other sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
art_courses = {'History', 'Math', 'Art', 'Design'}
print(cs_courses.difference(art_courses))
→ {'CompSci', 'Physics'}
It gives back values of cs_courses that are not present in art_courses
empty_set = {}
empty_set = set()
Only the second method can be used. The first method would create empty dictionary