Python
a programming language
Class 3
Collection data types in the Python
 List
 Tuple
 Set
 Dictionary
List
 List is a collection which is ordered and changeable. Allows duplicate members.
 Syntax:
List_name=[ item1, item2,……]
print(List_name)
 Eg:
thislist = ["apple", "banana", "cherry"]
print(thislist)
Operations on List
 Accessing items
 Changing Elements
Access Items
Syntax:
List_name[ index_number ]
Eg:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Change Item Value
 Syntax:
List_name [ Index_number ] = value
 Eg:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Loop through a list
Syntax:
for variable_name in List_name:
statements
 Eg:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Check if Item exist
Syntax:
if item in List_name:
statements
 Eg:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
List Length
Syntax:
len(List_name)
 Eg
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
Add Items
Syntax:
List_name.append(“item”)
 Eg
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Insert()
 To add an item at the specified index, use the insert() method:
Syntax:
List_name.insert( index,value)
 Eg:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Remove Item
Syntax:
List_name.remove(element)
 Eg:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Pop()
 The pop() method removes the specified index,(or the last item if index is not
specified)
Syntax:
List_name.pop()
 Eg:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
Del()
 The del keyword removes the specified index:
Syntax:
del List_name[ index ]
 EG:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
Clear()
 The clear() empties the list
Syntax:
List_name.clear()
 Eg:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
List methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Exercise:
 Print the 2nd item in a list
 Replace the 1st element with “kiwi”
 Append “mango” to the current list
 What is insert()
 What is remove()

Python Collection datatypes

  • 1.
  • 2.
    Collection data typesin the Python  List  Tuple  Set  Dictionary
  • 3.
    List  List isa collection which is ordered and changeable. Allows duplicate members.  Syntax: List_name=[ item1, item2,……] print(List_name)  Eg: thislist = ["apple", "banana", "cherry"] print(thislist)
  • 4.
    Operations on List Accessing items  Changing Elements
  • 5.
    Access Items Syntax: List_name[ index_number] Eg: thislist = ["apple", "banana", "cherry"] print(thislist[1])
  • 6.
    Change Item Value Syntax: List_name [ Index_number ] = value  Eg: thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thislist)
  • 7.
    Loop through alist Syntax: for variable_name in List_name: statements  Eg: thislist = ["apple", "banana", "cherry"] for x in thislist: print(x)
  • 8.
    Check if Itemexist Syntax: if item in List_name: statements  Eg: thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list")
  • 9.
    List Length Syntax: len(List_name)  Eg thislist= ["apple", "banana", "cherry"] print(len(thislist))
  • 10.
    Add Items Syntax: List_name.append(“item”)  Eg thislist= ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)
  • 11.
    Insert()  To addan item at the specified index, use the insert() method: Syntax: List_name.insert( index,value)  Eg: thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)
  • 12.
    Remove Item Syntax: List_name.remove(element)  Eg: thislist= ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
  • 13.
    Pop()  The pop()method removes the specified index,(or the last item if index is not specified) Syntax: List_name.pop()  Eg: thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist)
  • 14.
    Del()  The delkeyword removes the specified index: Syntax: del List_name[ index ]  EG: thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
  • 15.
    Clear()  The clear()empties the list Syntax: List_name.clear()  Eg: thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
  • 16.
    List methods Method Description append()Adds an element at the end of the list clear() Removes all the elements from the list copy() Returns a copy of the list count() Returns the number of elements with the specified value extend() Add the elements of a list (or any iterable), to the end of the current list index() Returns the index of the first element with the specified value insert() Adds an element at the specified position pop() Removes the element at the specified position remove() Removes the item with the specified value reverse() Reverses the order of the list sort() Sorts the list
  • 17.
    Exercise:  Print the2nd item in a list  Replace the 1st element with “kiwi”  Append “mango” to the current list  What is insert()  What is remove()