fruits = ["Apple", "Banana", "Orange"]
What does fruits[1] give us?
"Banana"
names = []
names.append("Angel")
names.append("Emmanuel")
names.remove("Angel")
["Emmanuel"]
Make me a list of anything (with whatever length you want) and print out each item
Example response:
numbers = [1,2,3]
print(numbers[0])
print(numbers[1])
print(numbers[2])
colors = ["Red","Blue","Yellow"]
colors_2 = colors.copy()
print(colors_2)
["Red","Blue","Yellow"]
What does it mean for something to be mutable?
The ability to change the value of something (i.e. changing the value of a list)
fruits = ["Apple", "Banana", "Orange", "Strawberry"]
What does fruits[-3] give us?
"Banana"
names = ["Angel", "Emmanuel"]
names.insert(1, "Lydia")
names.pop(2)
["Angel, "Lydia"]
numbers = [1, 2, 3, 4, 5]
I want the number 5 to be first. Then, I want to remove the number 3. Finally, I want to add the number 6.
Final result: [5, 2, 4, 6]
Example response:
numbers.remove(5)
numbers[0] = 5
numbers.remove(3)
numbers.append(6)
colors = ["Red", "Red", "Red", "Red", "Blue"]
print(colors.count("Red"))
4
Ints, floats, strings, lists, objects, functions, booleans, etc.
fruits = ["Apple", "Banana", "Orange", "Strawberry", "Grape", "Watermelon"]
What does fruits[6] give us?
names = ["Angel", "Emmanuel", "Lydia"]
names.remove("angel")
Error!
numbers = [1, 2, 3, 4, 5, 6]
I want to check if the number 3 is in the list, and if it is then I want to remove it. Then, I want to add the number 7 if the number 2 is in the list
End result: [1, 2, 4, 5, 6, 7]
Example response:
if 3 in numbers:
numbers.remove(3)
if 2 in numbers:
numbers.append(7)
colors = ["Purple", "Red", "Green", "Lavender", "Violet"]
colors.reverse()
print(colors)
["Violet", "Lavender", "Green", "Red", "Purple"]
Why are we able to index into lists?
Lists are ordered
fruits = ["Apple", "Banana", "Orange", "Strawberry", "Grape", "Watermelon"]
What does fruits[:4] give us?
["Apple", "Banana", "Orange", "Strawberry"]
names = []
names.append(["Angel", "Emmanuel"])
names.insert(100, "Lydia")
[["Angel", "Emmanuel], "Lydia"]
numbers = [1, 2, 3, 4, 5]
I want to take user input, and if the user's number is in the list, remove it from the list
Example response:
user_number = int(input("Enter a number: "))
if user_number in numbers:
numbers.remove(user_number)
colors = ["Blue", "Red", "Yellow", "Orange"]
colors.sort()
print(colors)
["Blue", "Orange", "Red", "Yellow"]
How many items can you put in a list?
An infinite amount!
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
What does fruits[-4:-1] give us?
names = ["Angel", "Lydia", "Emmanuel"]
names[0:3] = "Angel"
['A','n','g','e','l']
30 SECONDS!!!
numbers = []
1. Add the numbers 1 through 4 to the list
2. Remove the number 3
3. Print out the number at index 3
4. Change the number in position 0 to 7
5. Reset the list to [1, 2, 3, 4] using appends and removes
This would produce an error! At step 3
colors = ["Pink", "Silver", "Gold"]
colors_2 = ["Scarlet", "Turquoise", "Gray"]
colors.extend(colors_2)
print(colors)
["Pink", "Silver", "Gold", "Scarlet", "Turquoise", "Gray"]
Explain 3 properties of lists
Mutable - can change the value of it
Ordered - can index into it (items are in a set order)
Dynamic - lists grow and shrink as needed
Can store any sorts of data (not code blocks like conditional statements)
Can be infinitely appended to