Python for Everybody (PY4E)
by Charles R. Severance (aka Dr. Chuck)
๐ฌ 17 sections with multiple video lessons
๐จโ๐ซ Prof. Dr. Charles R. Severance
โ Completely free
https://www.py4e.com/lessons
by Charles R. Severance (aka Dr. Chuck)
๐ฌ 17 sections with multiple video lessons
๐จโ๐ซ Prof. Dr. Charles R. Severance
โ Completely free
https://www.py4e.com/lessons
๐10โค4๐1
Common techniques for using the set() function in Python:
1. Create a set from a list:
1. Create a set from a list:
my_list = [1, 2, 2, 3, 3, 3]
my_set = set(my_list)
2.Add an item to a set:my_set = {1, 2, 3}
my_set.add(4)
3.Remove an item from a set by its value:my_set = {1, 2, 3}
my_set.remove(3)
4.Check if an item is in a set:my_set = {1, 2, 3}
if 3 in my_set:
print("The item is in the set.")
5.Get the length of a set:my_set = {1, 2, 3}
set_length = len(my_set)
6.Loop through the items in a set:my_set = {1, 2, 3}
for item in my_set:
print(item)
7.Get the union of two sets:set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
8.Get the intersection of two sets:set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
๐20
Common techniques for using the zip() function in Python:
1. Zip two lists together:
1. Zip two lists together:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
zipped_lists = zip(list1, list2)
2. Unzip a zipped list:zipped_lists = [(1, 4), (2, 5), (3, 6)]
list1, list2 = zip(*zipped_lists)
3. Loop through a zipped list:zipped_lists = [(1, 4), (2, 5), (3, 6)]
for item1, item2 in zipped_lists:
print(item1, item2)
4.Convert a zipped list to a dictionary:zipped_lists = [(1, 4), (2, 5), (3, 6)]
my_dict = dict(zipped_lists)
5. Zip three or more lists together:list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
zipped_lists = zip(list1, list2, list3)
๐8