import os print os.path.isfile(fname) li=[] if not li: print “empty" “12345”.zfill(10) hasattr(a,'attributename') except (Exception1, Exception2) as e: pass import os os.listdir() newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in xrange(0, len(l), n): yield l[i:i+n] import urllib2 urllib2.urlopen('http://www.example.com/').read() def matmult(a,b): zip_b = zip(*b) return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) for col_b in zip_b] for row_a in a] def primes(n): """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((ni*i-1)/(2*i)+1) return [2] + [i for i in xrange(3,n,2) if sieve[i]] def binary_search(array, target): lower = 0 upper = len(array) while lower < upper: # use < instead of <= x = lower + (upper - lower) // 2 val = array[x] if target == val: return x elif target > val: if lower == x: # this two are the actual lines break # you're looking for lower = x elif target < val: upper = x Source: https://habr.com/ru/post/267903/
All Articles