Create a list of the first ten powers of two:
powersof2 = [] for x in range(1, 11): powersof2.append(2 ** x) >>> print powersof2 [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
Powers of two example with map and anonymous lambda function:
powersof2 = map(lambda x:2 ** x, range(1,11)) >>> print powersof2 [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
Powers of two example with list comprehension:
powersof2 = [ 2 ** x for x in range(1, 11) ] >>> print powersof2 [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
sentence = "She sells seashells by the seashore" wordlist = [] for word in sentence.split(): if word.lower().startswith("s"): wordlist.append(word) print wordlist ['She', 'sells', 'seashells', 'seashore']
wordlist = filter(lambda word:word.lower().startswith("s"), sentence.split()) >>> print wordlist ['She', 'sells', 'seashells', 'seashore']
wordlist = [ word for word in sentence.split() if word.lower().startswith("s") ] >>> print wordlist ['She', 'sells', 'seashells', 'seashore']
>>> [ letter * number for number in [1,2,3] for letter in ["a","b","c"] ] ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc']
thelist = [] for number in [1,2,3]: for letter in ["a","b","c"]: thelist.append(letter * number)
Example from Python Course
Celsius = [39.2, 36.5, 37.3, 37.8] Fahrenheit = [ "%.2f" % ((float(9)/5)*x + 32) for x in Celsius ] print Fahrenheit ['102.56', '97.70', '99.14', '100.04']
driveletters = [ "%s:" % letter for letter in string.ascii_uppercase ] driveletters[:len(driveletters)/2] ['A:', 'B:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:', 'M:'] driveletters[len(driveletters)/2:] ['N:', 'O:', 'P:', 'Q:', 'R:', 'S:', 'T:', 'U:', 'V:', 'W:', 'X:', 'Y:', 'Z:']
from pprint import pprint uniqips = set( [ line.split()[0] for line in open("access.log") ] ) pprint(list(uniqips)[:5]) ['180.76.5.65', '74.125.19.39', '220.181.51.109', '123.125.71.75', '178.255.215.65']
Presenter: | Shawn K. O'Shea |
---|
Presented to: | GNHLUG's PySIG |
---|
Presented on: | July 26, 2012 |
---|
Latest version: | You can find the latest version of this presentation on my website: http://eth0.net/ |
---|
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |