[List] or "array" Motivate: 10 scores that need to input, process, and access repeatedly. 10 variables score1, score2,...score10 ??? Input into them? Find largest score, or sort them, or sum them ... Infeasible to make such code. Also, rigid and inflexible; it must be exactly 10 scores. Want to be able to have any number of scores. An array/list is used to conveniently store lots of data. This data can be accessed many times and places in the program. E.g. sorted order, outliers, duplicates, counts of each value, mode, median, distances from mean, MAD (mean absolute deviation), searchable (does x exist), ... A list contains many "items" / "elements" / "components". A list has a name and a size/length which is the number of elements it currently has. The elements can be accessed by numeric index, starting at 0, ending at size-1. Programmer's responsibility to ensure that index is within bounds (size of the array): A major source of errors in programs. The elements could be of different types, but usually all the same type. scores = [] creates a list variable. Required. Often, name is plural. OR: scores = list() is empty, length is 0. scores.append(something) appends to the end of the list num_scores = int(input("How many scores do you have to input: ")) scores = [] for i in range(num_scores): score = int(input("Enter a score:")) scores.append(score) print(scores) #displayed in list notation. Avoid #loop over the elements of a list using the 'in' operator for score in scores: print(score, end=" ") print() #Functions that have a list argument: len(scores) min(scores) #elements must be all strings or all numbers max(scores) # " sum(scores) #all elements must be numbers print("Number of scores: ",len(scores)) print("Min score:",min(scores)) print("Max score:",max(scores)) print("Sum of scores:",sum(scores)) average = sum(scores) / len(scores) print("Average score:",average) for score in scores: print("Difference of score from average:", score-average, end=" ") print() my_list = [init_value]*size create a list of length size, each element initialized to init_value digit_counters = [0]*10 #a counter for each digit print(digit_counters) num_numbers = int(input("How many integers: ")) for i in range(num_numbers): #loop over the numbers num = int(input("Input a positive integer: ")) for digit in str(num): #convert num to string, loop over the digit chars digit_counters[int(digit)] += 1 #increment count of that digit print(digit_counters) for d in range(10): print("Number of "+str(d)+"'s: "+str(digit_counters[d])) A list containing certain values that are useful for the program: #powers of 2 that have 'names' names = ['kilo','mega','giga','tera','peta','exa','zetta','yotta'] abbrevs = ['K','M','G','T','P','E','Z','Y'] for i in range(10,80+1,10): #powers from 10 to 80 print(f"2^{i} {names[i//10-1]:4s} = {2**i:25,}") # print("2^%2d %4s %1s = %19d"%(i,names[i//10-1],abbrevs[i//10-1],2**i)) *************************************************************** #loop over the elements of a list using the 'in' operator for name in ['Alice','Bob','Carol','Ted','Zippy']: print("Welcome,",name," Your name has",len(name),"characters.") #similar friends = ['Alice','Bob','Carol','Ted','Zippy'] for friend in friends: print("Welcome,",friend," Your name has",len(friend),"characters.") #Input the names of the friends num_friends = int(input("How many friends do you have to input: ")) friends = [] for f in range(num_friends): friends.append(input("Input the name of friend #"+str(f+1)+": "))