Chapter 3 : Phython Codes Flashcards
What is a data structure
A data structure s a special kind of variable that can hold serveral values.
Give 3 examples of storing data values
- Social media platforms storing data on all the different accounts.
- A team manager who wants to store data on all his team players.
- A busniess owner storing data on all their products
What does traversing mean?
To look at every value on a data structure
Name 2 reasons people might want to traverse data
- A team manager wants to send an email to all its players
- Social Media platofrm wants to know which accounts are online and which are offline.
How can a user change the size of a list?
- Append a value to make the list longer
- Delete a value to make the list shorter
What happens if the stop value doesnt match the size of the list?
It will either end too early, missing a few values or it will go on for too long, causing an out of bounds error.
Phython code for traversing a list
topiclist = [“A”, “B”, “C”]
for i in range [3] :
print (i, topiclist[i])
Phython code for finding the list length
topiclist = [“A”, “B”, “C”]
stop = len(topiclist)
for i in range (stop) :
print (i, topiclist[i])
What is a program interface?
The part of the program that handles input and output.
Name 5 requirments for a program interface
- Tell the user what the program is and what does it do
- Allow the user to exit the program
- Allow the user to enter an input
- Let the user make choices or select options
- Display results or answers
What is the error in this program?
topiclist = []
choice == “X”
while choice == “X” :
print(T O P I C S)
print(===================)
print(“\n”)
print(“A: Append a value”)
print(“B: Print the teamlist”)
print(“X: Exit the Program”)
The wrong logical test is used.
It should be != instead of ==
What is the error in this program?
topiclist= []
while choice != “X” :
print(T O P I C S)
print(===================)
print(“\n”)
print(“A: Append a value”)
print(“B: Print the teamlist”)
print(“X: Exit the Program”)
choice = input(“Enter your choice: “)
Choice is not defined as it has not been given a value yet.
What is the error in this program?
choice = “ “
while choice != “X” :
topiclist []
print(T O P I C S)
print(===================)
print(“\n”)
print(“A: Append a value”)
print(“B: Print the teamlist”)
print(“X: Exit the Program”)
choice = input(“Enter your choice: “)
The topiclist is inside the loop. Whenever the loop is repeated, the topiclist will be cleared and go back to being empty.
What is nesting?
When a program structure is inside of another structure.
What is the command to print an element?
i = input (“Which list item do you want to print?”)
i = int(i)
print (topiclist[i])
What is the command to edit an element?
i = input (“Which list item do you want to edit?”)
i - int(i)
teamlist[i] = input (“Enter a new value.”)
What is the command to delete an element?
i = input (“Which list item do you want to delete?”)
i - int(i)
del topiclist[i]
What is the full program using double indentation?
teamlist = []
choice = “ “
while choice != “X” :
print(“ T O P I C S”)
print(“=======================”)
print(“This list will show you all the topics”)
print(“\n”)
print(“A: Append a value”)
print(“B: Print the topic list”)
print(“X: Exit the program”)
if choice == “A” :
topic = input(“Enter a new topic”)
topiclist.append(topic)
if choice == “B” :
print(topiclist)