Skip to content

Instantly share code, notes, and snippets.

@KavenTheriault
Last active November 29, 2018 16:15
Show Gist options
  • Select an option

  • Save KavenTheriault/8f44efcb42d09550b0e5f16dce76d4e2 to your computer and use it in GitHub Desktop.

Select an option

Save KavenTheriault/8f44efcb42d09550b0e5f16dce76d4e2 to your computer and use it in GitHub Desktop.
Python notes

Named tuples

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])

point1 = Point(24, 67)
print(point1.x)
print(point1.y)

Dataclasses

@dataclasses.dataclass
class Person():
    name: Optional[int] = None
    age: Optional[int] = None

Lambda

x = lambda a : a + 10
print(x(5))
# 15

Map

def myfunc(a, b):
return a + b

x = map(myfunc, ('apple', 'banana', 'cherry'), ('orange', 'lemon', 'pineapple'))
# ['appleorange', 'bananalemon', 'cherrypineapple']

List Comprehensions

>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]

>>> data = [('a','b'), ('c','d')]
>>> [x[0] for x in data]
['a', 'c']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment