Tuples Dictionaries
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Tuples
Tuples is a sequence data type as well. We can keep different elements inside tuples. Tuples are a combination of different elements separated by a comma.
Tuples are also like a list but with a change that we can not change indivudual values inside a tuple. We can reference those individual elements though.
That makes a tuple immutable. Lists are mutable. Mutable means, we can change the individual elements.
tuple1 = 1, 2, "third element", Truetuple1(1, 2, 'third element', True)You might have noticed that list uses square brackets to store elements. Tuples uses round brackets to store elements. Just don't be confused as of now with these bracket notations.
Just stick to the notation that list uses square brackets and tuples use round brackets.
tuple2 = (1, 2, "third element", True)tuple2(1, 2, 'third element', True)tuple1[0] # Get the first index of tuple11tuple1[:-1] # get all elements except the last index(1, 2, 'third element')# This cell will throw an error as we will attempt
# to change an individual value here
# tuple1[0] = "New element"We can change a list to a tuple by using the tuple() function
list3 = list(range(10))
list3[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]tuple2 = tuple(list3)
tuple2(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)Dictionary
Dictionary stores different data types as a key value pair.
$$Key : Value$$
dict1 = dict(Name = "Tickles",
Age = 29,
Occupation = "Jobless",
Excuse = "I'm a lead AI engineer for Python"
)
dict1{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python"}We usually don't use this way to create dictionaries as this confuses a lot of people as to why the syntax for creating dictionaries is so different.
If a character sequence in python has a string data type, then why Name, Age, Occupation, Excuse are not typed inside the quotes.
Well, a lot of these things will be cleared by the time we've reached the function chapter.
For now, let's look at an easier way which we all use to create dictionaries.
dict2 = {
"Name" : "Tickles",
"Age" : 29,
"Occupation" : "Jobless",
"Excuse" : "I'm a lead AI engineer for Python"
}
dict2 {'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python"}dict2["Name"]'Tickles'Now notice, no matter the way we chose to create the dictionary, notice in the output, that dictionaries have the curly brackets / braces and that the key value pairs are displayed with this notation
$$Key:Value$$
Now let's look at some useful operations with dictionaries to clear why this particular data type is so important.
dict2{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python"}dict2["Name"]'Tickles'Indexing with dictionaries
Individual values are stored inside a dictionaries as Values and we use some unique Keys to reference them.
$$Dictionary[Key] = Value$$
As you might have noticed, indexing with dictionaries is different from indexing in lists and tuples.
dict2["Age"]29We can overwrite the indivudual values stored inside a dictionary. But let's first learn how to add values to a dictionary.
dict2["Salary"] = "I do get paid. No trust me I earn. Alright you got me"dict2{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python",
'Salary': 'I do get paid. No trust me I earn. Alright you got me'}dict2["Magic power"] = "1000 magic points"dict2{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python",
'Salary': 'I do get paid. No trust me I earn. Alright you got me',
'Magic power': '1000 magic points'}Now let's look at how we can delete individual elements of a dictionary.
Lets delete the value of the key "Salary" as Tickles clearly got offended with this question.
We will use the del keyword here.
del dict2["Salary"]dict2{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python",
'Magic power': '1000 magic points'}Make sure you don't run the code cell containing del keyword twice, otherwise python will try to delete the value of key "Salary" again, which will cause error because we have already deleted it once.
# This will throw a key error (meaning key doesn't exist)
# del dict2["Salary"]dict2 = {
"Name" : "Tickles",
"Age" : 29,
"Occupation" : "Jobless",
"Excuse" : "I'm a lead AI engineer for Python"
}
dict2{'Name': 'Tickles',
'Age': 29,
'Occupation': 'Jobless',
'Excuse': "I'm a lead AI engineer for Python"}Some useful methods inside dictionaries
dict2.items()dict_items([('Name', 'Tickles'), ('Age', 29), ('Occupation', 'Jobless'), ('Excuse', "I'm a lead AI engineer for Python")])dict2.get("Name")'Tickles'dict2.pop("Excuse")"I'm a lead AI engineer for Python"dict2{'Name': 'Tickles', 'Age': 29, 'Occupation': 'Jobless'}Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →