Unit 9: Data Structures Flashcards
What are the two types of data structures?
Lists and dictionaries
Can you add or remove items from a lists or dictionary?
Can you join 2 lists together?
Yes, you will learn how to add and remove items from a list or dictionary. You will also learn how to join 2 lists together and how dictionaries are different from lists.
What are the two other ways to store data?
We’ll add to our knowledge of variables with two other ways to store data: lists and dictionaries.
We have talked about how to store integers and strings in variables. Can you store a collection of items into just one variable?
Yes
A collection of items is called a ______
list
Code a list of friends– ( their names are Mary, Joe, Mark, and Emily) invited to a party and print them out.
invited_friends = [‘Mary’,’Joe’,’Mark’,’Emily’]
print(invited_friends)
Why are lists powerful?
Lists are particularly powerful because we can access each item in the list individually as well as the whole list as a group.
When you use computers, counting tends to start at 0, so to access the second item, we use position _____
“1”
Code a list of friends– ( their names are Mary, Joe, Mark, and Emily) invited to a party and print them out and access the second person.
invited_friends = [‘Mary’,’Joe’,’Mark’,’Emily’]
print(invited_friends)
print(invited_friends[1])
Code a list of friends– ( their names are Mary, Joe, Mark, and Emily) invited to a party and print them out and use a for loop.
invites_list = [‘Mary’,’Joe’, ‘Mark’, ‘Emily’]
for count in range(0,4):
print(invites_list[count])
The for loop lets us step through each item on the list and print it out. We manually set the end of our loop to 4 so that it would go through each of the positions.
Code a list of friends– (you do not now how many items are in our list) invited to a party and print them out.
Use a for loop.
Python gives us a special function to find out how many items are in a list–> len()
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
length = len(invites_list)
print(length)
Output:
4
We can use this variable in our for loop so that our loop always works no matter how long the list is.
We can use this variable–> len() in our for loop so that our loop always works no matter how long the list is. Show an example of this given this information:
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
length = len(invites_list)
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
length = len(invites_list)
for count in range(0,length):
print(invites_list[count])
What if we want to add, remove or change things in our list?
Change:
Instead of Joe coming to our party, his sister Lisa is going to come instead, so let’s change his name.
Add:
We have decided that we want to add Mandy and Carlos to our invitation list so we’re going to add their names to the end of our list.
Remove:
Mark can’t make it for the party.
Changing things–> just like you can access the items in the list with the bracket notation, we can change things in the list with it.
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
print(invites_list[1])
invites_list[1] = “Lisa”
print(invites_list[1])
Output:
Joe
Lisa
We add items to a list after we created it using append()
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
invites_list.append(“Mandy”)
invites_list.append(“Carlos”)
print(invites_list)
Remove:
we use the del command to remove an item.
We need to know what position it is for it to work. Mark is item 2, in the third position in the list.
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
invites_list.append(“Mandy”)
invites_list.append(“Carlos”)
del invites_list[2]
print(invites_list)
How do you add lists together?
Refer to this example and think about how you can add lists together.
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
invites_list.append(“Mandy”)
invites_list.append(“Carlos”)
del invites_list[2]
more_invites = [“Sherry”, “Michael”, “Nick”]
print(more_invites)
We can use the + sign to add the two lists together.
We can either assign it to the same list variable or to a new one.
New one:
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
invites_list.append(“Mandy”)
invites_list.append(“Carlos”)
del invites_list[2]
more_invites = [“Sherry”, “Michael”, “Nick”]
full_invite_list = invites_list + more_invites
print(full_invite_list)
Adding the new items to one of our original list variables:
invites_list = [‘Mary’,’Joe’,’Mark’,’Emily’]
invites_list.append(“Mandy”)
invites_list.append(“Carlos”)
del invites_list[2]
more_invites = [“Sherry”, “Michael”, “Nick”]
invites_list = invites_list + more_invites
print(invites_list)
We are going to create a list of test scores. Jake got a 93 on the first test, an 87 on the second test, and a 98 on the third test.
Let’s create a list on the scores and use a for loop to calculate the average of our four test scores.
Then, Jake’s teacher let him retake the fourth test and he got a 92 on the retake. Let’s change that test score and then calculate the average again.
Finally, Jake’s teacher decided not to include the third test score in his final grade. Now we need to remove it and check his average again.
We will use the len() function to find out how many test scores there are so that we can use this code again if we need to do so.
test_scores = [93,87,98]
def calculate_average(scores):
total = 0
number_of_scores = len(scores)
for count in range(0, number_of_scores):
total = total + scores[count]
average = total/ number_of_scores
return average
test_scores[3] = 92
del(test_scores[2])
print(calculate_average(test_scores))
Drag and drop each line of code in the program to complete the following:
Create a list of items containing 7 programming languages.
“Ada”
“Basic”
“C”
“C++”
“Java”
“Python”
“Scratch”
Display the list using the print function.
Print the first item in the list.
Print the last item in the list.
Print the length of the list.
Print the last item in the list using the len() function.
Print all items in the list using a for loop.
Add a new programming language, “Smalltalk” to the end and print the updated list.
Remove the language “Basic” and print the updated list.
Print the last item
programming_langs = [‘Ada’,’Basic’, ‘C’, ‘C++’,’Java’,’Python’, ‘Scratch’]
print(programming_langs)
# Print the first item
print(programming_langs[0])
#Print the last item
print(programming_llangs[6])
# Print the length of list
print(len(programming_langs))
# Print the last item using length function
print(programming_langs[len(programming_langs)-1])
# Print all items using a for loop
for i in range(0,len(programming_langs)):
print(programming_langs[i])
# Add new language to the list and print result
programming_langs.append(“Smalltalk”)
print(programming_langs)
# Remove the language Basic and print result
del programming_langs[1]
print(programming_langs)
Activity: Continuing our list of programming langs
Drag and drop each line of code in the program to complete the following:
Create a list of items containing 7 programming languages.
“Ada”
“Basic”
“C”
“C++”
“Java”
“Python”
“Scratch”
Create a list of containing 3 programming languages.
“C#”
“Cobol”
“Fortran”
Add the 2 lists together, store this new list in programming_langs and print the result.
Sort the new list using the sort() method and print result. Note that sort() sorts the items of a list in ascending order. Unlike append() or insert(), the sort() method does not take any arguments.
Create the list of 7 items
programming_langs = [‘Ada’,’Basic’, ‘C’, ‘C++’, ‘Java’, ‘Python’, ‘Scratch’]
more_langs = [‘C#’,’Cobol’, ‘Fortran’]
programming_langs = programming_langs + more_langs
print(programming_langs)
programming_langs.sort()
print(programming_langs)
What does sort() do?
sort() sorts the items of a list in ascending order. Unlike append() or insert(), the sort() method does not take any arguments.
As we learned in previous units, when we’re Inside a loop, we iterate over a block of code for a specific number of times or until the test expression is false. However, sometimes we wish to end, or terminate the loop early or don’t want to execute the rest of the loop body for the current iteration for a specific condition within the loop. What do we use in those cases when we want to explicitly control the flow of the loop?
break and continue statements
What is the break statements used for?
The break statement is used when we want to stop a loop before it would normally stop. The break ends the iteration of the loop and moves the control flow to the next line of code immediately after the loop body. You can check the flowchart in CTY
If the break statement is inside a nested loop (loop inside another loop), the break will _________
the break will end only the innermost loop.
Let’s take a look at how this might be useful when looping through the items of a list. Say you have a list of fruits and you’re checking whether “banana” is inside the loop. If you find it, you are finished and there is no need to continue searching through the list. You should then use the _______ to terminate your search.
break statement.
break with lists.
my_fruits_list = [‘Apple’, ‘Banana’, ‘Orange’, ‘Mango’, ‘Berry’]
for fruits in my_fruits_list:
if fruits == ‘Banana’:
print(“Found banana. No need to search anymore”)
break
print(fruits)
print(“Out of the loop”)