num
if
even and greater than 20
Intro to Data Structures
Example:
fruits = ["apple", "banana", "cherry"]
student1 = "Alice" student2 = "Bob" student3 = "Charlie" # vs. students = ["Alice", "Bob", "Charlie"]
[]
numbers = [1, 2, 3, 4, 5] mixed = ["cat", 3.14, True]
Question: Can a list hold different data types?
colors = ["red", "green", "blue"] print(colors[0]) # red print(colors[2]) # blue
Q: What will print(colors[1]) output?
print(colors[1])
pets = ["dog", "cat", "fish"] pets[1] = "hamster" print(pets) # ["dog", "hamster", "fish"]
.append()
numbers = [1, 2, 3] numbers.append(4) print(numbers) # [1, 2, 3, 4]
Q: What will happen if we run numbers.append(99)?
numbers.append(99)
.remove(value)
.pop(index)
fruits = ["apple", "banana", "cherry"] fruits.remove("banana") print(fruits) # ["apple", "cherry"] fruits.pop(0) print(fruits) # ["cherry"]