Skip to content

Instantly share code, notes, and snippets.

@LoicH
Last active December 11, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LoicH/d6866c829d76fcd2438cd9645d971366 to your computer and use it in GitHub Desktop.
Save LoicH/d6866c829d76fcd2438cd9645d971366 to your computer and use it in GitHub Desktop.
Cheat sheets

break, continue, pass

break

Terminates the current loop and resumes execution at the next statement, just like the traditional break found in C.

continue

Returns the control to the beginning of the loop

pass

used when a statement is required syntactically but you do not want any command or code to execute

else with loops

If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

Example: searching for prime numbers from 10 to 20

   for num in range(10,20):  #to iterate between 10 to 20
      for i in range(2,num): #to iterate on the factors of the number
         if num%i == 0:      #to determine the first factor
            j=num/i #to calculate the second factor
            print '%d equals %d * %d' % (num,i,j)
            break #to move to the next number, the #first FOR
      else:        # else part of the loop
         print num, 'is a prime number'
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
...

itertools

Infinite Iterators:

  • count(10) --> 10 11 12 13 14 ...
  • cycle('ABCD') --> A B C D A B C D
  • repeat(10, 3) --> 10 10 10 (if no 2nd arg, repeat without stopping)

Iterators terminating on the shortest input sequence:

  • compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
  • takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
  • dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
  • groupby(iterable, keyfunc) makes an iterator that returns consecutive keys and groups from the iterable
  • [Function from default module] map(pow, (2,3,10), (5,2,3,4)) --> 32 9 1000. Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted
  • starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 ('prezipped' args)

Combinatoric generators:

  • product('ABCD', repeat=2) → AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
  • permutations('ABCD', 2) → AB AC AD BA BC BD CA CB CD DA DB DC
  • combinations('ABCD', 2) → AB AC AD BC BD CD [sorted]
  • combinations_with_replacement('ABCD', 2) → AA AB AC AD BB BC BD CC CD DD [sorted]

Numpy

infinity:

  • np.infty or -np.infty; np.inf or np.NINF.
  • np.isfinite(5)True; np.isfinite([5, -np.infty])array([ True, False], dtype=bool)
  • Same as above: np.isnan, isinf, isneginf

Python convention

wrap long operation

`` a = '1' \

+ '2'

``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment