Python 基础复习
不复习老忘
| Character | Format |
|---|---|
| d, | Integer |
| u | Unsigned integer |
| f | Floating point as m.ddddd |
| e | Floating point as m.ddddde+/-xx |
| E | Floating point as m.dddddE+/-xx |
| g | Use %e for exponents less than −4−4 or greater than +5+5, otherwise use %f |
| c | Single character |
| s | String, or any Python data object that can be converted to a string by using the str function. |
| % | Insert a literal % character |
| Modifier | Example | Description |
|---|---|---|
| number | %20d | Put the value in a field width of 20 |
| - | %-20d | Put the value in a field 20 characters wide, left-justified |
| + | %+20d | Put the value in a field 20 characters wide, right-justified |
| 0 | %020d | Put the value in a field 20 characters wide, fill in with leading zeros. |
Built-in Collection Data Types¶
methods used to build data structures
list
| Method Name | Use | Explanation |
|---|---|---|
| append | alist.append(item) | Adds a new item to the end of a list |
| insert | alist.insert(i,item) | Inserts an item at the ith position in a list |
| pop | alist.pop() | Removes and returns the last item in a list |
| pop | alist.pop(i) | Removes and returns the ith item in a list |
| sort | alist.sort() | Modifies a list to be sorted |
| reverse | alist.reverse() | Modifies a list to be in reverse order |
| del | del alist[i] | Deletes the item in the ith position |
| index | alist.index(item) | Returns the index of the first occurrence of item |
| count | alist.count(item) | Returns the number of occurrences of item |
| remove | alist.remove(item) | Removes the first occurrence of item |
big O for list operation
| Operation | Big-O Efficiency |
|---|---|
| index [] | O(1) |
| index assignment | O(1) |
| append | O(1) |
| pop() | O(1) |
| pop(i) | O(n) |
| insert(i,item) | O(n) |
| del operator | O(n) |
| iteration | O(n) |
| contains (in) | O(n) |
| get slice [x:y] | O(k) |
| del slice | O(n) |
| set slice | O(n+k) |
| reverse | O(n) |
| concatenate | O(k) |
| sort | O(n log n) |
| multiply | O(nk) |
string
| Method Name | Use | Explanation |
|---|---|---|
| center | astring.center(w) | Returns a string centered in a field of size w |
| count | astring.count(item) | Returns the number of occurrences of item in the string |
| ljust | astring.ljust(w) | Returns a string left-justified in a field of size w |
| lower | astring.lower() | Returns a string in all lowercase |
| rjust | astring.rjust(w) | Returns a string right-justified in a field of size w |
| find | astring.find(item) | Returns the index of the first occurrence of item |
| split | astring.split(schar) | Splits a string into substrings at schar |
set
| Method Name | Use | Explanation |
|---|---|---|
| union | aset.union(otherset) | Returns a new set with all elements from both sets |
| intersection | aset.intersection(otherset) | Returns a new set with only those elements common to both sets |
| difference | aset.difference(otherset) | Returns a new set with all items from first set not in second |
| issubset | aset.issubset(otherset) | Asks whether all elements of one set are in the other |
| add | aset.add(item) | Adds item to the set |
| remove | aset.remove(item) | Removes item from the set |
| pop | aset.pop() | Removes an arbitrary element from the set |
| clear | aset.clear() | Removes all elements from the set |
dictionary
| Method Name | Use | Explanation |
|---|---|---|
| keys | adict.keys() | Returns the keys of the dictionary in a dict_keys object |
| values | adict.values() | Returns the values of the dictionary in a dict_values object |
| items | adict.items() | Returns the key-value pairs in a dict_items object |
| get | adict.get(k) | Returns the value associated with k, None otherwise |
| get | adict.get(k,alt) | Returns the value associated with k, alt otherwise |
| Operator Use Explanation | ||
|---|---|---|
| [] | myDict[k] | Returns the value associated with k, otherwise its an error |
| in | key in adict | Returns True if key is in the dictionary, False otherwise |
| del | del adict[key] | Removes the entry from the dictionary |