Week 1-2,3-4 notes + hacks
- 3.1 and 3.2 Variables, Assignments, and Data Abstractions
- Variables
- Types of data
- Lists
- Assignments
- Data Abstraction
- Lists and Strings
- Managing Complexity in a Program
- Ap Exam Use
- 3.3 and 3.4 Mathematical Expressions and Strings
- Algorithm Steps
- Arithmetic Operation
- Variables
- Sequencing is Important
- Strings
- Hacks/Hw
- Num1 = 50
- Num2 = Num1 % 9 + 15
- Num3 = Num2 / Num1 + ( Num2 * 2 )
- Num4 = Num3 + Num1 / 5 - 10
- Result = Num4 - Num2
- Result = 20.4 ^^
- Num1 = 10
- Num2 = Num1 % 3 * 4
- Num1 = Num2
- Num3 = Num1 * 3
- Result = Num3 % 2
- Result = 0 ^^
- valueA = 4
- valueB = 90
- valueC = 17
- valueB = valueC - valueA
- valueA = valueA * 10
- if valueB > 10:
- print(valueC)
- Result = 17 ^^
- type = "curly"
- color = "brown"
- length = "short"
- type = "straight"
- hair = type + color + length
- print(hair)
- Result = "straightbrownshort" ^^
- Unit 3 Sections 5-7
3.1 and 3.2 Variables, Assignments, and Data Abstractions
Variables
- A variable is an abstraction in a program that can hold a value
- Has three parts, name, value and type
- Meaningful variables helps readability and understanding the code
Types of data
- Integer: A number
- String: A word
- Boolean: True or False
Lists
- Print multiple values without creating many variables
- easily add and change values in the list
Assignments
- The assignment operator allows a program to change the value represented by a variable
- Used to assign values to a variable
- Value that is stored in the variable is the one most recently assigned to it
Data Abstraction
- Method in programing to represent data in a useful form, taking aspects of data that arent being used
- Variables and lists are important for data abstraction
Lists and Strings
- Element = individual value in the list
- Index = a way to referance elements in a list
Managing Complexity in a Program
- Data abstractions help by giving the collection a name without refrencing specific details
Ap Exam Use
- Index questions do not start at 0, but 1
colorList=["green", "red", "pink", "purple", "blue", "brown"]
for i in range(len(colorList)):
print(colorList[i])
def sort(testScores):
# variable assigned to the length of the test score list
length=len(testScores)
# loops through the length of the test score list amount of times
for i in range(length):
for j in range((i+1),length):
# checks in order of the list if the next value is greater than it or not, and swaps places if its less than it
if testScores[i]>testScores[j]:
l1=testScores[i]
testScores[i]= testScores[j]
testScores[j]=l1
# once the whole list checks out, it outputs the score as a list
print(testScores)
# list of scores
testScoresList=[78,94,72,86,65,91,92,88,78,55]
# function being called
sort(testScoresList)
3.3 and 3.4 Mathematical Expressions and Strings
Algorithm Steps
- Sequencing
- Selection
-
Iteration
-
Can be represented in two ways: Flow Charts, Pseudocode
Arithmetic Operation
- for addition+, * multiplication, / division and - subtraction
- Operations in parentheses should be done first
- Division and multiplication should be done before addition and subtraction
- Modules work similar to multiplication
Variables
- Numerical value stored in a variable
Sequencing is Important
- Assigning a variable will overide the last value which was assigned to the same variable.
Strings
- A string is a collection of characters. It could be numbers and symbols, but it is still a string
- len() finds the length of lists and strings
- lower() to convert to lowercase
- concat() returns a string made up on the concatenated strings
Hacks/Hw
Num1 = 50
Num2 = Num1 % 9 + 15
Num3 = Num2 / Num1 + ( Num2 * 2 )
Num4 = Num3 + Num1 / 5 - 10
Result = Num4 - Num2
Result = 20.4 ^^
Num1 = 10
Num2 = Num1 % 3 * 4
Num1 = Num2
Num3 = Num1 * 3
Result = Num3 % 2
Result = 0 ^^
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA
valueA = valueA * 10
if valueB > 10:
print(valueC)
Result = 17 ^^
type = "curly"
color = "brown"
length = "short"
type = "straight"
hair = type + color + length
print(hair)
Result = "straightbrownshort" ^^
Noun = "Mr.Mortenson"
Adjective = "handsome"
Adjective2 = "Very"
Verb = "is"
abrev = Noun[:7]
yoda = (Adjective2+ " "+ Adjective+ " "+ abrev+ " "+Verb+ ".")
print(yoda)
# Result is "Very handsome Mr.Mort is."
cookie = "choclate"
cookie2 = "rasin"
len1 = len(cookie) / 2
len2 = len(cookie2) * 45
vote1 = (cookie, "vote", len2)
vote2 = (cookie2, "vote", len1)
votes = (vote1, " ", vote2)
print(votes)
# Result (('choclate', 'vote', 225), ' ', ('rasin', 'vote', 4.0))