# Create a list
my_list = [1, 2, 3, 4, 5]
print(my_list)
[1, 2, 3, 4, 5]
# Access elements by index
my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3
1 3
# find the index of a specific value
my_list = [1, 2, 3, 4, 5]
ind = my_list.index(2)
print(ind)
1
# slice a list to get value within a certain range of indices
my_list = [1, 2, 3, 4, 5]
part_of_my_list = my_list[2:4]
print(part_of_my_list)
[3, 4]
# chagne the value of a list
my_list = [1,2,4,4,5]
my_list[2] = 3
print(my_list)
[1, 2, 3, 4, 5]
# Add elements to the end
my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
# Insert element at a specific index
my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 7)
print(my_list) # Output: [1, 2, 7, 3, 4, 5, 6]
[1, 2, 7, 3, 4, 5]
# Remove element by value
my_list = [1, 2, 3, 4, 5]
my_list.remove(5)
print(my_list) # Output: [1, 2, 4, 5]
[1, 2, 3, 4]
# Remove specific element by value
my_list = ["apple", "banana", "cherry"]
my_list.remove("banana")
print(my_list)
['apple', 'cherry']
# remove specific element by index
my_list = ["apple", "banana", "cherry"]
my_list.pop(1)
print(my_list)
['apple', 'cherry']
# remove the last element
my_list = ["apple", "banana", "cherry"]
my_list.pop()
print(my_list)
['apple', 'banana']
# use del to remote element
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
['banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
del thislist
print(thislist)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-19-4b9c960a30eb> in <cell line: 3>() 1 thislist = ["apple", "banana", "cherry"] 2 del thislist ----> 3 print(thislist) NameError: name 'thislist' is not defined
my_list = ["cherry", 100, True]
print(my_list)
['cherry', 100, True]
my_list = [1, 2, ['apple','cherry'], 6, 7]
print(my_list)
# Access the inner list
inner_list = my_list[2]
print(inner_list) # Output: ['apple', 'cherry']
[1, 2, ['apple', 'cherry'], 6, 7] ['apple', 'cherry']
my_list = ["apple", "banana", "cherry"]
# Get the length of the list
print(len(my_list)) # Output: 3
3
my_list = [15, 5, 6, 4, 5]
my_list.sort(reverse=True)
print(my_list)
[15, 6, 5, 5, 4]
my_list = ["banana", "apple", "cherry"]
my_list.sort()
print(my_list)
['apple', 'banana', 'cherry']
my_list = [1,2,3,4,5]
lastone = my_list[-1]
print(lastone)
5
animals = ('bear', 'cat', 'dog', 'elephant', 'cat')
print(animals)
print(animals[0])
('bear', 'cat', 'dog', 'elephant', 'cat') bear
#
animals = ('bear', 'cat', 'dog', 'elephant', 'cat')
print(animals[-1])
cat
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
('apple', 'banana', 'cherry', 'orange')
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
('a', 'b', 'c', 1, 2, 3)
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
apple banana cherry
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
print(thedict)
print(thedict["brand"])
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2024} Telsa
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
keys = thedict.keys()
print(keys)
dict_keys(['brand', 'model', 'year'])
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
values = thedict.values()
print(values)
dict_values(['Telsa', 'Model 3', 2024])
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
thedict.update({"year":2020})
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2020}
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
thedict.update({"color":"red"})
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2024, 'color': 'red'}
thedict = {
"brand": "Telsa",
"model": "Model 3",
"year": 2024
}
thedict.pop("year")
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3'}
thisset = {"apple", "banana", "cherry"}
print(thisset)
{'banana', 'apple', 'cherry'}
#Sets cannot have two items with the same value.
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
{'banana', 'apple', 'cherry'}
# add one item to a set
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
print("apple" in thisset)
{'banana', 'orange', 'apple', 'cherry'} True
# check does an element exist in a set
thisset = {"apple", "banana", "cherry"}
print("appl" in thisset)
False
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
{'mango', 'papaya', 'banana', 'apple', 'cherry', 'pineapple'}