my_list = [2, 3, 1, 2]my_list.append(3)my_list.insert(0, 1)my_list[3] =4my_list # evaluate what this list holds at this pointmy_list[6] =10my_tupple = (3, 4, 1)my_tupple.append(3)my_tupple # evaluate what this tuple holds at this pointmy_dictionary = {3: 4, 1: 2, 5: 4}my_dictionary[5] =10my_dictionary # evaluate what this dictionary holds at this pointmy_dictionary.append(4: 5)my_dictionary # evaluate what this dictionary holds at this point
Evaluate the code
Indicate when errors are thrown
my_list = [2, 3, 1, 2]my_list.append(3)my_list.insert(0, 1)my_list[3] =4my_list # evaluate what this list holds at this point
[1, 2, 3, 4, 2, 3]
# error --- my_list[6] = 10my_tupple = (3, 4, 1)# error --- my_tupple.append(3)my_tupple # evaluate what this tuple holds at this point
(3, 4, 1)
my_dictionary = {3: 4, 1: 2, 5: 4}my_dictionary[5] =10my_dictionary # evaluate what this dictionary holds at this point
{3: 4, 1: 2, 5: 10}
# error --- my_dictionary.append(4: 5)my_dictionary # evaluate what this dictionary holds at this point
{3: 4, 1: 2, 5: 10}
Iterating over data structures
Use for x in data_structure to retrieve values/keys (cannot make changes with this type of loop)
my_list = [3, 5, 5]for value in my_list:print(value)
3
5
5
my_tuple = (3, 5, 5)for value in my_tuple:print(value)
3
5
5
Iterating over dictionaries
my_dictionary = {3: "a", 5: "b"}for key in my_dictionary:print(key)
3
5
You can change values in a dictionary with for key in dictionary
It counts how many unique names there are in the file
It returns an integer with the count
Use a list for this
assert count_names("names.txt") ==11
Write a function – solution
def count_names(file_name): f =open(file_name, "r") name_list = []for line in f: name = line.strip()if name notin name_list: name_list.append(name) f.close()returnlen(name_list)def main():assert count_names("names.txt") ==11main()
Submit code for attendance
Submit your count_names functions to Gradescope for attendance.
It counts how many unique names there are in the file
It returns an integer with the count
Use a dictionary for this
assert count_names("names.txt") ==11
Write a functionn – solution
def count_names(file_name): f =open(file_name, "r") name_dict = {}for line in f: name = line.strip()if name notin name_dict: name_dict[name] ="" f.close()returnlen(name_dict)def main():assert count_names("names.txt") ==11main()