Professional Documents
Culture Documents
Collection
Python Collection (Arrays)
There are four collection data types in the Python programming language:
• List
• Tuple
• Set
• Dictionary
List
List is a collection which is ordered and changeable. Allows duplicate members. Access the list
items by referring to the index number. Python has a set of built-in methods that you can use on
lists.
Method Description
append(value) Adds an element at the end of the list
insert(position, value) Adds an element a the specified position
clear() Removes all the elements from the list(version 3.3 or later)
index(value) Returns the index of the first element with the specified value
StaffName = []
StaffName.append("Soe")
StaffName.append("Aung")
StaffName.append("Myint")
print (StaffName)
A tuple is a collection which is ordered and unchangeable. Cannot add items after create the tuple
and remove items in a tuple.So assign the items in a tuple while the tuple creation.In Python tuples
are written with round brackets.
Method Description
len() To determine how many items have in tuple
Syllabus=("Java","C#","SQL Server(461)")
Set
A set is a collection which is unordered and unindexed. In Python sets are written with curly
brackets.Once a set is created, cannot change its items, but can add and remove items. Note: Sets
are unordered, so the items will appear in a random order.
Method Description
add(value) To add one item to a set
remove(value) To remove an item in a set (Note : If the item to remove does not
exist, will raise an error)
discard(value) To remove an item in a set(Note : If the item to remove does not
exist, will not raise an error)
clear() To clear all item in the set
Centre={"Pansodan","Myaynigone","TheingyiZay","Hledan1","Hledan2"}
print (Centre)
Dictionary
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are
written with curly brackets, and they have keys and values.Adding an item to the dictionary is done
Method Description
get(key) To get the value of the given key
len(Dictionary Object) To determine how many items (key-value pairs) a dictionary have
CentreInfo= {
"Pansodan":245180,"Myaynigone":502233,"Theingyi Zay":384268
print (CentreInfo)
CentreInfo["Hledan-1"]=524400
print (CentreInfo)
Exercise
• Your favorite foods are store in List, Tuple, Set and Dictionary. And display it.