Unit Notes and Homework (Day 4)
Lists and Iteration Lesson
fruits = ["apple", "grape", "strawberry"]
print (fruits)
const fruits = ["apple", "grape", "strawberry"];
fruits ← [apple, grape, strawberry]
fruits = ["apple", "grape", "strawberry"]
index = 1
print (fruits[index])
Methods in Lists
Method | Definition | Example |
---|---|---|
append() | adds element to the end of the list | fruits.append("watermelon") |
index() | returns the index of the first element with the specified value | fruits.index("apple") |
insert() | adds element at given position | fruits.insert(1, "watermelon") |
remove() | removes the first item with the specified value | fruits.remove("strawberry") |
reverse() | reverses the list order | fruits.reverse() |
sort() | sorts the list | fruits.sort() |
count() | returns the amount of elements with the specified value | fruits.count("apple") |
copy() | returns a copy of the list | fruits.copy() |
clear() | removes the elements from the list | fruits.clear() |
sports = ["football", "soccer", "baseball", "basketball"]
index = 0
# change the value "soccer" to "hockey"
sports.remove(sports[index])
sports.insert(0, "hockey")
print(sports)
sports = ["football", "soccer", "baseball", "basketball"]
# add "golf" as the 3rd element in the list
sports.insert(2, "golf")
print(sports)
Iteration
Iteration is the repetition of a process or utterance applied to the result or taken from a previous statement. There's a lot of types of iteration though, what to use? How do we apply iteration to lists?
Some methods include using a "for loop", using a "for loop and range()", using a "while loop", and using comprehension
Lists, tuples, dictionaries, and sets are iterable objects. They are the 'containers' that store the data to iterate.
Each of these containers are 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
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.
Loops
Well, above is basically just printing them again, so how do we takes these iterators into something we can make use for?
Loops take essentially what we did above and automates it, here are some examples.
Using the range() function
But wait, there's more
Need to save even more time? The above is useful for many occasions, but can get tedious fast, in this case, use range()
Else, elif, and break
For when 1 statement isn't enough
Else:when the condition does not meet, do statement()- Elif: when the condition does not meet, but meets another condition, do statement()> Break:stop the loop
2D Iteration
2D Arrays
A 2D array is simply just a list of lists. The example below is technically correct but... Conventially 2D arrays are written like below. This is because 2D arrays are meant to be read in 2 dimensions (hence the name). Writing them like below makes them easier to visualize and understand.
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"]
index = 0
inp = input().lower()
def show_letters(inp):
for letter in inp:
for let in words:
if letter == let[index]:
print(let)
show_letters(inp)
# btw the input is batman
keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [" ", 0, " "]]
def print_matrix3(matrix):
for a in matrix:
itr = iter(a)
print(next(itr), next(itr), next(itr))
print_matrix3(keypad)
Alternatively, find a way to print the matrix using the iter() function you already learned. Or use both!
letters = [["`", 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", ",", ".", "/"]]
letters_lower1 = [letter.lower() for letter in letters[1]] # additional lines of code for better capitalization
letters_lower2 = [letter.lower() for letter in letters[2]]
letters_lower3 = [letter.lower() for letter in letters[3]]
print(letters[3][6] + letters_lower2[0] + letters_lower3[5] + letters_lower1[7])
print(letters[3][6] + letters_lower2[0] + letters_lower1[5])
print(letters[0][9] + letters[0][9])