Data types in Python
- Naveen
- 0
Now let’s discover data types that we can use to manipulate more data and do more things, such as lists, sets, and dictionaries.
Lists
A list is a sequence of arbitrary objects. You remember we said that strings are a sequence of (text) characters, so some of the things we already seen about strings apply here, the difference is that in list you store anything (data of any type that python knows) whereas In a sequence you store only a character.
As a first example, here is a list of numbers:
But list can also contain things of different types, so here is another example which shows that:
This list contains numbers, a string and list as well.
We can use our variables also here, then our example would look like:
Like all sequences, lists are ordered. Next, we will see how we can access content of a list one by one. We call that to iterate through the list, and we will see, we can do that using for loops. But for now, one important thing we can do is access the members of the list using the list index. The basic syntax is mylist [i], here I being the index we want to access.
Here is an example, using index 3 to 6 to access the elements between that numbers of a given list:
We can also access the element in the reverse order by using negative index, like -1 for the last element and -2 for the one before the last element. Nice isn’t?
This property of lists is interesting since you can automatically create a new list that is the result of taking a part of the existing list. Moreover, you can take all members up to a given index using the [:end] notation or all members from a given index using the [start:] notation:
We can get the size of a list, but for that, we just use a built-in function called len() and pass the list to that function, as follows:
We can also reverse a list using the reverse() method. For example, let’s take our new_list again and reverse() on it, as follows:
Dictionaries
A dictionary or dict maps a key to a value. The key can be any type of python object that computes a hash value. The value referenced by the key can be any type of python object.
Dictionaries are similar to sequences, and there is a way to return a sequence version of a dictionary as we will see in a minute. But one important difference is that a dictionary does not preserve order.
Here is an example:
We can access a value in the dictionary using the right key:
We can add an item to the dictionary:
Tuple
As we have seen, list is a type we use a lot to handle a sequence of arbitrary objects. But it is not the only one. There is also the tuple type, useful for an immutable sequence. That means that a tuple is a kind of list that you cannot change once you have defined it.
The notation for a tuple uses parenthesis while lists use brackets. For example, here is a tuple containing the Boolean values values we already know: Bools = (True, False)
Since they are a sequence type, almost everything we said about lists can be applied to tuple: access by index, slicing, and many more. The only thing is that operations that one can use to change or extend a list do not apply to a tuple.