Unit 3.8 and 3.10 Notes
- What are lists?
- Don't do this!
- Iteration
- Using the range function
- Printing a 2d Array
- More functions
- HW
- Challenge Attempt
What are lists?
- A sequence of variables
- used to store multiple items
- collection of data # Lists
- one of 4 collection data types
- others: Tuple (ordered, unchangeable, can be duped), Set(unordered, changeable, can't be duped), and Dictionary (ordered, changeable, can't be duped)
List examples in Python, JavaScript, and Pseudocode.
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])
sports = ["football", "soccer", "baseball", "basketball"]
# change the value "soccer" to "hockey"
sports.remove("soccer")
sports.insert(1, "hockey")
print (sports)
sports = ["football", "soccer", "baseball", "basketball"]
# add "golf" as the 3rd element in the list
sports.insert(2, "golf")
print (sports)
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
Iteration
- Iteration is the repetition of a process or utterance applied to the result or taken from a previous statement.
- Some ways to do iteration is for and while loops and range
- Lists, tuples, dictionaries, and sets are iterable objects. They are the 'containers' that store the data to iterate.
- 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 ## Iterator? Iterable? Iteration?
- When an object is iterable it can be used in an iteration
- When passed through the function iter() it returns an iterator
- Strings, lists, dictionaries, sets and tuples are all examples of iterable objects.
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)
keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [" ", 0, " "]]
# Better example matrix
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
print("Raw matrix (list of lists): ")
print(keypad)
print("Matrix printed using nested for loop iteration:")
print_matrix1(keypad)
print()
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
def print_matrix3(matrix):
for row in matrix:
for col in row:
print(col, end = " ")
print()
print_matrix3(keypad)
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix4(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j], end=" ")
print()
print_matrix4(keypad)
Find a way to print the matrix using the iter() function you already learned. Or use both!
keypad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[" ", 0, " "]]
def print_matrix3(matrix):
for row in matrix:
# for col in row:
print(*row, end=" ")
print()
print_matrix3(keypad)
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", ",", ".", "/"]]
dob = "APRIL20,2022"
for character in dob:
for row in keyboard:
for k in row:
if str(k) == character:
print(k, end = "")
break
print("\nMy age: ")
print(keyboard[0][1] + keyboard[0][9] + keyboard[0][6])
Use the list below to turn the first letter of any word (using input()) into its respective NATO phonetic alphabet word
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"]
inp = input().lower()
output = " "
for letter in inp:
for word in words:
if letter == word[0]:
output += word + " "
print(output)
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", ",", ".", "/"]]
dob = "APRIL20,2022"
for character in dob:
for row in keyboard:
for k in row:
if str(k) == character:
print(dob , end = " ")
print()
# didn't work