Skip to content

Instantly share code, notes, and snippets.

class Person {
String name;
int age;
Person(this.name, this.age);
void lieAboutMyAge() {
print("My name is $name, my age is ${age - 10}");
}
import os
try:
working_dir = os.getcdw()
print(working_dir)
except:
print('error')
from time import sleep
while True:
try:
print("Try and stop me")
sleep(1)
except Exception:
print("Ok I'll stop!")
from time import sleep
while True:
try:
print("Try and stop me")
sleep(1)
except:
print("Don't.. stop.. me now!")
try:
...
except:
pass
>>> class Card:
... __slots__ = 'rank', 'suite'
... def __init__(self, rank, suite):
... self.rank = rank
... self.suite = suite
...
>>> qh = Card('queen', 'hearts')
>>> # Calculate all profucs of an input
>>> list(itertools.product('abc', repeat=2))
[('a', 'a'), ('a', 'b'), ('a', 'c'),
('b', 'a'), ('b', 'b'), ('b', 'c'),
('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> # Calculate all permutations
>>> list(itertools.permutations('abc'))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'),
('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
[
{'name': 'Ed', 'age': 24},
{'name': 'Jane', 'age': 34},
{'name': 'Janet','age': 34},
{'name': 'John', 'age': 32},
{'name': 'John', 'age': 64},
{'name': 'John', 'age': 99},
{'name': 'Sara', 'age': 64}
]
import operator
people.sort(key=operator.itemgetter('age'))
people.sort(key=operator.itemgetter('name'))
people = [
{ 'name': 'John', "age": 64 },
{ 'name': 'Janet', "age": 34 },
{ 'name': 'Ed', "age": 24 },
{ 'name': 'Sara', "age": 64 },
{ 'name': 'John', "age": 32 },
{ 'name': 'Jane', "age": 34 },
{ 'name': 'John', "age": 99 },
]