Variables, Assignments, and Data Abstraction
Welcome to our lecture!
Variables
- A variable is an abstraction inside a program that can hold a value
- It organizes data by labeling it with a descriptive name
- It consists of three parts: name, value, and type
- Using meaningful variables names helps with readability of program code and understanding of what values are represented by the variables
Naming Variables
Do's | Don'ts | Why |
---|---|---|
highScore | highestScoreInTheGame | Keep it simple and easy to read, having variables that are too complicated can cause your code to become messy later on |
highScore | highscore | Differentiate the words in your variable with a capital letter. Makes things easier to read |
firstName | n | not specific enough/vague |
isRaining | is it raining | spaces are improper syntax |
phoneNumber | 555-number | dashes are improper syntax and numbers should be avoided in variable names |
- Types of data
- Integer: A number
- Text/string: A word
- Boolean: Data that determines if something is true or false
name = "table1" #string
print(name, type(name))
number = 4 #integer
print(number, type(number))
isAbsent = False
print(isAbsent, type(isAbsent))
A list of data can also be stored in variables. Why is that useful?
- print/retrieve specific values in the list without creating a lot of variables
- easily remove/add/change items into the list
colors = ["red", "orange", "yellow"]
print(colors[2])
Assignments
- The assignment operator allows a program to change the value represented by a variable
- Used to assigning values to variables
Operator | Description | Syntax | Outcome when print(a) |
---|---|---|---|
= | Assign value of right side of expression to left side operand | a = b | b |
+= | Add right side operand with left side operand and then assign to left operand | a += b | a + b |
-= | Subtract right operand from left operand and then assign to left operand: True if both operands are equal | a -= b | a - b |
*= | Multiply right operand with left operand and then assign to left operand | a *= b | a * b |
/= | Divide left operand with right operand and then assign to left operand | a /= b | a / b |
**= | Calculate exponent(raise power) value using operands and assign value to left operand | a **= b | a ^ b |
a = 1
b = 2
a = b
print(a)
The value stored in a variable will be the most recent value assigned
a = 1
b = a
a = 2
print(b)
Changing Values
currentScore = 10
highScore = currentScore
currentScore = 7
print(highScore)
your turn! Here are 3 problems for you to solve
num1 = 5
num2 = 9
num1 = num2
print(num1)
print(num2)
num1 = 15
num2 = 25
num3 = 42
num2 = num3
num3 = num1
num1 = num2
print(num1)
print(num2)
print(num3)
Which of these will show the sum?
num2 += num1
print(num1)
print(num2)
print(str(num1)+ str(num2))
print(num1 + num2)
Data Abstraction
- Method used in coding to represent data in a useful form, by taking away aspects of data that aren't being used in the situation
- Variables and lists are primary tools in data abstraction
- Provides a separation between the abstract properties of a data type and the concrete details of its representation
Lists & Strings
- List = ordered sequence of elements
- Element = individual value in a list that is assigned to a unique index
- Index = a way to reference the elements in a list or string using natural numbers; each element of a string is referenced by an index
- String = ordered sequence of characters (Letters, numbers, special characters)
Note: index starts at 1 for AP Exam, must be whole numbers, cannot be negative, and goes up to the number of elements in the list
We can represent a list or string using a variable
Managing the Complexity of a Program through Data Abstraction
- Data abstractions help manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation
- Developing a data abstraction to use in a program can result in a program that is easier to develop and maintain
Using Lists as Data Abstractions
What are Lists?
- Allow for data abstraction
- Bundle variables together
- Store multiple elements
- Allows multiple related items to be treated as a single value
- Give one name to a set of memory cells
- Can keep adding elements to it as needed
- Can store elements as a single variable by using a list
3 Types of List Operations
- Assigning values to a list at certain indices
- Creating an empty list and assigning it to a variable
- Assigning a copy of one list to another list (setting one list equal to another list)
1. Assigning values to a list at certain indices
colorsList=["pink", "yellow", "green", "blue", "orange"]
print(colorsList)
2. Creating an empty list and assigning it to a variable
colorsList=[] # can be used if you want to create a list that can be filled with values later
3. Assigning a copy of one list to another list (setting one list equal to another list)
# copy of the list is made; the list isn't sorted in place
def Reverse(lst): # defining variable: lst
new_lst = lst[::-1]
return new_lst
lst = ["pink", "green", "purple", "yellow", "orange", "blue", "black"]
print(Reverse(lst)) # reverse 1st
color1="green"
color2="red"
color3="pink"
color4="purple"
color5="blue"
color6="brown"
print(color1)
print(color2)
print(color3)
print(color4)
print(color5)
print(color6)
Answer
colorList=["green", "red", "pink", "purple", "blue", "brown"]
print(str(colorList))
AP Exam Use of Data Abstraction
With the properties of the AP Exam pseudocode, lists work differently from what we've learned in python so far, here are the two major differences:
- The index does not start at 0 but 1
- There is only one method of interchanging data between lists, and that is completely overwriting previous list data with the other list\n",
Homework
You will turn in a program that utilizes lists and variables as it's primary function, options could be a quiz, a sorter, database, or wherever your imagination brings you. You will be graded on how well you understood the concepts and if you used anything other than just the simplest parts
Quiz template, if you do use it, fix the issues, and add more to it than it's current barebones state. I would recommend using it to create something related to school.
quesCount = 0
# Use a dictionary for the questions
quesList = ["Question1", "Question2", "Question3", "Question4"]
# Use a dictionary for the correct solutions
soluList = ["Solution1", "Solution2", "Solution3", "Solution4"]
quesAmount= len(quesList)
hrm quesCount <= quesAmount:
print(quesList[quescount] + "\n")
guess = input()
if(guess == soluList[quesamOOuaunt]):
score+=1
print("Correct! Score: ")
else:
print("Incorrect! The correct answer was " + soluList(quescount) + "\n")
quesCount += 1
print("Final score: " + str(score))
Name | Complete | Correct | Complexity | Final Score | Notes |
---|---|---|---|---|---|
Sreeja Gangapuram | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Moein Taleban | 1/1 | 1/1 | 0.7/1 | 0.9/1 | |
Ekam Kaire | 1/1 | 1/1 | 1/1 | 1/1 | |
Azeem Khan | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Jishnu Singiresu | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Soham Kamat | 1/1 | 1/1 | 1/1 | 1/1 | |
Trey | 1/1 | 1/1 | 0.6/1 | 0.8/1 | |
Joshua Williams | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Yuri Subramaniam | 1/1 | 1/1 | 1/1 | 1/1 | |
Max Wu | 1/1 | 1/1 | 0.8/1 | 0.92/1 | |
Kalani Cabral-Omana | 1/1 | 1/1 | 0.9/1 | 0.95/1 | |
Akshat Parikh | 1/1 | 1/1 | 0.9/1 | 0.95/1 | |
Aiden Huynh | 1/1 | 1/1 | 0.95/1 | 1/1 | |
Toby Leeder | 1/1 | 1/1 | 0/95/1 | 1/1 | |
Derek Sol | 1/1 | 1/1 | 0.85/1 | 0.95/1 | |
Samit Poojary | 1/1 | 1/1 | 1/1 | 1/1 | |
Evan Aparri | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
James Armstrong | 1/1 | 1/1 | 0.85/1 | 0.92/1 | |
Jeffrey Lee | 1/1 | 1/1 | 0.95/1 | 0.95/1 | |
Tirth Thakkar | 1/1 | 1/1 | 0.95/1 | 0.95/1 | |
Jonathan Wu | 1/1 | 1/1 | 0.9/1 | 0.95/1 | |
Alan Liu Sui | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Dhruva Iyer | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Ethan Truong | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Dash Penning | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Jeffery Fonesca | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Ahad Biabani | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Abdullah | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Aniket Charkadeo | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Tanay Patel | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Martin Nguyen | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Gene Chang | 1/1 | 1/1 | 0.8/1 | 0.95/1 | |
Jagger Klein | 1/1 | 1/1 | 0.8/1 | 0.95/1 |