input() and raw_input()
- input(): Gets inputs from the user
1 2 3 | |
- raw_input(): Gets input from the user, as a string
1 2 3 | |
repr() and str()
- repr(): Returns a string representation of a value
- A synonym for repr(‘x’) is ‘x’
1 2 3 4 | |
- str(): Converts a value to a string
1 2 3 4 | |
Slicing
- numbers[beg : end : direction(interval)]
- range(beg…<end), i.e., [beg, end)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Membership
- if … in …
- if … not in …
Lists
- To convert a list of characters such as the preceding code back to a string, use ‘ ’.join(somelist)
1 2 3 4 | |
- Deleting Elements use del
1 2 3 4 5 | |
- Assigning to Slices
1 2 3 4 5 | |
- Slice assignments can be used to insert elements without replacing any of the original ones
1 2 3 4 5 6 7 8 | |
- String Formatting
1 2 3 4 5 6 7 8 9 | |
- Template Strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
- find()
1 2 3 | |
- split()
1 2 3 4 5 6 7 8 | |
- join() The inverse of split, used to join the elements of a sequence
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
- strip() Get rid of the characters listed in strip()
1 2 3 4 5 6 7 8 9 | |
- title()
1 2 | |
- string.capwords()
1 2 3 | |
- replace()
1 2 | |
- maketrans() and translate()
1 2 3 4 5 6 | |
- An optional second argument can be supplied to specify that should be deleted
1 2 3 4 5 | |
Dictionary
- dict()
1 2 3 | |
- clear()
1 2 3 4 5 6 7 8 9 10 11 | |
- copy()
1 2 3 4 5 6 7 8 9 | |
- deepcopy() Avoid the situation that modify the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
- fromkeys Create a new dictionary with the given keys.
1 2 3 4 5 6 7 8 | |
- get()
1 2 3 4 5 6 7 | |
- has_key() Check whether a dictionary has a given key.
1 2 3 4 5 6 7 | |
- items() Return all the items of dictionary as a list of the form (key, value)
1 2 3 | |
- iteritems()
1 2 3 4 5 6 | |
- pop()
1 2 3 4 5 6 7 | |
- popitem()
1 2 3 4 5 | |
- setdefault() Set the value corresponding to the given key if it not already in the dictionary
1 2 3 4 5 | |
- update()
1 2 3 4 5 6 7 8 9 | |
- values
1 2 3 4 5 6 | |
- range(a, b), [a, b)
1 2 3 4 5 | |
Importing
- import sth. from a module
1 2 3 | |
- Define aliases
1 2 3 | |
- loop the key of a dictionary
1 2 3 4 5 6 7 | |
- Slightly Loopy
1 2 | |
1 2 3 4 5 6 7 8 | |
Executing and Evaluating
- exec
1 2 | |
- eval
1 2 3 4 | |
Bonus: Trick of Reduce Code
- Comparison
1 2 3 | |
- or
1 2 3 4 5 | |
- a if b else c
1 2 3 4 5 | |