Start New Project
$ django-admin startproject [name]
$ cd [name]
$ python manage.py startapp [app]
Run server
$ python manage.py runserver
Start New Project
$ django-admin startproject [name]
$ cd [name]
$ python manage.py startapp [app]
Run server
$ python manage.py runserver
# Command 0
# 2to3 on command line
$ 2to3 -w file.py
# Command 1
# Importing modules
import math
# or, import one item
from math import sin
# Command 2
# Reloading modules
reload(math)
# Command 3
# Executing a script
execfile("my_script.py")
# Command 4
# Evaluating code
eval("a=2*3")
# Command 8
# Asserting values, example asserting that a variable is a string
assert type(name) is StringType
# Command 9
# Mapping functions onto lists
a = [1,2,3,4,5]
map(lambda x:2*x, a)
# This returns [2,4,6,8,10]
# Command 11
# Reduction functions can be applied too
reduce(lambda x,y:x+y, a)
# This returns 15
# Command 12
# You can filter out specific values from a list
filter(lambda x:x % 2 == 0, a)
# This returns [2, 4]
# Command 13
# You can ennumerate a list
b = ['a','b','c','d']
list(ennumerate(b, start=1))
# This returns [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
# Command 16
# You can check to see what class an object is an instance of
isinstance(a, list)
# Command 18, 19, 20
# You can investigate the variable space of your program
globals()
locals()
vars()
# Command 26
# You can use ranges with for loops
for counter in range(10):
print count
# Command 27
# For large ranges, you can use xrange
for counter in xrange(10000):
print counter
# Command 29
# Sorting functions is easy
list1 = ['df', 'er', 'ab']
sorted(list1)
# Command 30
# Summing values
sum(a)
# This returns 15
# Command 37
# Pickling data objects
import pickle
output = open('data.pk1', 'wb')
data1 = {'a': [1,2,3,4],
'b', ('String')
pickle.dump(data1, output)
output.close()
# Command 40
# You can ask a user for input
person = input("Please enter your name: ")
# Command 43
# Slices
list2 = [1,2,3,4,5,6,7,8,9]
list2[1:3]
# This returns [2,3,4]
list2[:2]
# This returns [1,2,3]
list2[6:]
# This returns [7,8,9]
# Command 50
# String concatenation
str1 = "a," + str(1) + ", b"
# This returns "a, 1, b"