Basic Operation
- 安装某一个包, terminal里面打 pip install name
- 自定义函数
1 | def soton_fun1(x1,x2): |
- 内置函数
1 | #知道具体某一个column的位置 |
Tuple
Create Tuple
1 | tup = 5, 4, 6 |
- Tuple is unchangable, and the objects stored in tuple can be dfferent.
- If the object stored in tuple is mutable, then we can modify this object.
unpacking tuples
1 | a, b = 1, 2 |
- Using the feature of unpacking tuples,we can swap variable names easily
advanced tuple unpacking
1 | values = 1, 2, 3, 4, 5 |
- the returns of a, b is (1, 2)—tuple, and the return of rest is [3, 4, 5]—list
python format function
1 | seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] |
- See Book P53
- seq is a nested tuple
- The numbers in curly braces are the orders/positions of the parameters in the format function.(这里大括号里面的指定的是后面format函数里面参数的位置)
List
Basic ideal
- Lists are modifiable and we can use [ ] or list function to define.
Adding and removing
1 | a_list.append(xx) |
Concatenating and combining lists
1 | [1, 2, 3] + [4, 5, 6] |
Sorting
1 | a_list.sort() |
Slicing
1 | a_list[1:5] |
Built-in sequence functions
Enumerate
- enumerate function is used to compute a dict mapping the values of a sequence to their locations in the sequence.
- link:http://www.runoob.com/python3/python3-func-enumerate.html
- usually used in for loop
1 | list(enumerate(a_list)) |
Sorted function
- it gives a new sorted list form the given sequence
1 | c_list = sorted([7, 5, 5, 2]) |
Zip function
- It pairs up the elements of a number of lists, tuples or other sequences to create a list of tuples
- It the length of the lists are not the same, the number of elements it produces is determined by the shortest sequence
- Usually used in for loop, possibly combined with enumerate
_ Given a “zipped” sequence, zip function can be used to unzip this sequence. And it’s very clever!
1 | zipped = zip(a_list, b_list, c_list) |
Reversed
- reverse the sequence
1 | list(reversed(range(10))) |
Dict
- It is also called hash map or associative array
- Dict is created through { } or by using dict() function, and it’s mutable
-it is the collection of key-value pairs. - Key must be unique, but the value don’ t need.
- Value can be any type.
- When we are creating a dict, if we have two same key, the second value will be collected.
Create and add
1 | d1 = {'a': 'some value', 'b': [1, 2, 3, 4]} |
Check and delete
1 | 'b' in d1 |
Merge dicts
1 | d1.update({'c': 'xixi', 5: 'lalal'}) |
Create dicts from sequences
1 | mapping = {} |
Default value
1 | if key in some_dict: |
1 | words = ['apple', 'bat', 'bar', 'atom', 'book'] |
- the second and third are precisely and better.
- link:http://www.runoob.com/python3/python3-att-dictionary-setdefault.html
Valid key types
- Key should bu immutable, like int, float, string, tuples and so on.
- List is unacceptable, cause it is mutable
- Term—hashability
- Use hash function to check
- List can only be used if you convert it to tuple first.
1 | hash('string') |
Set
- Set is an unordered collection of unique elements.
- Use set function or { } to create. But only use set function if you want to create a empty set.
- link:http://www.runoob.com/python3/python3-set.html
List, Set, Dict and Nested list Comprehensions
- The condition can be omitted
- Basic form for List: [expr for val in collection if condition]
1 | strings = ['a', 'as', 'bat'] |
- Basic form for Set: {exp for value in collection if condition}
1 | {len(x) for x in strings} |
- Basic form for Dict: {key-expr: value_expr for value in collection if condition}
1 | {val: index for index, val in enumerate(strings)} |
Nested list comprehensions
- Arrange according to the order of nesting
- The order of the for expressions would be the same if you wrote a nested for loop.
1 | # P1, method 1 |
Fucntions
- It is worth writing a reusable function if repeat the same of similar code more than once.
- It is declared with the def keyword and returned form with the return keyword.
- Fucntions can have multiple return statement.
- If there is no returnin the whole function, then it will return none.
- Each function can have multiple positional arguments and keyword arguments.
- Keyword arguments are used to specify default values or optional arguments.
- The keyword arguments must follow the positional arguments.
1 | def my_dunction(x, y, z=1.5): |
Return multiple values
1 | def f(): |
Functions are objects
- functions can be used as objects
- Examples are uniforming the strings.
1 | # Method 1 |
Anonymous fucntions (Lambda)
- It is a way to write functios consisting of a single statement.
1 | # Method 1 |
Curring: Partial Argument Application
- Deriving new functions from existing ones by partial argument application.
1 | def add_numbers(x, y): |