Algorithm and Data Structures Flashcards
What is a data structure?
A data structure is a way of storing and organizing data according to a certain format or structure, allowing us to access and manipulate the data in a certain way
Examples of data structures
tables to display schedules
a novel stores and organizes texts in paragraphs
Why are data structures important?
Data structures are a crucial part of computer programming as programming relies on our ability to store data, access that data, and manipulate that data to create the desired outcome.
Therefore, it is a top priority that engineers leverage data structures optimally to organize and manipulate data in an efficient and meaningful way
What are the 4 primary data structures in Python3?
- List
- Tuple
- Dictionary
- Set
So when thinking about a program… the data structure should be top of mind in the process of mapping out the path to success
- What are we trying to achieve?
- What data will we need?
- What data do we have already?
- What will we need to do with the data?
- What are our challenges?
- How is this data stored?
- How will we interact with the data?
- How will we store the data?
- How will we access the data?
- How will we manipulate the data to create our desired outcome?
- How will we overcome our challenges?
what are the conditions needed to produce an optimal output from the given input?
What is a list?
a data structure that allows us to store elements of different data types in one container, linearly. Each element is indexed from 0 and up
Are lists mutable?
Yes, lists are mutable.
Are lists ordered?
Yes, lists are ordered linearly with each element at a specific index. The first element is at index 0, the next is at index 1, and so forth
What are two ways to merge two lists together?
- using the + operator
merged_list = list_1 + list_2
- using the extend() property of a list to add the elements of one list at the end of another:
merged_list = list_1.extend(list_2)
two ways of adding elements to a list
- the append() method adds an element to the end of a list
list. append(new_element) - the insert() method adds an element at a specified index within a list
list. insert(1, new_element)
two ways of removing an element from a list
- the pop() method removes the last element from a list, and returns that element
list. pop() - the remove() method removes a specified element from a list
list. remove(this_element)
what is list slicing?
provides a sublist of the list being sliced.
the syntax for list slicing is:
list[starting_index, ending_exclusive_index, steps]
How can we verify the existence of an element in a list?
using the in operator
element_hello in list
this will return True if element_hello is in the list
and False if not in the list
What is list comprehension?
a technique that uses a for loop to create a new list from an existing list
since the result is always a NEW list, it’s good practice to assign list comprehension to a new variable
list comprehension is enclosed in square brackets
What is a tuple?
a tuple is similar to a list in that it can contain different data types and is ordered linearly and 0 indexed
a tuple is different because it is enclosed in parentheses AND is immutable. Meaning the tuple cannot be altered. BUT, the elements of a tuple are mutable and can be altered
since tuples are immutable, it is NOT possible to add or delete elements from an existing tuple