Data Abstraction in Python
lesson plan
Data Abstraction
- Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation
- data abstractions manage complexity in programs by giving a collection of data a name without refrencing the specific elements of the representation
- Data Abstraction makes it easier to implement, develop and maintain code
- The use of lists allows for multiple related values to be treated as a single value
- In AP exam the index values for lists start rom 1 and not 0 like in python.
Using simple lists and working with variables
- Lists are a powerful tool that can be used to organize, maintain and develop related data
- For example - A list of students in a class, if there was no list we would assign a variable to each student but if the data was in a list everyone would be under one variable. If a student left the class or was added instead of deleting entire variable we can edit the previous list.
- this helps organize data and make less mistakes
shopping_list = ["milk",
"pasta",
"eggs",
"spam",
"bread",
"rice"
] # list syntax is "[]"
another_list = shopping_list
print(id(shopping_list)) # each object has a uniques id; the id() function returns this id for the specifies object
print(id(another_list)) # As shopping_list is attritibuted to another_list, the id for both are the same as the object in questiom does not change
print(another_list)
shopping_list += ["cookies"] # adding cookies to the list
print(shopping_list)
print(id(shopping_list)) # the id does not change
print(another_list)
a = b = c = d = e = f = another_list # it is possibe to atrribute the same object to various variables
print(a)
print("Adding cream")
# .append() is a function which can be used to place new items into old lists
b.append("cream") # changes in one of the variables chnges all the lists in other as the object in question is the same.
print(a)
print(c)
print(d)
Breaking lists into more lists
- A list of data can be further segregated using more similar characteristics in the data
- For example, the code segment below segregates the data given into 2 new lists. One that one contains plants and one that contains only shrubs.
- This can be used to also organize data received from API's and other resources.
data = [
"Andromeda - Shrub",
"Bellflower - Flower",
"China Pink - Flower",
"Daffodil - Flower",
"Evening Primrose - Flower",
"French Marigold - Flower",
"Hydrangea - Shrub",
"Iris - Flower",
"Japanese Camellia - Shrub",
"Lavender - Shrub",
"Lilac - Shrub",
"Magnolia - Shrub",
"Peony - Shrub",
"Queen Anne's Lace - Flower",
"Red Hot Poker - Flower",
"Snapdragon - Flower",
"Sunflower - Flower",
"Tiger Lily - Flower",
"Witch Hazel - Shrub",
]
# two empty lists
flowers = []
shrubs = []
for plant in data: # A for loop that goes through each item in the list
if "Flower" in plant:
flowers.append(plant) # executed if "flowers" is in the item
elif "Flower" not in plant:
shrubs.append(plant) # executed if "shrubs" is in the item
print("Shrubs {}".format(shrubs)) # The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}
print("Flowers {}".format(flowers))
albums = [("Welcome to my nightmare", "Alice cooper", 1975),
("Bad Company", "Bad Company", 1974),
("Nightflight", "Budgie", 1981),
("More Mayhem", "Emilda May", 2011),
("Ride the Lightning", "Metallica", 1984),
]
print(len(albums)) # number of items in the list
for name, artist, year in albums:
print("Album: {}, Artist: {}, year: {}"
.format(name, artist, year)) #returns a segregated and labled presentation of the songs
panagram = """the quick brown
fox jumps\tover
the lazy dog"""
words = panagram.split() # splitting the string above into individual words. Separator here is any whitespace.
print(words)
numbers = "9,223,372,036,854,775,807"
print(numbers.split(",")) # separator is ","
generated_list = ['9', ' ',
'2', '2', '3', ' ',
'3', '7', '2', ' ',
'0', '3', '6', ' ',
'8', '5', '4', ' ',
'7', '7', '5', ' ',
'8', '0', '7', ' ',
]
values = "".join(generated_list) # converting the list into a string
print(values)
values_list = values.split() # separator is any whitespace
print(values_list)