x = [1, 2, 3, 'a', 'b', 'c'] x y = x y x.append("this") x x.append a_func = x.append a_func a_func("that") x type(x) a_class = type(x) a_class z = a_class() type(z) my_int = 42 my_float = 42.0 my_str = "42" my_bool = True my_int == my_float str(my_int) == my_str bool(my_int) == my_bool [my_int, my_float, my_str, my_bool] {'numbers': (my_int, my_float), 'others': (my_str, my_bool)} my_list = [1, 4.2, True, "Hello, world!"] len(my_list) my_str = my_list[-1] my_str len(my_str) a = my_str.split(',')[0] + '!' a a = my_list[1:3] a x = 42 40 < x < 99 1 <= x < 40 65 == 0x41 == 0b01000001 == ord('A') [x for x in range(10)] [x * 1.5 for x in range(10)] [x * 1.5 for x in range(10) if x > 4] {x: x * 1.5 for x in range(10)} "vehicle '{0}' has wheel count: {1}".format('car', 4) "a score of {:.2%}".format(.96578) "{:,}".format(1000000) "{:.2e}".format(1000000) def total(sub, tip, tax=0.06): return sub + (sub * tax) + tip total(10.0, 2.0) total(10.0, 2.0, 0.10) total(10.0, 2.0, tax=0.05) total(10.0, tax=0.09, tip=1.5) class Person(object): def __init__(self, name, age): self.name = name self.age = age def __str__(self): return "{n} is {a}".format(n=self.name, a=self.age) def __iadd__(self, years): self.age += years return self def __cmp__(self, other): return cmp(self.age, other.age) person = Person("John Smith", 42) print person person += 1 print person person > Person("Jane Doe", 42) class Foo(object): BAR = 1 def __init__(self, value): self.value = value # creation of self.value def __str__(self): text = "" text += str(self.value) # instance variable text += ', ' text += str(self.BAR) # class variable return text print Foo(2) def show_call(func): print "called {0}".format(func.__name__) return func class Foo(object): BAR = 1 @show_call def __init__(self, value): self.value = value # creation of self.value @show_call def __str__(self): text = "" text += str(self.value) # instance variable text += ', ' text += str(self.BAR) # class variable return text print Foo(2) from collections import Counter for char, count in Counter("The quick brown fox jumped over the lazy dog.").items(): if count > 1 and char != ' ': print char, count from itertools import permutations for permutation in permutations(('a', 'b', 'c')): print permutation >>> import requests >>> requests.get('https://api.github.com/user', auth=('user', 'pass')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json; charset=utf8' >>> r.encoding 'utf-8' >>> r.text u'{"type":"User"...' >>> r.json() {u'private_gists': 419, u'total_private_repos': 77} >>> from suds.client import Client >>> url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl' >>> client = Client(url) >>> print client Service (WebServiceTestBeanService) tns="http://test.server.enterprise.rhq.org/" Prefixes (1): ns0 = "http://test.server.enterprise.rhq.org/" Ports (1): (Soap) Methods: addPerson(Person person, ) echo(xs:string arg0, ) getList(xs:string str, xs:int length, ) getPercentBodyFat(xs:string name, xs:int height, xs:int weight) getPersonByName(Name name, ) hello() testExceptions() testListArg(xs:string[] list, ) testVoid() updatePerson(AnotherPerson person, name name, ) Types (23): Person Name Phone AnotherPerson >>> from sh import git, ls, wc >>> git(checkout="master") >>> print ls("-l") >>> longest_line = wc(__file__, "-L") >>> import serial >>> ser = serial.Serial('COM1') >>> ser.write("hello") >>> ser.close() >>> from veracity import Repository, WorkingCopy, Item >>> repo = Repository('veracity', remote='http://public.veracity-scm.com/repos/veracity') >>> print repo.name >>> print repo.users >>> print repo.branches >>> print repo.tags >>> work = repo.checkout("~/v/veracity") >>> work.delete() >>> work = WorkingCopy("~/v/veracity", repo='veracity') >>> work.update(branch='master') >>> item = Item('docs/GettingStarted.txt', work=work) >>> item.lock()