Lesson 3.8-3.10 Notes and Homework
This is the jupyter notebook for lessons 3.8-3.10.
fruits = ["apple", "grape", "strawberry"]
print (fruits)
const fruits = ["apple", "grape", "strawberry"];
fruits ← [apple, grape, strawberry]
brands = ["nike", "adidas", "underarmour"] #string
numbers = [1, 2, 3, 4, 5] #integer
truefalse = [True, False, True] #boolean
fruits = ["apple", "grape", "strawberry"]
index = 1
print (fruits[index])
Methods in Lists
- append(): adds element to the end of the list
- index(): returns the index of the first element with the specified value
- insert(): adds element at given position
- remove(): removes the first item with the specified value
- reverse(): reverses the list order
- sort(): sorts the list
- count(): returns the amount of elements with the specified value
- copy(): returns a copy of the list
- clear(): removes the elements from the list
sports = ["football", "hockey", "baseball", "basketball"]
# change the value "soccer" to "hockey"
print (sports)
sports = ["football", "soccer", "golf", "baseball", "basketball"]
# add "golf" as the 3rd element in the list
print (sports)
The code below is not good because there is too much code. It is not very efficient.
print("alpha")
print("bravo")
print("charlie")
print("delta")
print("echo")
print("foxtrot")
print("golf")
print("hotel")
print("india")
print("juliett")
print("kilo")
print("lima")
print("mike")
print("november")
print("oscar")
print("papa")
print("quebec")
print("romeo")
print("sierra")
print("tango")
print("uniform")
print("victor")
print("whiskey")
print("x-ray")
print("yankee")
print("zulu")
#please help me
Coding all of these individually takes a lot of unnecessary time, how can we shorten this time?
- Iteration: repetition of a process applied to the result or taken from a previous statement.
- Many types of iteration
- Some are for loops, while loops, and "for loop and range()", etc.
-
Lists, tuples, dictionaries, and sets are iterable
-
Able to iterate with the iter() command.
There are 2 types of iteration:definite and indefinite. Definite iteration clarifies how many times the loop is going to run, while indefinite specifies a condition that must be met
for variable in iterable:
statement()
a = ['alpha', 'bravo', 'charlie']
itr = iter(a)
print(next(itr))
print(next(itr))
print(next(itr))
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]
# using a for loop
for i in list:
#for item in the list, print the item
print(i)
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]
# Taking the length of the list
lengthList = len(list)
# Iteration using the amount of items in the list
for i in range(lengthList):
print(list[i])
list = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu"]
# Once again, taking the length of the list
lengthList = len(list)
# Setting the variable we are going to use as 0
i=0
# Iteration using the while loop
# Argument saying WHILE a certain variable is a certain condition, the code should run
while i < lengthList:
print(list[i])
i += 1
x = range(5)
for n in x:
print(n)
words = ["alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliett", "kilo",
"lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "xray", "yankee", "zulu"]
i=0
while i<4:
inp = input().lower()
output = " "
for j in inp:
for u in words:
if j == u[0]:
output += u + " "
print(inp)
print(output)
i += 1
keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [" ", 0, " "]]
It is usually formatted in the way below because a 2D array is meant to be 2-dimensional:
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix1(matrix):
for i in range(len(matrix)): # outer for loop. This runs on i which represents the row. range(len(matrix)) is in order to iterate through the length of the matrix
for j in range(len(matrix[i])): # inner for loop. This runs on the length of the i'th row in the matrix (j changes for each row with a different length)
print(matrix[i][j], end=" ") # [i][j] is the 2D location of that value in the matrix, kinda like a coordinate pair. [i] iterates to the specific row and [j] iterates to the specific value in the row. end=" " changes the end value to space, not a new line.
print() # prints extra line. this is in the outer loop, not the inner loop, because it only wants to print a new line for each row
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
print("Raw matrix (list of lists): ")
print(keypad)
print("Matrix printed using nested for loop iteration:")
print_matrix1(keypad)
print()
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix2(matrix):
for row in matrix: # Iterates through each "row" of matrix. Row is a dummy variable, it could technically be anything. It iterates through each value of matrix and each value is it's own list. in this syntax the list is stored in "row".
for col in row: # Iterates through each value in row. Again col, column, is a dummy variable. Each value in row is stored in col.
print(col, end=" ") # Same as 1
print() # Same as 1
print_matrix2(keypad)
fruit = ["apples", "bananas", "grapes"]
print(fruit)
print(*fruit) # Python built in function: "*". Figure out what it does
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix3(matrix):
for i in matrix:
print(*i)
print_matrix3(keypad)
Alternatively, find a way to print the matrix using the iter() function you already learned. Or use both!
def print_matrix4(matrix):
code = "your code goes here"
keyboard = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
Print what month you were born and how old you are by iterating through the keyboard (don't just write a string).
keyboard = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
def printMonthAge(matrix):
j=0
q = matrix[0]
w = matrix[1]
e = matrix[2]
r = matrix[3]
t = e[6] + w[6] + r[5] + w[2]
y = str(q[1]) + str(q[5])
while j==0:
print("Month born: ", t)
print("Age: ", y)
j += 1
printMonthAge(keyboard)
Challenge
Change all of the letters that you DIDN'T print above to spaces, " ", and then print the full keyboard. (the things you did print should remain in the same spot)
Alternative Challenge: If you would prefer, animate it using some form of delay so it flashes one of your letters at a time on the board in order and repeats. (this one may be slightly more intuitive)
DO NOT HARD CODE THIS. Don't make it harder on yourself, iterate through, make it abstract so it can be used dynamically. You should be able to input any string and your code should work.
Note: This is my work for the code. I don't think it works because the code is not displaying anything. At this point, I am stuck and I don't know what to do now.
keyboard = [["`", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "-", "="],
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
def printMonthAge(matrix):
q = matrix[0]
w = matrix[1]
e = matrix[2]
r = matrix[3]
p = 0
j = 0
g = 0
f = 0
testV = 0
# for i in matrix:
while j < 13:
if q[j] != q[1]:
q[j] = " "
else:
testV +=1
j += 1
# p += 1
while g < 12:
if w[f] != w[1]:
w[f] = " "
else:
testV +=1
g += 1
# f += 1
while p < 11:
if e[p] != e[1]:
e[p] = " "
else:
testV +=1
p += 1
# o += 1
while f < 10:
if r[f] != r[1]:
r[f] = " "
else:
testV +=1
f += 1
# a += 1
# print(*i)
iter(matrix)
printMonthAge(keyboard)
If you get stuck you can just make a picture with an array and print it (I will grade based on how good it looks)
I do expect an attempt so write some code to show you tried the challenge.
keyboard = [[" ", 1, " ", " ", " ", 5, " ", " ", " ", " ", " ", " ", " "],
[" ", " ", "E", " ", " ", " ", "U", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", "J", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", "N", " ", " ", " ", " "]]
def printMonthAgeKeyboard(matrix):
for i in matrix:
print(*i)
printMonthAgeKeyboard(keyboard)