Data Abstraction in Python
2022-08-29 Lecture- Data Abstraction in Python
Data Abstraction
To properly manage our data while coding, we might require different types of data structures to successfully attain the goal of our program. Properly storing our data makes it easier for us to share information with fellow devs, across the internet, and also working in other technlogical fields.
The following are some comm ways to stored structured data in programing
Variables
Variables are contianers of data. They are the most primative and basic structure of data containing only a reference and a value, the value of the variable can be dynamic, and the value can be referenced to by the name of the variable. There are many primative data types in the python programing language, but a unique characteristic of python is that variables are given a data type at assignment.
- Some primative data types are integers, floats, strings (array of chars), chars, etc.
msg = "Hello World"
print(msg)
Python Lists
Lists are sequential containers of data that could store multiple values per key. Lists in python are denoted by brackets. We can check that our list is of the list data type with the type()
function
Always be precise and detailed when noting down variable names.
Each list has a length attribute that denotes how big or long the list is. in python, this is achieved via the len()
function
langs = ["cpp", "python", "html", "css", "java", "javascript"]
print(langs)
# Getting the data type
print(type(langs))
# Printing the length
print(len(langs))
# Printing the 3rd element
print(langs[2])
Dictionaries
Dictionaries are sequential containers of data that are similar to lists but contain pairs of keys and values. Dictionaries in python are denoted by curly braces.
We can access the keys of the dictionary with the .keys()
method, and we can access the values of the dictionary with the .values()
method. Additionally, we can also use the .get()
method to get a value from a key.
Dictionaries are very similar to a JSON in how it stores its data.
info = {
"name": "Alex Lu",
"age": 15,
"score": 3,
"langs": "cpp"
}
print("info: ", info, "type: " + str(type(info)), "Length: " + str(len(info)))
for key in info.keys():
print(key)
print("---------")
for val in info.values():
print(val)
print("--------- Now using .get()")
for key in info.keys():
print(key + ":", info.get(key))
To add to a list, we can use the .append()
method to add in more inputs to the list. I have modified the InfoDb List from class and added my own entries and values into the dictionary.
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person
InfoDb.append({
"FirstName": "John",
"LastName": "Mortensen",
"Gender": "Male",
"DOB": "October 21",
"Residence": "San Diego",
"Email": "jmortensen@powayusd.com",
"Fav_Color": "Unknown",
"Sleep_Schedule" : "Unknown",
"is_Teacher": True,
"Hobbies": ["Teaching", "Programming"],
"Games": []
})
InfoDb.append({
"FirstName": "Alex",
"LastName": "Lu",
"Gender": "Male",
"DOB": "November 29, 2006",
"Residence": "San Diego",
"Email": "maodou1258@gmail.com",
"Fav_Color": "White",
"Sleep_Schedule" : "1am",
"is_Teacher": False,
"Hobbies": ["Programming", "Tennis", "Reading", "Sleeping"],
"Games": ["League of Legends", "VALORANT", "Minecraft", "OSU"]
})
InfoDb.append({
"FirstName": "Evan",
"LastName": "Aparri",
"Gender": "Male",
"DOB": "November 10, 2005",
"Residence": "San Diego",
"Email": "evanaparri@gmail.com",
"Fav_Color": "Blue",
"Sleep_Schedule" : "2am",
"is_Teacher": False,
"Hobbies": ["Running", "Reading", "Eating Asian Food/ Food Connoisseur", "Sleeping", "Homework", "Programming"],
"Games": []
})
# Print the data structure
print(InfoDb)
If we want to add values to InfoDb, this could easily be achived with the .append()
method
def add_entry():
name = input("First and last name seperated by spaces: ").split()
gender = input("What is your gender? ")
birthdate = input("When were you born? ") # Not gonna care about date time formatting cuz all programmers hate that
residence = input("What city do you live in? ")
email = input("What is your email? ")
color = input("What is your favorite color? ")
sleep = input("When do you sleep? ")
is_teacher = bool_input("Are you a teacher? [yes/no] ")
hobbies = []
print("Now asking for Hobbies ")
hobbies = multi_input(hobbies)
games = []
print("Now asking for Games ")
games = multi_input(games)
entry = {
"FirstName": name[0],
"LastName": name[1],
"Gender": gender,
"DOB": birthdate,
"Residence": residence,
"Email": email,
"Fav_Color": color,
"Sleep_Schedule": sleep,
"is_Teacher": is_teacher,
"Hobbies": hobbies,
"Games" : games
}
return entry
def bool_input(prompt):
while True:
try:
return {"yes":True,"no":False}[input(prompt).lower()]
except KeyError:
print("Invalid input please enter yes or no")
def multi_input(arr):
i = 0
temp = ""
while temp != "cancel":
i+=1
temp = input("Please enter item number {0}, type cancel to proceede ".format(str(i)))
if temp.lower != "cancel":
arr.append(temp)
return arr
new_entry = add_entry()
InfoDb.append(new_entry)
print(InfoDb[-1])
Formatting and printing the data in the list
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Gender:", d_rec["Gender"])
print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Email:", d_rec["Email"])
print("\t", "Favorite Color:", d_rec["Fav_Color"])
print("\t", "Is Teacher?:", d_rec["is_Teacher"])
print("\t", "Hours of Sleep:", d_rec["Sleep_Schedule"])
print("\t", "Hobbies: " + ", ".join(d_rec["Hobbies"]))
print("\t", "Games: " + ", ".join(d_rec["Games"]))
print()
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
Now we print the entires of InfoDb in reverse
The first method uses array slicing, while the second method iterates in reverse order starting from the last index of the array, this is accomplished through the reversed()
function paired with the range()
and len()
functions.
def for_loop_reversed_1(arr):
print("Cheesed Reverse For loop output:")
for i in arr[::-1]:
print_data(i)
def for_loop_reversed_2(arr):
for i in reversed(range(0,len(arr))):
print_data(arr[i])
# for_loop_reversed_1(InfoDb) does the same thing
for_loop_reversed_2(InfoDb)
def for_loop(arr):
print("Output of Database")
# The for loop declares a local variable "i" and assigns each DB record to i over each iteration
for i in arr:
# Using the predermined print_data function to neatly format our data in a human friendly manner
print_data(i)
print(type(i)) # Note that "i" will retain its value outside of the for-loop, but not outside of the function
for_loop(InfoDb)
def for_loop_with_index(arr):
print("Output of Database")
# Using the range function to print out data based on the index in the array
for i in range(0,len(arr)):
# Using index notation to print data
print_data(arr[i])
for_loop_with_index(InfoDb)
def while_loop(arr):
print("Output of Database")
# Initializing a counter variable to keep track of the current index of InfoDb
i = 0
# While loop to run certain processes before i becomes equal to the max index + 1, terminate once i is equal to len(arr)
while i < len(arr):
print_data(arr[i])
i+=1 # I FORGOT TO DO THIS OH GOD WHY MY VSCODE IS BROKEN
while_loop(InfoDb)
def recursion(arr, counter):
# Check if the current index is within bounds
if counter < len(arr):
# prints data
print_data(arr[counter])
# Recalling the same function for counter+1 index, printing the next item in the list
recursion(arr, counter+1)
# If previous if statement does not pass, return 0 and terminate the recursion
return 0
recursion(InfoDb, 0)
Some cool list operations including for loops and recursion in cluding reversing, list comprehension, and recursion
print("List comprehension, create a list on the spot")
my_arr1 = [2<<i for i in range(0,10)]
print("List of squares of 2: ", my_arr1)
print("\nReversing a list")
my_arr2 = [1,2,3,4,5]
print("Original List: ", my_arr2)
print("Reversed List: ", my_arr2[::-1])
print("\nSorting a list")
my_arr3 = [67,84,19,43,175,78]
print("Sorted ascending: ", sorted(my_arr3))
print("Sorted descending: ", sorted(my_arr3, reverse=True))
print("\nEXTRA: Qsort using list comprehension")
def Qsort(arr):
if arr == []:
return []
pivot = arr[0]
l = Qsort([i for i in arr[1:] if i < pivot])
r = Qsort([i for i in arr[1:] if i >= pivot])
return l + [pivot] + r
print(Qsort(my_arr3))
q_a = {
"What is the derivative of position?" : ["velocity"],
"What is the integral of acceleration?" : ["velocity"],
"Name a product of photosynthesis" : ["glucose", "oxygen"],
"What rhetorical device includes the repitition of a word or phrase at the end of clauses?" : ["epistrophe"]
}
# Returns a tuple of lists of questions and values
def get_pair(dict):
return [q for q, a in dict.items()], [a for q, a in dict.items()]
# Function to calculate percentage
def percentage(x, y):
return '{0:.2f}'.format(100 * float(x)/float(y))
# Some States
correct = 0
total = len(q_a)
# Get the answer + question pairs
q_a_pairs = get_pair(q_a)
# Ask and evaluate questions
for i in range(0,len(q_a_pairs[0])):
rsp = input("QUESTION: " + q_a_pairs[0][i])
# Use .lower to maintain case insensitivity
if rsp.lower() in q_a_pairs[1][i]:
print(rsp + " was correct!")
correct+=1
else:
print("Wrong, {0} was incorrect, the correct answer was: {1}".format(rsp, ", ".join(q_a_pairs[1][i])))
print("Congrats, you got {0}% on this quiz".format(percentage(correct,total)))
It's just too free 😎