DATA STRUCTURES
In my previous post, we learned about the history of the python programming language and how wrote our first code in python. In this article, we are going to learn about data structures in python.
Data structures are containers that hold a different set of data. They include Lists, Tuples, and Dictionaries.
- LISTS
This is a collection of ordered items.
Items on a list are usually enclosed by the square brackets [] and each item is separated by a comma.
Example
We can create a list of days of the week
days_of_the_week = ["Monday","Tuesday","Wednesday"]
List manipulation
Various inbuilt functions are used to manipulate data in a list.
i. To access an item in the list above
element[index of the item]
Note: The index of elements begins at 0 so if we want to print the first element in our days of the week list we use the following code
print(days_of_the_week[0])
ii. To add an item to a list
element.append(new item)
This function adds an item at the end of the list
To add an item at a different position we use the following method
element.insert(index, item)
ii. To delete an item
element.remove(item to be removed)
element.pop() - Removes the last item in the list
NESTED LISTS
This is a list within a list
_Example_
days_of_the_week = [["Monday", "Tuesday"],["Wednesday"],["Thursday"]]
To access an item in a nested list, for example, to access "Wednesday" in the above-nested list we use
days_of_the_week[1][0]
i.e. days_of_the_week[index of the item to be displayed][index of the item]
- TUPLES
Tuples are a collection of ordered items, however, they differ from lists in that the information within tuples cannot be changed. The other difference is that items within a tuple are encircled within parenthesis ().
_Example_
days_of_the_week = ("Monday", "Tuesday","Wednesday")
- DICTIONARY
This is a collection of key, value pairs.
The items in a dictionary are usually enclosed within curly brackets and are separated by a comma.
Example
days_of_the_week = {"day1":"Monday", "day2":"Tuesday"}
In the above example "day1" is the key while "Monday" is the value
Key value pair manipulation
i. To add items
days_of_the_week["day3"] = "Wednesday"
Will add a new key-value pair to the above dictionary.
ii. To access value in a dictionary
print(days_of_the_week["day2"])
The above code will display "Tuesday" to the user
- SETS
This is an unordered list of items that do not allow duplication of elements.
There are two ways to create a set;
a. Using the inbuilt set function
element = set()
Example
days_of_the_week = (("Monday", "Tuesday","Wednesday"))
b. A set can be defined using the curly braces
Example
days_of_the_week = {"Monday","Tuesday","Wednesday"}
- OTHER DATA STRUCTURES
i. Stacks - This is a linear data structure that uses the last in first out (LO-FO), this means that when an element is added at one end an element can only be removed from that end
ii. Queues - They are similar to stacks however they are based on the First in/First our principle (FI-FO). The element that is least added is removed first.
To learn more on stacks and queues check out https://www.javatpoint.com/python-stack-and-queue
ALGORITHMS
This is a set of instructions that are executed to get the solution to a given problem.
- if
This checks if a certain condition is met and gives the desired outcome
Example
age = 16
if age > 18:
print("You are an adult")
The above checks whether the person has achieved the age of 18.
- if.....else
This checks if a condition is met, if the condition is not met, the program gives an alternative outcome
Example
age = 12
if age > 18:
print("You are an adult")
else:
print("You are not an adult")
- if....else if
This checks for multiple conditions.
Example
age = 12
if age <13:
print("You are a baby")
else if age <20:
print("You are a teenager")
else:
print("You are an adult")
- For loops
This is used to iterate over a sequence. This sequence can either be a list, a tuple, or a dictionary.
Example
days_of_the_week = ["Monday","Tuesday","Wednesday"]
for item in days_of_the_week:
print(item)
The outcome of the above code will be
Monday
Tuesday
Wednesday
- While loops
This is used to iterate over a sequence until a certain condition is met
Example
days_of_the_week = ["Monday","Tuesday","Wednesday"]
count = 0
while count <4:
for item in days_of_the_week:
print("item")
count += 1
The above code will run until it reaches a count of 4. And it will loop through the list and give an output of the items contained in the list.
For more on data structures and algorithms check out
https://www.w3schools.com/python/
NOTE: Indentations errors are common when dealing with algorithms.
Wrong indentation
for item in items:
print(item)
Correct indentation
for item in items:
print(item)
- Its also important not to forget the colon
For conventions check out
https://www.datacamp.com/community/tutorials/pep8-tutorial-python-code
Top comments (0)