Python

  1. check type

    1
    isinstance(n,int)
  2. list

    1
    2
    3
    4
    5
    6
    7
    8
    t1 = ['a', 'b', 'c'];
    t2 = ['d', 'e', 'f'];
    t1.extend(t2);
    t1.append('g');
    t1 = t1+['g']
    t1.sort();
    del t1[1:3];
    t1.remove('b');
  3. dictionary

    1
    2
    3
    4
    5
    6
    7
    eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
    eng2sp.values() #'uno','dos','tres'
    eng2sp.items()
    'one' in eng2sp #true
    inverse = invert_dict(eng2sp)
    d = dict(zip('abc',range(3)))
    d.get(word,0) #d[word] if word in d, 0 otherwise
  4. tupe: similar with list but immutable

  5. file operation

    1
    2
    3
    4
    5
    6
    7
    8
    fout = os.getcwd()
    os.path.abspath('tmp.txt')
    os.path.exists('tmp.txt')
    os.path.isfile/isdir
    os.listdir(cwd)
    fout = open('output.txt','w')
    fout.write('hehe')
    fout.close()