Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created July 4, 2012 15:34
Show Gist options
  • Save kristopherjohnson/3047911 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3047911 to your computer and use it in GitHub Desktop.
Python snippets
#!/usr/bin/env python
if name == 'Foo':
foo()
elif name == "Bar':
bar()
else:
wtf()
numbers = ['one', 'two', 'three']
for x in numbers:
do_something(x);
for i in range(len(numbers)):
do_something(numbers[i])
def a_function(x):
print x
print [n*3 for n in range(10) if n%2 == 1] # prints "[3, 9, 15, 21, 27]"
# create a matrix with five rows and six columns, initialized with zeros
matrix_5_by_6 = [[0 for col in range(6)] for row in range(5)]
f = open("myfile.txt")
for line in f:
process(line)
import re
result = re.match(pattern, string)
@kristopherjohnson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment