0% found this document useful (0 votes)
31 views

Data Type in Python

There are four main collection data types in Python: lists, tuples, sets, and dictionaries. Lists are ordered and changeable. Tuples are ordered and unchangeable. Sets are unordered and unindexed with no duplicate items. Dictionaries contain key-value pairs with no duplicate keys. Each data type has different properties to make it suitable for different use cases when working with data sets.

Uploaded by

chiragg642
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Data Type in Python

There are four main collection data types in Python: lists, tuples, sets, and dictionaries. Lists are ordered and changeable. Tuples are ordered and unchangeable. Sets are unordered and unindexed with no duplicate items. Dictionaries contain key-value pairs with no duplicate keys. Each data type has different properties to make it suitable for different use cases when working with data sets.

Uploaded by

chiragg642
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Types in python

There are four collection data types in the Python programming language:

 List is a collection which is ordered and changeable. Allows duplicate members.


 Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
 Set is a collection which is unordered and unindexed. No duplicate members.
 Dictionary is a collection which is ordered and changeable. No duplicate members.

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”]

mylist = ["apple", "banana", "cherry"]

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:

a= ["apple", "banana", "cherry"]

print(len(a))

type()

From Python's perspective, lists are defined as objects with the data type 'list':

mylist = ["apple", "banana", "cherry"]

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:

a = ("apple", "banana", "cherry")

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':

mytuple = ("apple", "banana", "cherry")

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.

thisset = {"apple", "banana", "cherry"}

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.

Duplicates Not Allowed

Sets cannot have two items with the same value.

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.

Duplicates Not Allowed


Dictionaries cannot have two items with the same key:

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))

Dictionary Items - Data Types

The values in dictionary items can be of any data type:

thisdict = {

"brand": "Ford",

"electric": False,

"year": 1964,

"colors": ["red", "white", "blue"]

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()

print(x) #before the change

car["color"] = "white"

print(x) #after the change

output

dict_keys(['brand', 'model', 'year'])

dict_keys(['brand', 'model', 'year', 'color'])

Get Values

The values() method will return a list of all the values in the dictionary.

x = thisdict.values()

output

dict_values(['Ford', 'Mustang', 1964])

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()

print(x) #before the change

car["year"] = 2020
print(x) #after the change

output

dict_values(['Ford', 'Mustang', 1964])

dict_values(['Ford', 'Mustang', 2020])

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()

print(x) #before the change

car["color"] = "red"

print(x) #after the change

Nested Dictionaries

A dictionary can contain dictionaries, this is called nested dictionaries.

Create a dictionary that contain three dictionaries:

myfamily = {

"child1" : {

"name" : "Emil",

"year" : 2004

},

"child2" : {

"name" : "Tobias",

"year" : 2007

},

"child3" : {

"name" : "Linus",

"year" : 2011

}
}

Or, if you want to add three dictionaries into a new dictionary:

child1 = {

"name" : "Emil",

"year" : 2004

child2 = {

"name" : "Tobias",

"year" : 2007

child3 = {

"name" : "Linus",

"year" : 2011

myfamily = {

"child1" : child1,

"child2" : child2,

"child3" : child3}

You might also like