>>>a = 23
>>>b = 12
>>>temp = a
>>>a = b
>>>b = temp
>>>print a, b
12, 23
!python
>>>a, b = 23, 12
>>>a, b = b, a
>>>print a, b
12, 23
!python
>>>super_users = get_all_superusers_for_organization(org_name)
>>>if len(super_users) > 0:
>>>    print(u"Super users are {}".format(super_users))
>>>else:
>>>    print(u"No super users")
!python
>>>super_users = get_all_superusers_for_organization(org_name)
>>>if super_users:
>>>    print(u"Super users are {}".format(super_users))
>>>else:
>>>    print(u"No super users")
!python
>>>langs = ["Python", "go", "lua"]
>>>index = 0
>>>length = len(langs)
>>>while index < length:
>>>    print(index, langs[index])
>>>    index += 1
!python
>>>langs = ["Python", "go", "lua"]
>>>for index, value in enumerate(langs):
>>>    print(index, value)
!python
>>>if user.is_superadmin or user.is_siteadmin or user.is_staff:
>>>    # Grant Access to some page
!python
>>>if any([user.is_superadmin, user.is_siteadmin, user.is_staff]):
>>>    # Grant Access to some page
!python
>>>if user.is_superadmin and user.is_siteadmin and user.is_staff:
>>>    # Grant Access to admin page
!python
>>>if all([user.is_superadmin, user.is_siteadmin, user.is_staff]):
>>>    # Grant Access to admin page
!python
>>>status_code = requests.post("someurl", params={"foo": "bar"}).status_code
>>>if status_code >= 200 and status_code <= 299:
>>>    print("HTTP call is success")
!python
>>>status_code = request.post("someurl", params={"foo": "bar"}).status_code
>>>if 200 <= status_code <= 299:
>>>    print("HTTP call is success")
!python
>>>250 + 6 is 256
True
>>>250 + 7 is 257
False
!python
>>>250 + 6 == 256
True
>>>250 + 7 == 257
True
!python
>>>langs = ["Python", "Go", "Lua"]
>>>new_langs = langs
>>>new_langs.append("Javascript")
>>>print(langs, new_langs)
(['Python', 'Go', 'Lua', 'Javascript'], ['Python', 'Go', 'Lua', 'Javascript'])
!python
>>>langs = ["Python", "Go", "Lua"]
>>>new_langs = langs[:]
>>>new_langs.append("Javascript")
>>>print(langs, new_langs)
(['Python', 'Go', 'Lua'], ['Python', 'Go', 'Lua', 'Javascript'])
!python
>>>nos = [1, 4, 5, 6]
>>>evens = []
>>>for no in nos:
>>>    if no % 2 == 0:
>>>        evens.append(no)
>>>print(evens)
[4, 6]
!python
>>>nos = [1, 4, 5, 6]
>>>evens = [no for no in nos if no % 2 == 0]
>>>print(evens)
[4, 6]
!python
>>>def foo():
>>>    return "foo"
>>>def bar(names=[]):
>>>    names.append(foo())
>>>    return names
>>>bar()
["foo"]
>>>bar()
["foo", "foo"]
!python
>>>def bar(names=None):
>>>   name = foo()
>>>   if not names:
>>>       return [name]
>>>   names.append(name)
>>>   return names
>>>bar()
['foo']
>>>bar()
['foo']
!python
>>>d = {'foo': 'bar'}
>>>if 'baz' in d:
>>>    temp = d['baz']
>>>else:
>>>    temp = ""
!python
>>>d = {'foo': 'bar'}
>>>temp = d.get('baz', "")
!python
>>>words = ["a", "the", "foo", "bar", "foo"]
>>>frequency = {}
>>>for word in words:
>>>    if word in frequency:
>>>        frequency[word] += 1
>>>    else:
>>>        frequency[word] = 1
>>>print(frequency)
{'a': 1, 'the': 1, 'foo': 2, 'bar': 1}
!python
>>>from collections import Counter
>>>frequency = Counter(words)
>>>print(frequency)
Counter({'foo': 2, 'a': 1, 'the': 1, 'bar': 1})
!python
#Look before you leap
>>>d = {'foo': 'bar'}
>>>if 'baz' in d:
>>>    temp = d['baz']
>>>else:
>>>    raise KeyError("baz is missing")
!python
#Easier to Ask for Forgiveness than Permission
>>>try:
>>>    temp = d["baz"]
>>>except KeyError:
>>>    raise KeyError("baz is missing")
!python
>>>f = open('foo.txt')
>>>print(f.read())
>>>f.close()
!python
>>>with open('foo.txt') as f:
>>>    print(f.read())
!python
>>>with open('foo.txt') as f:
>>>    for line in f.readlines():
>>>        print(line)
!python
>>>with open('foo.txt') as f:
>>>     for line in f:
>>>         print(line)
!python
>>>class Person(object):
>>>    def __init__(self, name, email):
>>>        self.name = name
>>>        self.email = email
>>>p = Person("kracekumar", "[email protected]")
>>>print(p.name, p.email)
('kracekumar', '[email protected]')
!python
>>>from collections import namedtuple
>>>Person = namedtuple('Person', ['name', 'email'])
>>>p = Person("kracekumar", "[email protected]")
>>>print(p.name, p.email)
('kracekumar', '[email protected]')
!python
>>>try:
>>>   foo()
>>>except:
>>>   print("something") # sometimes people write pass. PURE EVIL !
!python
>>>try:
>>>   foo()
>>>except (IndexError, KeyError) as e:
>>>    print("something went wrong")
- 
Email: [email protected] 
- 
Twitter: @kracetheking