Variables
- Should be very short and simple
- Not complicated
Popcorn Hack
age = 16
print(age)
favoriteFood = "Pizza"
print(favoriteFood)
16
Pizza
Data Types
- Integer: a mathematical number
- boolean: a yes/no or true/false statement
Examples
highScore = 11
isSunny = True
firstName = "Tucker"
print(str(highScore) + " is an integer")
print(str(isSunny) + " is a boolean")
print(firstName + " is a string")
11 is an integer
True is a boolean
Tucker is a string
Changing Values
A variable itself isn’t static, so you can change it to give different or more updated information. An example of this is having a variable that calculates changing age.
The following example shows how you can replace one variable with another. See if you can figure out what data will be printed before it runs, TUCKER.
currentScore = 10
highScore = currentScore
currentScore = 7
print(str(highScore))
10
num1 = 25
num2 = 15
num3 = 30
num2 = num3
num3 = num1
num1 = num2
print(str(num1))
print(str(num2))
print(str(num3))
30
30
25
3.2 Data Abstraction
New Types of Variables
- strings
- an ordered sequence of characters
- may contain letters, numbers, and all other special characters
- ex: words, phrases, sentences, ID numbers
- lists (?)
- an ordered sequence of elements
- ex: students in a class, songs in a playlist, etc…
groupNames = ["Tucker", "Taj", "Eashaan", "William"]
print(groupNames[0])
print(groupNames[1])
print(groupNames[2])
print(groupNames[3])
# it's in order (the integers) in which they will print
Tucker
Taj
Eashaan
William
Popcorn Hack
Try to create a list of your current classes and PRINT ONLY THE SECOND CLASS.
# these are listed in order for integers (0, 1, 2, 3, 4), so whatever value (number in order) you say to print will print the corresponding in the list
currentClasses = ["AP Stats", "USH", "AFA", "AP CSP", "AM LIT 1"]
print(currentClasses[1])
USH
Data Abstraction: Lists
- lists allow for data abstraction
- bundle variables together
- strings, numbers, characters, etc…
- give one name to a set of memory cells
- do not need to know how many variables will be needed
- do not need to know how the elements are stored together
- bundle variables together
Examples
scores1 = [89, 12, 45, 91, 20] # these scores are just random numbers
scores2 = [23, 25, 96, 55]
scores1 = scores2 # <- prints scores 2 INSTEAD
print(str(scores1))
[23, 25, 96, 55]
How Lists Manage the Complexity of a Program
- May not need as many variables (improves readability)
- Ex: variable for each student is more tedious than one variable that holds all students
- Change the number of variables
- Ex: if a student transfers in/out of school, you won’t need to add/delete an entire variable (you’re just deleting one element from a list)
Homework: Hack 1
Sort the variables in the code cell below into these categories and write the adjoining numbers for each type:
- Integer: Variable 1
- List: Variable 3
- Boolean: Variable 4
- String: Variable 2
# Variable 1: this is the INTEGER
numStudents = 26
print(numStudents)
# Variable 2: this is the STRING
car = "Tesla"
print(car)
# Variable 3: this is the LIST
groupNames = ["Tucker", "Taj", "Eashaan", "William"]
print(groupNames)
# Variable 4: this is the BOOLEAN
dogsbeatcats = True
print(dogsbeatcats)
26
Tesla
['Tucker', 'Taj', 'Eashaan', 'William']
True
Homework: Hack 2
Define one variable for each type above. Make them surrounding personal unique interest. For the list, convert it to a string using JSON and print it out.
age = 16
print(age)
favoriteFood = "Tacos"
print(favoriteFood)
favoriteActivities = ["Hiking", "Video Games", "Exercising", "Family", "Friends"]
print(favoriteActivities)
livesOnEarth = True
print(livesOnEarth)
16
Tacos
['Hiking', 'Video Games', 'Exercising', 'Family', 'Friends']
True
import json
favoriteActivities = ["Hiking", "Video Games", "Exercising", "Family", "Friends"]
print(type(favoriteActivities))
a = json.dumps(favoriteActivities)
print(a)
print(type(a))
<class 'list'>
["Hiking", "Video Games", "Exercising", "Family", "Friends"]
<class 'str'>
import json
lst = [1,2,3,4]
print(type(lst))
a = json.dumps(lst)
print(a)
print(type(a))
<class 'list'>
[1, 2, 3, 4]
<class 'str'>