Basic
package import
If the import module in the same dir1
from . import core
If the import module in the top dir1
from .. import core
If the import module in the other subdir1
from ..other import core
List
generate fixed length of empty list
1 | list = [0]*26 |
String
reverse the string
1 | st = '1101010' |
Tuple
named tuple
1 | from collections import namedtuple |
builtin
ord(…)
Return the integer ordinal of a one-character string.1
2print ord('b')
>> 98
collections module
defaultdict
defaultdict means that if a key is not found in the dictionary, then instead of
a KeyError being thrown, a new entry is created. The type of the new entry is
given by the argument of defaultdict.1
2
3
4
5somedict = {}
print(somedict[3]) # KeyError
somedict = defaultdict(int)
print(somedict[3]) # print int(),thus 0
1 | >> ice_cream = defaultdict(lambda: 'Vanilla') |