= 'My favorite number is '
x = 21
y
print(x, y)
My favorite number is 21
Juma Shafara
November 1, 2023
python containers, python collections, list, set, object, tuple, dictionary, indexing, negative indexing, range of indexes, adding items, deleting items, Juma Shafara, What is an object?
Containers in Python are objects that contain other objects
In python, everything is an object. Even the simplest strings and numbers are considered as objects
Containers (also called collections) are objects that contain objects
In the example below, the fruits
variable contains three strings
Before we continue, we have a humble request, to be among the first to hear about future updates of the course materials, simply enter your email below, follow us on (formally Twitter), or subscribe to our YouTube channel.
[]
)
,
)
['dog', 'cat', 'rabbit', 'monkey']
<class 'list'>
A list can contain mixed data types.
0
refers to the first item, 1 refers to the second item, and so onNegative indexing is used to access the items of a list using negative numbers.
Where -1
refers to the last item, -2
refers to the second last item, and so on.
By using a colon ie :
, we can access a range of items at once.
Simply separate two indexes using the colon.
The first index is the start of the range, while the second index is the end of the range (not included)
If you don’t specify the last index, the range ends with the last item of the list
In this case, the range includes the last item.
The append()
method adds an item to the end of the list.
In this example, we will add 'fish'
to our pets list
The insert()
method inserts an item at the specified index.
In this case, we will insert 'rabbit'
to the index 0 and 'hamster'
to index 2
The pop()
method removes the last item from a list
The remove()
method removes the specified item value.
['dog', 'cat', 'monkey']
To delete a specified index, use the del
keyword
The length of a list refers to the number of items in a list, use the len()
method
To change an item’s value, access the index first and use the assignment operator.
The extend()
methods adds all items from one list to another
()
{}
A set can contain mixed data types, but can NOT contain mutable items like lists, sets and dictionaries
# A set can contain objects of different data types
mixed = {'dog', 21, True}
print(mixed)
print(type(mixed))
{True, 'dog', 21}
<class 'set'>
for
loop to access all its items one-by-oneNote: We’ll discuss a for loop in the next chapter
To add items to a set, use the add()
or update()
method.
The add()
method adds one item to a set.
The items of a set can NOT be changed because a set is immutable or unchangeable.
To remove an item from a set, use the remove()
method.
You should specify the value of the item you want to remove.
{'rabbit', 'dog'}
You can also use the discard()
method.
The difference between the remove()
and discard()
methods is that the discard()
method does not raise an error if the specified item is not present.
You can also use the pop()
method to remove an item.
But we cannot determine which item will be removed because a set is unordered.
To get the difference between two sets, use the subtraction operator (-
)
A dictionary is an unordered and mutable colletion of items.
A dictionary is written with curly brackets.
Each item in a dictionary contains a key/value
pair.
{'first_name': 'Voila', 'last_name': 'Akullu', 'age': 16}
In the above example we have 3 items:
'first_name'
, and its value is 'Viola'
.
'last_name'
, and its value is 'Akullu'
'age'
, and its value is 30
To access an item, specify the key name of an item inside square brackets.
# Accessing items
person = {
'first_name': 'Voila',
'last_name': 'Akullu',
'age': 16
}
print(person['last_name'])
Akullu
If you try to access an item using a key name that does not exist, an error will be raised
To add new items, specify a new index key name inside the square brackets and assign a value using the assignment operator.
To change an item, refer to its key name using square brackets and use the assignment operator.
To remove an item, use the pop() method.
The pop()
method removes an item with the specified key name.
# Remove items
person = {
'first_name': 'Voila',
'last_name': 'Akullu',
'age': 16
}
person.pop('age')
print(person)
{'first_name': 'Voila', 'last_name': 'Akullu'}
Another way to remove an item is to use the del
keyword.
A dictionary can contain another dictionary.
# Nesting dictionaries
employees = {
'manager': {
'name': 'Akullu Viola',
'age': 29
},
'programmer': {
'name': 'Juma Shafara',
'age': 30
}
}
print(employees)
{'manager': {'name': 'Akullu Viola', 'age': 29}, 'programmer': {'name': 'Juma Shafara', 'age': 30}}
To access an item in a nested dictionary, access the key name of the dictionary then the key name of the item
# Using a dictionary constructer
names = ('a1', 'b2', 'c3')
dictionary = dict(names)
print(dictionary)
{'a': '1', 'b': '2', 'c': '3'}