Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
Last active February 28, 2016 16:29
Show Gist options
  • Save mjgpy3/d435ee4a67a45b72253c to your computer and use it in GitHub Desktop.
Save mjgpy3/d435ee4a67a45b72253c to your computer and use it in GitHub Desktop.
CoolPythonStuff.mkd

List comprehensions

[i for i in xrange(1, 20) if i % 3 == 0]

Dot

foo.bar()

In Object Oriented programming, send the message "bar" to foo

Join

" x ".join(["42", "3", "1000", "81"])

Split

'42 x 3 x 1000 x 81'.split(' x ')

Negative indexing

>>> list = range(1, 6)
>>> list
[1, 2, 3, 4, 5]
>>> list[-1]
5

List slicing

>>> [1, 2, 3, 4, 5][2:5]
[3, 4, 5]
>>> [1, 2, 3, 4, 5][2:4]
[3, 4]
>>> [1, 2, 3, 4, 5][2:2]
[]
>>> [1, 2, 3, 4, 5][:2]
[1, 2]
>>> [1, 2, 3, 4, 5][:]
[1, 2, 3, 4, 5]
>>> [1, 2, 3, 4, 5][1:2]
[2]
>>> [1, 2, 3, 4, 5][1:3]
[2, 3]
>>> [1, 2, 3, 4, 5][2:-2]
[3]
>>> [1, 2, 3, 4, 5][2:]

Reverse a list

>>> a
[1, 2, 3, 99, 5]
>>> a[::-1]
[5, 99, 3, 2, 1]

Tuples

>>> (92, 35)
(92, 35)
>>> (92, 35)[0]
92
>>> (92, 35)[1]
35

Classes

>>> class Person:
...   def __init__(self, name, age):
...     self.name = name
...     self.age = age
...   def say_hello(self):
...     print "Hi, my name is " + self.name + ", I am " + str(self.age)
... 
>>> Person
<class __main__.Person at 0x7fbeadcfe390>
>>> you = Person("Stephen", 20)
>>> me = Person("Michael", 23)
>>> you.say_hello()
Hi, my name is Stephen, I am 20
>>> me.say_hello()
Hi, my name is Michael, I am 23
>>> you.name
'Stephen'
>>> me.age
23

Dictionaries

>>> def make_person(name, age):
...   return { 'name': name, 'age': age }
... 
>>> me = make_person('Michael', 23)
>>> me
{'age': 23, 'name': 'Michael'}
>>> you = make_person('Stephen', 20)
>>> you
{'age': 20, 'name': 'Stephen'}

Fibs examples

def n_fibs(n):
  fibs = [1, 1]

  if n <= 2:
    return fibs[:n]

  while n != 2:
    fibs.append(fibs[-1] + fibs[-2])
    n -= 1

  return fibs

def nth_fib(n):
  a, b = 1, 1

  while n != 1:
    a, b = b, b + a
    n -= 1

  return a

def nth_fib_recursive(n):
  if n <= 2:
    return 1

  return (nth_fib_recursive(n - 1) +
    nth_fib_recursive(n - 2))

print [nth_fib(n) for n in xrange(1, 20)]
print [nth_fib_recursive(n) for n in xrange(1, 20)]

print n_fibs(20)

Lambdas and High-Order functions (aka functions as values)

>>> def cmp1(a, b): return a - b
... 
>>> def cmp2(a, b): return b - a
... 
>>> sorted([1, 923235, 12, 11, 9, -9], cmp1)
[-9, 1, 9, 11, 12, 923235]
>>> sorted([1, 923235, 12, 11, 9, -9], cmp2)
[923235, 12, 11, 9, 1, -9]
>>> a = [(1, 2), (1, 1), (1, 9), (0, 7), (-1, 7)]
>>> def special(a, b):
...    if a[0] != b[0]:
...      return a[0] - b[0]
...    return a[1] - b[1]
... 
>>> sorted(a, special)
[(-1, 7), (0, 7), (1, 1), (1, 2), (1, 9)]
>>> sorted(a, lambda a, b: a[1] - b[1])
[(1, 1), (1, 2), (0, 7), (-1, 7), (1, 9)]
>>> def call_with_fn(value, fn):
...   return fn(value)
... 
>>> call_with_fn(40, lambda x: x + 2)
42
>>> def add_two(x): return x + 2
... 
>>> call_with_fn(40, add_two)
42
>>> add_two = lambda x: x + 2
>>> call_with_fn(40, add_two)
42
>>> def apply_twice(value, fn):
...   return fn(fn(value))
... 
>>> apply_twice(40, lambda x: x + 1)
42
@douggilliland
Copy link

I need someone to talk me through the lambdas. Functions as values is weird enough in C but this is painful to read.

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