Fundamentals | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 Data Abstraction
Student led teaching on Data Abstraction. Learn how data abstraction involves managing complexity in programs
Data Abstraction
Choosing the Right Data Type in Python
In Python, choosing the appropriate data type for your variables is essential to ensure efficient and correct program execution. Here’s when to use some common data types:
Integers (int
)
- Use Cases: Use integers when you need to represent whole numbers without fractional parts. Integers are suitable for counting, indexing, and performing integer arithmetic.
Floating-Point Numbers (float
)
- Use Cases: Use floating-point numbers when you need to represent real numbers with decimal points. They are suitable for calculations that involve fractions or decimal values.
Strings (str
)
- Use Cases: Use strings when you need to work with text data, such as names, sentences, or any sequence of characters. Strings are versatile and can store both plain text and formatted text.
Lists (list
)
- Use Cases: Use lists when you need an ordered collection of items that can be modified. Lists are ideal for storing multiple values of the same or different data types.
Tuples (tuple
)
- Use Cases: Use tuples when you need an ordered collection of items that should remain immutable (unchangeable). Tuples are useful for representing fixed sequences of data.
Dictionaries (dict
)
- Use Cases: Use dictionaries when you need to store key-value pairs, where each key maps to a specific value. Dictionaries are efficient for looking up values based on keys.
Sets (set
)
- Use Cases: Use sets when you need an unordered collection of unique elements. Sets are useful for tasks like removing duplicates from a list or performing set operations.
Booleans (bool
)
- Use Cases: Use booleans to represent binary values, such as
True
orFalse
. They are essential for making decisions and controlling the flow of your program using conditional statements.
None (NoneType
)
- Use Cases: Use
None
when you want to represent the absence of a value or indicate that a variable has no assigned value. It’s often used as a placeholder or default initial value.
Choosing the right data type helps make your code more readable, efficient, and easier to maintain. Understanding the characteristics and limitations of each data type is essential for effective Python programming.
Represent a list or string using a variable.
# We are going to cover representing lists or strings with a variable.
# A list is an ordered sequence of elements. An element is an individual value in a list that is assigned a unique index.
# For example, if I have a grocery list with milk, eggs, and cookies, each item is an element.
groceryList = ["milk", "eggs", "cookies"]
# one ^ two ^ three ^
# The index is a method for referencing the elements in the list. For example, if I reference item 1, it will be milk.
# IMPORTANT: most if not all programming languages index at 0. This means that the 0th element would be milk and the 1st element is eggs.
# For the AP Exam, indexing will be at 1.
# Next is a string which is an ordered sequence of characters.
# This may contain letters, numbers, and special characters. Examples include:
string = "Perry"
string2 = "12"
string3 = "!@#"
stringn4 = "Perry12!@#"
Develop data abstraction using lists to store multiple elements.
# Data abstraction allows for the reduction of a particular body of data to a simplified representation of the whole.
# For example, saying that I have a “grocery list” is a lot easier than saying “I have a list with milk, eggs, and cookies.”
groceryList = ["milk", "eggs", "cookies"]
# bundle ^
# In this example, we bundle this data into one term and simplify it.
# In programming, you can bundle variables together by type and you can name your bundle (name your list).
# With a list, you don’t need to know how many variables will be needed or how the elements will be stored together.
# The exam reference sheet will provide notation for lists.
How does data abstraction manage complexity in program code?
# Data abstraction can manage complexity in programs by bundling data together.
# This can result in a program that is easier to develop and maintain.
# It also allows multiple related items to be treated as a single value.
# You also need less variables since one variable holds all the other ones and it allows you to easily modify these variables.
groceryList = ["milk", "eggs", "cookies"] # nice and short
# long and bad
item1 = "milk"
item2 = "eggs"
item3 = "cookies"
# modify variables:
groceryList = ["milk", "eggs", "cookies"] # nice and short
print("Initial List")
print(groceryList)
# replace the index 1 with chocolate
groceryList[1] = "chocolate" # notice how index 1 is the 2nd element (AP exam index at 1)
# add a new element
groceryList.append("flour")
print("Print List with Flour addeded")
print(groceryList)
# remove element
groceryList.remove("milk")
print("Print List with Milk removed")
print(groceryList)
Initial List
['milk', 'eggs', 'cookies']
Print List with Flour addeded
['milk', 'chocolate', 'cookies', 'flour']
Print List with Milk removed
['chocolate', 'cookies', 'flour']
JSON implementation
JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. It has several uses. For example, if someone is managing an inventory of cars, they can use JSON to import data and export it.
JSON is used when data is sent from a web server to a webpage.
So the car person can use JSON to display their current inventory of cars to sell on their webpage.
Converting from PYTHON TO JSON
import json # you will need to import this library
# python dictionary
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON
# we first CALL the library and use the dumps() function
# to call the library and use dumps(), we do json.dumps()
# note: do you know of function machines in math? where you put in the variable, it does some operations, and outputs it?
# a code function is the same! it takes some data, does some code on it, and outputs it!
# essentially, we say "Hey! JSON library! I would like to use your dumps() function!"
# and then we give the function what we want to dump: "Here is the thing I want to dump into JSON!"
# then the library says back "Here you go! Here's your JSON! I stored it in the y variable!"
y = json.dumps(x)
print(y) # the result is a JSON string
{"name": "John", "age": 30, "city": "New York"}
Formatting
# the result above isn't easy to read. let's fix that by formatting!
import json
x = {
"name": "John",
"age": 30,
"city": "New York"
}
print("Indent")
y = json.dumps(x, indent=4) # we call the function but say "Please indent it for me!"
print(y + "\n") # the "\n" is a newline seperator and adds a space between each print statement
print("Indent + seperator")
y = json.dumps(x, indent=4, separators=(".", " = ")) # we change the seperator
print(y + "\n")
print("Indent + seperator + sorted")
y = json.dumps(x, indent=4, separators=(".", " = "), sort_keys=True) # now we are sorting them
print(y)
Indent
{
"name": "John",
"age": 30,
"city": "New York"
}
Indent + seperator
{
"name" = "John".
"age" = 30.
"city" = "New York"
}
Indent + seperator + sorted
{
"age" = 30.
"city" = "New York".
"name" = "John"
}
Converting from PYTHON TO JSON
import json
x = '{ "name":"John", "age":30, "city":"New York"}' # some JSON
y = json.loads(x) # parse with loads() function
print(y["age"]) # the result is a Python dictionary
30
Challenge Problem
ingredients = ["butter", "white sugar", "light brown sugar", "vanilla extract", "eggs", "flour", "chocolate chips", "baking soda", "salt", "baking powder"]
# Print this list
# << CODE >>
print("Ingredients:", ingredients)
# Create a list called bowl
# Your list must include:
# "flour"
# "baking soda"
# "salt"
# "baking powder"
# When creating this list, make sure to remove these items from the "ingredients" list!
# Print your list
# << CODE >>
bowl = ["flour", "baking soda", "salt", "baking powder"]
print("Bowl:", bowl)
# list comprehension
ingredients = [ingredient for ingredient in ingredients if ingredient not in bowl]
print("Ingredients:", ingredients)
# Create a list called cream
# Include:
# "butter"
# "white sugar"
# "light brown sugar"
# "vanilla extract"
# "eggs"
# When creating this list, make sure to remove these items from the "ingredients" list!
# Print your list
# << CODE >>
cream = ["butter", "white sugar", "light brown sugar", "vanilla extract", "eggs"]
print("Cream:", cream)
# for loop
for c in cream:
ingredients.remove(c)
print("Ingredients:", ingredients)
# Create a list called "dough"
# Combine the bowl list and cream list together
# Print your list
# << CODE >>
dough = []
for b in bowl:
dough.append(b)
for c in cream:
dough.append(c)
print("Dough:", dough)
# short way
dough2 = bowl + cream
print("Dowgh2", dough2)
# Append chocolate chips to the dough list and remove it from the ingredients list
# Print the list
# << CODE >>
dough += [ingredients.pop()]
print("Complete Dough:", dough)
print("Empty Ingredients:", ingredients)
# Create a string that says "Now roll the dough into balls and place them on cookie sheets!"
# Print it
# << CODE >>
# Create an int called temperature and set it to 375
# Print "Place in a <<your integer goes here>> F oven for 8-10 minutes and remove just before they start to turn brown."
# << CODE >>
# Create an int called "cool down" using pascal case and set it to 2
# Print "Let them sit on the baking pan for <<your integer goes here>> minutes before removing to cooling rack."
# << CODE >>
# Create 5 string (use whatever casing you feel)
# First string should be "Enjoy"
# Second string should be "your"
# Third string should be "CHOCOLATE"
# Fourth string should be "CHIP"
# Fifth string should be "COOKIES!!!"
# Using ONE print statement, print ALL of these variables (with a space between each)
# << CODE >>
# JSON CHALLENGE
# can your do the whole project by using a JSON list?
# can you convert it to a python dictionary and do all these steps?
# Yes this is a real cookie recipe. You can find it below if you wanna make them! :)
# https://joyfoodsunshine.com/the-most-amazing-chocolate-chip-cookies/
Ingredients: ['butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs', 'flour', 'chocolate chips', 'baking soda', 'salt', 'baking powder']
Ingredients: ['butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs', 'chocolate chips']
Cream: ['butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs']
Ingredients: ['chocolate chips']
Dough: ['flour', 'baking soda', 'salt', 'baking powder', 'butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs']
Dowgh2 ['flour', 'baking soda', 'salt', 'baking powder', 'butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs']
Complete Dough: ['flour', 'baking soda', 'salt', 'baking powder', 'butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs', 'chocolate chips']
Empty Ingredients: []
ingredients = ["butter", "white sugar", "light brown sugar", "vanilla extract", "eggs", "flour", "chocolate chips", "baking soda", "salt", "baking powder"]
bowl = ["flour", "baking soda", "salt", "baking powder"]
ingredients = [item for item in ingredients if item not in bowl]
print(ingredients)
['butter', 'white sugar', 'light brown sugar', 'vanilla extract', 'eggs', 'chocolate chips']
Hacks
Review each of the sections above and produce …
- A list of data that could work with your personal project
- A dictionary of data that could work with your personal project
- Try to create a mix of list and dictionaries to represent a real world collection of data.