You are on page 1of 3

Chapter (4)

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)

del Removes the specific elements from the list


remove(value) Removes the element with the specified value

pop(position) Removes the element at the specified position


count(value) Returns the number of elements with the specified value

index(value) Returns the index of the first element with the specified value

reverse() Reverse the order of the list

sort() Sorts the list

Create the “Staff.py”

StaffName = []

StaffName.append("Soe")

StaffName.append("Aung")

StaffName.append("Myint")

print (StaffName)

Python Programming Ch 4-1


KMD Computer Centre
Tuple

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

Create the “Syllabus.py”

Syllabus=("Java","C#","SQL Server(461)")

print (len(Syllabus)," Syllabus in SE Course.\nThey are ",Syllabus)

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

update([value1,value2,…]) To add more than one item to a set

len(SetObject) To determine how many items have in 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

del() Delete the set

Create the “Centre.py”

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

Python Programming Ch4-2


KMD Computer Centre
by using a new index key and assigning a value to it. Can access the items of a dictionary by
referring to its key name.

Method Description
get(key) To get the value of the given key

values() To return values of a dictionary


items() To return item (key-value pairs)

len(Dictionary Object) To determine how many items (key-value pairs) a dictionary have

pop(key) To remove the item with the specified key name

clear() To clear all item in the dictionary

del To delete the dictionary

Create the “CentreInfo.py”

CentreInfo= {

"Pansodan":245180,"Myaynigone":502233,"Theingyi Zay":384268

print (CentreInfo)

CentreInfo["Hledan-1"]=524400

print (CentreInfo)

print ("Pansodan Phone Number is ",CentreInfo["Pansodan"])

Exercise

• Your favorite foods are store in List, Tuple, Set and Dictionary. And display it.

Python Programming Ch4-3

You might also like