Data Type in Python
Data Type in Python
There are four collection data types in the Python programming language:
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for
a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
I. List
Lists are used to store multiple items in a single variable.
A=[2,6,8,10]
A=[3,5.9,”computer”]
List items are ordered, changeable, and allow duplicate values.List items are indexed, the first item has index [0],
the second item has index [1] etc.
Ordered
When we say that lists are ordered, it means that the items have a defined order, and that order will not change.If
you add new items to a list, the new items will be placed at the end of the list.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
List Length
To determine how many items a list has, use the len() function:
print(len(a))
type()
From Python's perspective, lists are defined as objects with the data type 'list':
print(type(mylist))
II.Tuple
Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and
unchangeable. Tuples are written with round brackets.
Example:
print(a)
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.Tuple items are indexed, the first item has
index [0], the second item has index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a defined order, and that order will not
change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been
created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
Tuple Length
To determine how many items a tuple has, use the len() function:
type()
From Python's perspective, tuples are defined as objects with the data type 'tuple':
print(type(mytuple))
III Set
Sets are used to store multiple items in a single variable.A set is a collection which is both unordered and
unindexed.Sets are written with curly brackets.
print(thisset)
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
Unchangeable
Sets are unchangeable, meaning that we cannot change the items after the set has been created.Once a set is
created, you cannot change its items, but you can add new items.
Length of a Set
To determine how many items a set has, use the len() method.
IV. Dictionary
Dictionaries are used to store data values in key:value pairs.A dictionary is a collection which is ordered,
changeable and does not allow duplicates.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
Dictionary Items
Dictionary items are ordered, changeable, and does not allow duplicates.Dictionary items are presented in
key:value pairs, and can be referred to by using the key name.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict["brand"])
Ordered
When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not
change.
Unordered means that the items does not have a defined order, you cannot refer to an item by using an index.
Changeable
Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been
created.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
print(thisdict)
Dictionary Length
To determine how many items a dictionary has, use the len() function:
print(len(thisdict))
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
Accessing Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = thisdict["model"]
There is also a method called get() that will give you the same result:
x = thisdict.get("model")
Get Keys
The keys() method will return a list of all the keys in the dictionary.
x = thisdict.keys()
The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in
the keys list.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.keys()
car["color"] = "white"
output
Get Values
The values() method will return a list of all the values in the dictionary.
x = thisdict.values()
output
The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected
in the values list.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.values()
car["year"] = 2020
print(x) #after the change
output
Add a new item to the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.values()
car["color"] = "red"
Nested Dictionaries
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
child1 = {
"name" : "Emil",
"year" : 2004
child2 = {
"name" : "Tobias",
"year" : 2007
child3 = {
"name" : "Linus",
"year" : 2011
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3}