Examples of list operations¶

In [ ]:
# Create a list
my_list = [1, 2, 3, 4, 5]
print(my_list)
[1, 2, 3, 4, 5]
In [ ]:
# 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
In [ ]:
# find the index of a specific value
my_list = [1, 2, 3, 4, 5]
ind = my_list.index(2)
print(ind)
1
In [ ]:
# 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]
In [ ]:
# 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]
In [ ]:
# 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]
In [ ]:
# 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]
In [ ]:
# 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]
In [ ]:
# Remove specific element by value
my_list = ["apple", "banana", "cherry"]
my_list.remove("banana")
print(my_list)
['apple', 'cherry']
In [ ]:
# remove specific element by index
my_list = ["apple", "banana", "cherry"]
my_list.pop(1)
print(my_list)
['apple', 'cherry']
In [ ]:
# remove the last element
my_list = ["apple", "banana", "cherry"]
my_list.pop()
print(my_list)
['apple', 'banana']
In [ ]:
# use del to remote element
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
['banana', 'cherry']
In [ ]:
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
In [ ]:
my_list = ["cherry", 100, True]
print(my_list)
['cherry', 100, True]
In [ ]:
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']
In [ ]:
my_list = ["apple", "banana", "cherry"]
# Get the length of the list
print(len(my_list))  # Output: 3
3
In [ ]:
my_list = [15, 5, 6, 4, 5]
my_list.sort(reverse=True)
print(my_list)
[15, 6, 5, 5, 4]
In [ ]:

In [ ]:
my_list = ["banana", "apple", "cherry"]
my_list.sort()
print(my_list)
['apple', 'banana', 'cherry']
In [ ]:
my_list = [1,2,3,4,5]
lastone = my_list[-1]
print(lastone)
5

Examples of tuple operations¶

In [ ]:
animals = ('bear', 'cat', 'dog', 'elephant', 'cat')
print(animals)
print(animals[0])
('bear', 'cat', 'dog', 'elephant', 'cat')
bear
In [ ]:
#
animals = ('bear', 'cat', 'dog', 'elephant', 'cat')
print(animals[-1])
cat
In [ ]:
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y

print(thistuple)
('apple', 'banana', 'cherry', 'orange')
In [ ]:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)
('a', 'b', 'c', 1, 2, 3)
In [ ]:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2

print(mytuple)
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
In [ ]:
fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)
apple
banana
cherry

Example of dictionary¶

In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
print(thedict)
print(thedict["brand"])
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2024}
Telsa
In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
keys = thedict.keys()
print(keys)
dict_keys(['brand', 'model', 'year'])
In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
values = thedict.values()
print(values)
dict_values(['Telsa', 'Model 3', 2024])
In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
thedict.update({"year":2020})
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2020}
In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
thedict.update({"color":"red"})
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3', 'year': 2024, 'color': 'red'}
In [ ]:
thedict = {
  "brand": "Telsa",
  "model": "Model 3",
  "year": 2024
}
thedict.pop("year")
print(thedict)
{'brand': 'Telsa', 'model': 'Model 3'}

Examples of sets¶

In [ ]:
thisset = {"apple", "banana", "cherry"}
print(thisset)
{'banana', 'apple', 'cherry'}
In [ ]:
#Sets cannot have two items with the same value.
thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)
{'banana', 'apple', 'cherry'}
In [ ]:
# add one item to a set
thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)
print("apple" in thisset)
{'banana', 'orange', 'apple', 'cherry'}
True
In [ ]:
# check does an element exist in a set
thisset = {"apple", "banana", "cherry"}

print("appl" in thisset)
False
In [ ]:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)
{'mango', 'papaya', 'banana', 'apple', 'cherry', 'pineapple'}