Week 4

This week is about python lists.
A Python list is a collection of arbitrary objects, somewhat similar to an array in
many other programming languages but more flexible.
A list can be viewed as comma-separated items between square brackets. Each
item occupies one slot in the list. Each slot has a number associated to it. This
number is called index. Index is used to refer to item in the slot. List index is an
integer starting from 0 to list size – 1. Python allows negative index where index -
1 refers to the right most item.

Operations
+ concatenation (same as in string)
* repetition (same as in string)
len(L) number of items in list L
x in L membership –check whether x is in list L
for x in L : iteration through every item in list L

Functions
Common functions include
1 list.append(obj)
Appends object obj to list
2 list.count(obj)
Returns count of how many times obj occurs in list
3 list.extend(seq)
Appends the contents of seq to list
4 list.index(obj)
Returns the lowest index in list that obj appears
5 list.insert(index, obj)
Inserts object obj into list at offset index
6 list.pop(obj=list[-1])
Removes and returns last object or obj from list
7 list.remove(obj)
Removes object obj from list
8 list.reverse()
Reverses objects of list in place
9 list.sort([func])
Sorts objects of list, use compare function, if given

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Week 5

Week 1