Ana教授的第五节课,还是那么通俗易懂,先分享一个学习编程很好的网站:http://pythontutor.com/(可能需要科学上网)本篇学习笔记某些截图出自该网站。
Compound Data Types
Tuple
- an ordered sequence of elements, can mix element types
- cannot change element values,immutable ()
- represented with parentheses
- conveniently used to swap variable values:
- (x, y) = (y, x)
List
- ordered sequence of information, accessible by index
- a list is denoted by square brackets, []
- a list contains elements
- usually homogeneous (ie, all integers)
- can contain mixed types (not common)
- list elements can be changed so a list is mutable
OPERATIONS ON LISTS
ADD
- add elements to end of list with L.append(element) – mutates the list!
- combine lists together use L.extend(some_list) – mutate list
REMOVE
- delete element at a specific index with del(L[index])
- remove element at end of list with L.pop(), returns the removed element
- remove a specific element with L.remove(element)
- looks for the element and removes it
- if element occurs multiple times, removes first occurrence
- if element not in list, gives an error
CONVERT
- convert string to list with list(s), returns a list with every character from s an element in L
- can use s.split(), to split a string on a character parameter, splits on spaces if called without a parameter
- use ‘‘.join(L) to turn a list of characters into a string, can give a character in quotes to add char between every element
SORTED AND REVERSED
https://docs.python.org/3/tutorial/datastructures.html
MUTATION, ALIASING, CLONING
- lists are mutable
- behave differently than immutable types
- is an object in memory
- variable name points to object
- any variable pointing to that object is affected
- key phrase to keep in mind when working with lists is side effects
觉得slide很经典,很容易理解概念,就把几页slide截下了 avoid mutating a list as you are iterating over it