= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron']
elements print(elements)
['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron']
In Python, there are four built-in data structures: lists, sets, tuples, and dictionaries.
Lists: These are ordered, mutable collections, which means you can add, remove, or change elements after the list creation. Lists can contain elements of different data types and duplicate values are allowed. They are defined by elements separated by commas inside square brackets []
.
Sets: Sets are unordered collections of unique elements. That means they don’t maintain elements in any specific order and duplicate values are not allowed. Sets are particularly useful for keeping track of distinct elements. They are defined by elements separated by commas inside curly braces {}
(but not key-value pairs).
Tuples: Tuples are ordered, immutable collections, thus you can’t change them after their creation (no addition or removal of elements). Elements can be of different types, and duplicate values are allowed. They are often used for related pieces of data, such as a set of coordinates, or a data record. They are defined by elements separated by commas inside parentheses ()
.
Dictionaries: Dictionaries, also known as hash tables or associative arrays, are unordered collections of key-value pairs. That means they hold data items that are unordered and cannot be sorted. Each key in the dictionary should be unique, and keys map to values which can be of any data type. Dictionaries are defined by key-value pairs separated by commas inside curly braces {}
, where key and value are separated by a colon :
.
A list in Python is a collection of items that are ordered and changeable. Lists are written with square brackets []
.
You can create a list simply by enclosing a comma-separated sequence of items in square brackets []
. These items can be of different types: integers, floats, strings, and even other lists!
= ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron']
elements print(elements)
['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron']
In Python, you can access list items by referring to their index number. Indexes in Python start from 0 for the first element.
= elements[0]
first_element print(first_element) #Output: Hydrogen
Hydrogen
Negative indexing can be used to access items from the end of the list. For example, elements[-1]
will return the last element: ‘Boron’.
You can change the value of a specific item in the list by referring to its index number.
1] = 'Noble gas'
elements[print(elements) #Output: ['Hydrogen', 'Noble gas', 'Lithium', 'Beryllium', 'Boron']
['Hydrogen', 'Noble gas', 'Lithium', 'Beryllium', 'Boron']
To add an item to the end of the list, use the append()
method. To add an item at a specific position, use the insert()
method.
'Carbon')
elements.append(print(elements) #Output: ['Hydrogen', 'Noble gas', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
1, 'Helium')
elements.insert(print(elements) #Output: ['Hydrogen', 'Helium', 'Noble gas', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
['Hydrogen', 'Noble gas', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
['Hydrogen', 'Helium', 'Noble gas', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
The remove()
method removes the first occurrence of the specified element.
'Noble gas')
elements.remove(print(elements) #Output: ['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon']
The ability to create and manipulate lists is an important part of managing data. Lists are very flexible and can hold entirely different kinds of elements.
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses ()
and are immutable, which means you can’t change elements of a tuple once it’s defined.
A tuple is created by placing all the items inside parentheses ()
, separated by commas.
= ('Hydrogen', 'Helium', 'Lithium')
elements print(elements) #Output: ('Hydrogen', 'Helium', 'Lithium')
('Hydrogen', 'Helium', 'Lithium')
You can access tuple items by referring to their index, inside square brackets.
print(elements[0]) #Output: Hydrogen
Hydrogen
As mentioned before, tuples are unchangeable, or immutable. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
= list(elements)
elements 1] = 'Noble gas'
elements[= tuple(elements)
elements print(elements) #Output: ('Hydrogen', 'Noble gas', 'Lithium')
('Hydrogen', 'Noble gas', 'Lithium')
A set is an unordered collection of items where every item is unique (no duplicates).
You can create a set by using the built-in set()
function with a list or a tuple, or you can use curly braces{}
.
= {'Hydrogen', 'Helium', 'Lithium'}
elements print(elements) #Output: {'Hydrogen', 'Helium', 'Lithium'}
{'Helium', 'Hydrogen', 'Lithium'}
You can add single items using the add()
method, and multiple items using the update()
method.
'Beryllium')
elements.add(print(elements) #Output: {'Beryllium', 'Hydrogen', 'Lithium', 'Helium'}
'Boron', 'Carbon'])
elements.update([print(elements) #Output: {'Beryllium', 'Hydrogen', 'Boron', 'Lithium', 'Helium', 'Carbon'}
{'Helium', 'Beryllium', 'Hydrogen', 'Lithium'}
{'Helium', 'Hydrogen', 'Lithium', 'Boron', 'Carbon', 'Beryllium'}
To remove an item in a set, you can use the remove()
, or the discard()
method.
'Beryllium')
elements.remove(print(elements) #Output: {'Hydrogen', 'Boron', 'Lithium', 'Helium', 'Carbon'}
{'Helium', 'Hydrogen', 'Lithium', 'Boron', 'Carbon'}
Tuples and sets are valuable constructs when you need to store multiple items in a single variable, just like lists. Try to work with different types of data structures in Python and understand their strengths and weaknesses.
A dictionary in Python is an unordered collection of items. Dictionaries are used to store data values in key:value pairs. While the values can be of any data type and can repeat, keys must be of immutable type (string, number, or tuple) and must be unique.
Dictionaries are defined by curly braces {}
. A dictionary contains keys and values separated by a colon :
. Each key-value pair in a dictionary is separated by a comma.
Let’s make a dictionary to represent an element in the periodic table:
= {
carbon "atomic_number": 6,
"symbol": "C",
"name": "Carbon",
"atomic_weight": 12.01,
"period": 2,
"group": 14
}
You can access the items of a dictionary by referring to its key name, inside square brackets:
print(carbon["name"]) #Output: Carbon
Carbon
There’s also a method called get()
that will give you the same result:
print(carbon.get("name")) #Output: Carbon
Carbon
You can change the value of a specific item by referring to its key name:
print(carbon["period"])
"period"] = 3
carbon[print(carbon["period"]) #Output: 3
2
3
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
"discovery"] = 3750
carbon[print(carbon)
{'atomic_number': 6, 'symbol': 'C', 'name': 'Carbon', 'atomic_weight': 12.01, 'period': 3, 'group': 14, 'discovery': 3750}
The pop()
method removes the item with the specified key name:
"discovery")
carbon.pop(print(carbon)
{'atomic_number': 6, 'symbol': 'C', 'name': 'Carbon', 'atomic_weight': 12.01, 'period': 3, 'group': 14}
A dictionary in Python is a great tool that helps you store a variety of data types and structures. They are optimized for retrieving data. You must know the key to retrieve the value.