上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Lists and Arrays Python lists are dynamic.They can grow and shrink on demand. Python lists are also heterogeneous,a single list can hold arbitrary data types. Python lists are mutable sequences of arbitrary objects. 16
Lists and Arrays • Python lists are dynamic. They can grow and shrink on demand. • Python lists are also heterogeneous, a single list can hold arbitrary data types. • Python lists are mutable sequences of arbitrary objects. 16
上浒充通大¥ ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY List Operations Operator Meaning <seq><seq> Concatenation <seq><int-expr> Repetition <seq>[] Indexing len(<seq>) Length <seq>[:] Slicing for <var>in <seq>:Iteration <expr>in <seq> Membership (Boolean) 17 Python
List Operations Operator Meaning <seq> + <seq> Concatenation <seq> * <int-expr> Repetition <seq>[] Indexing len(<seq>) Length <seq>[:] Slicing for <var> in <seq>: Iteration <expr> in <seq> Membership (Boolean) Python Programming , 2/e 17
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY List Operations Except for the membership check,we've used these operations before on strings. The membership operation can be used to see if a certain value appears anywhere in a sequence. >>>1st=[1,2,3,4] >>3 in 1st True 18
List Operations • Except for the membership check, we‟ve used these operations before on strings. • The membership operation can be used to see if a certain value appears anywhere in a sequence. >>> lst = [1,2,3,4] >>> 3 in lst True 18
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY List Operations The summing example from earlier can be written like this: sum 0 for x in s: sumsum x Unlike strings,lists are mutable: >>>1st=[1,2,3,4] >>>1st[3] 4 >>> 1st [3]"Hello >>> lst [1, 2,3,'He11o'] >>> 1st[2]=7 lst ,2,7,'He11o1 19
List Operations • The summing example from earlier can be written like this: sum = 0 for x in s: sum = sum + x • Unlike strings, lists are mutable: >>> lst = [1,2,3,4] >>> lst[3] 4 >>> lst[3] = "Hello“ >>> lst [1, 2, 3, 'Hello'] >>> lst[2] = 7 >>> lst [1, 2, 7, 'Hello'] 19
上游充通大学 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY List Operations Method Meaning <list>.append(x) Add element x to end of list. <list>.sort() Sort (order)the list.A comparison function may be passed as a parameter. <list>.reverse() Reverse the list. <list>.index(x) Returns index of first occurrence of x. <list>.insert(i,x) Insert x into list at index i. <list>.count(x) Returns the number of occurrences of x in list. <list>.remove(x) Deletes the first occurrence of x in list. <list>.pop(i) Deletes the ith element of the list and returns its value. 20 Python
List Operations Method Meaning <list>.append(x) Add element x to end of list. <list>.sort() Sort (order) the list. A comparison function may be passed as a parameter. <list>.reverse() Reverse the list. <list>.index(x) Returns index of first occurrence of x. <list>.insert(i, x) Insert x into list at index i. <list>.count(x) Returns the number of occurrences of x in list. <list>.remove(x) Deletes the first occurrence of x in list. <list>.pop(i) Deletes the ith element of the list and returns its value. Python Programming , 2/e 20