Unit 3.1 and 3.2 Notes
- What is a variable?
- Types of data
- Assignments:
- Types of Assignments:
- Changing Values:
- Data Abstraction Practice:
- Homework
What is a variable?
- Has 3 parts: Name, value and type
- An data abstraction that can hold a value
- Make variable specific enough to understand
Types of data
- Integer: A number
- Text/String: A word
- Boolean: Data that can be true or false
Examples:
String: name = "table1" print(name, type(name))
Integer: number = 4 print(number, type(number))
Boolean: isAbsent = False print(isAbsent, type(isAbsent))
Assignments:
- Enable value of a variable to be changed
Types of Assignments:
- = : assigns value on right side to lest side
- += : adds right side with left side and assigns that value to the left side
- -= : subtracts right side with left and then assigns that value to the left
- *= : multiplies right side with left and then assigns that value to the left
- /= : divides right side with left and then assigns that value to the left
- **= : Calculates exponent value using operands and assign value to left side
Changing Values:
1st Problem Prediction: num1 = 9 num2 = 9
2/2!
2nd Problem Prediction: num1 = 42 num2 = 42 num3 = 15
3/3!
3rd Problem Prediction: print(num2)
1/1!
Data Abstraction Practice:
colorsList = ["green", "red", "pink", "purple", "blue", "brown"] # creating a index for each thing in the list
for i in colorsList: # recalling the indexes
print(i) # prints them
class Question:
def __init__(self, prompt, answer): # Creating own data type
self.prompt = prompt
self.answer = answer
quesList = [ # list with the question and MC answers
"Question 1.) Who is Roanldo playing for in the world cup?\n(a) Portugal\n(b) Argentina\n",
"Question 2.) What is 2+6\n(a) 8\n(b) 26\n",
"Question 3.) What is the color of the sky\n(a) green\n(b) blue\n",
"Question 4.) Who teaches this class?\n(a) Mr.Mortensen\n(b) Mr.Yeung\n"
]
questions = [ # defining the correct answers and the question associated with that answer using the class from above
Question(quesList[0], "a"),
Question(quesList[1], "a"),
Question(quesList[2], "b"),
Question(quesList[3], "a"),
]
def quiz(questions): # creating a function
score = 0 # setting base score
for question in questions:
print(question.prompt) # prints prompt with the mc
answer = input(question.prompt) # input for the answer
print(answer +"\n")
if answer == question.answer:
score += 1 # adds one to the score if the answer was right, does nothing if not
print("you got", score, "out of", len(questions)) # prints the number of questions correct over the number of questions
quiz(questions)