Section 3.1 ~ 3.2
- Vocab
- Key Words
- Notes
- First Hack
- Second Hack
- This is the code of Second Hack
- Third Hack
- Good things to use List
x = "James"
x += " Lee"
print(x)
- Integer : number
- string : letter
- Boolean : True or False
num = int(2)
strin = str("Hello")
Boo = True
if Boo:
print(num)
print(strin)
First Hack
- What is the assignment operator? The assignment operator is the operator that can make the value change. It used to put the new value in the variable. For example *= or += or -=
- In Collegeboard pseudocode, what symbol is used to assign values to variables? In the collegeboard, teacher use this <--- to put the value in variables.
- A variable, x, is initially given a value of 15. Later on, the value for x is changed to 22. If you print x, would the command display 15 or 22? x value will be printed as 22. If the programmer put print before changing the output will be 15.
Second Hack
<html>
<body>
<div class="container">
<div class="calendar">
<div class="month">
<button id="prev" onclick="prev()">Prev</button>
<button id="next" onclick="next()">Next</button>
<p id="month">Month Here</p>
</div>
</div>
</div>
<script>
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let index = 0;
function next() {
if (index > 11) {
index = 0;
}
else {
index += 1;
}
document.getElementById("month").innerHTML = months[index]
}
function prev() {
if (index < 0) {
index = 11;
}
else {
index -= 1;
}
document.getElementById("month").innerHTML = months[index]
}
</script>
</body>
</html>
Third Hack
- what is the list? List = group of several variables, variable in the list can also be the list.
- what is the elements? Elements is the values that make up the lists
- What is an easy way to reference the elements in a list or string? put the list name and put [index] in the next of list name. Then use print command like this (print(Listname[index])). Make sure that index starts with 0 not 1.
- What is an example of a string? Example of string is the letter. ex) "hello world", "Hi my name is"
a = ["chicken","pizza","chocolate","icecream","cola"]
for i in range(len(a)):
print(a[i])
a = ["chicken","pizza","chocolate","icecream","cola"]
print(a[2])
print(a[-1])
num1 = int(input("Input a number. "))
num2=int(input("Input a number. "))
num3=int(input("Input a number. "))
add=input("How much would you like to add? ")
numlist = []
# Add code in the space below
numlist.append(num1)
numlist.append(num2)
numlist.append(num3)
# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.
for i in range(len(numlist)):
numlist[i] += int(add)
print(numlist)
import getpass, sys
def question_with_response(prompt):
print("Question: " + prompt)
msg = input()
return msg
questions = 4
correct = 0
print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_with_response("Are you ready to take a test?")
rsp = question_with_response("The purpose of lists and dictionaries are to manage the ____ of a program")
if rsp == "complexity":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("Lists are a form of data ______")
if rsp == "abstraction":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("Which brackets are used to assign values to a variable to make a list?")
if rsp == "[]":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Good things to use List
A List is a data structure that allows you to efficiently manage a large number of data by grouping them. The main characteristic of list is that they have indexes. If you know the index, you can use the index to fetch data. Data lookups using indexes are processed very quickly.
Mylist = ["dog", "cat", "frog", "bird", "fish"]
print(Mylist)
print(Mylist[1])