Cheat sheet of python summarize from http://cs231n.github.io/python-numpy-tutorial/

##basic data type##

  1. type(x)
  2. print(x)
  3. length of string: len(x)
  4. s.capitalize()
  5. s.upper
  6. s.rjust(n) #right-justify a string, n – length of new string
  7. s.center(n)
  8. s.replace(‘x’,’y’) #replace ‘x’ with ‘y’
  9. print(‘ world ‘.strip()) #strip

##container##

###list### xs=[1,2,3] print(xs) #1,2,3 access the element of list print(xs[0]) #1 print(xs[-1]) #count from the end of the list; print “3” xs.append(‘bar’) #xs become [1, 2, 3, ‘bar’] xs.pop() remove and return the last element of the list #xs become [1,2,3]

slicing nums=list(range(5)) #create a list of integer [0,1,2,3,4]

print(nums[2:4]) #get a slice from index 2 to index 4 (exclusive) return "[2,3]"

print(nums[2:]) #get a slice from index 2 to end, prints "[2,3,4]"

print(nums[:]), #get a slice of the whole list, prints "[0,1,2,3,4]"

print(nums[:-1]) #get a slice from -1 to end, prints "[0,1,2,3]"

nums[2:4]=[8,9] #assign a new sublist to a slice

print(nums) #pirnts "[0,1,8,9,4]"

###loops### list=[1,2,3] for item in list: print(item) #prints 1,2,3

access the index of each element

for idx, item in enumerate(list):
`	print ('#%d: %s' %(idx+1, item))`

square of a number: x**2

List comprehensions###

list=[0,1,2,3,4]
squares=[]
for x in list:
	squares.append(x**2)
print(squares)

list comprehensions

list=[0,1,2,3,4]
squares=[x**2 for x in list]
print(squares) #print [0,1,4,9, 16]

###Dictionaries###

(key, value) pairs

	d={'cat':'cute', 'dog':'fury'}
	print(d[cat]) #prints "cute"
	#set a new entry
	d['fish']='wet'
	print(d.get('monkey','null') #prints 'null'
	print(d.get('fish', 'null)) #prints 'wet'
	del d['fish'] #remove 'fish':'wet'
	print(d.get('fish','null') #prints 'null'

loops d={‘cat’:’cute’, ‘dog’:’fury’} for item in d: characters=d[item] print(‘A %s is ‘%s’ %(item, characters))

access to both key and value d={‘cat’:’cute’, ‘dog’:’fury’} for item, characters in d.items(): print(‘A %s is %s’ %(item, characters))

Dictionary Comprehensions: nums={1,2,3,4,5} even_square={x:x**2 for x in nums if x%2==0}

###Sets###

set: unordered, non-repeated

animals={'cat', 'dog'}
print('cat' in animals) #prints 'True'
print('fish' in animals) #prints 'False'
animals.add('fish') #add 'fish' to animals
print('fish' in animals) #prints 'True'
print(len(animals)) #prints "3"
animals.remove('cat')
print('cat' in animals) #prints 'False'

Loops: animals={‘cat’, ‘dog’, ‘fish’} for idx, animal in animals: print(‘#%d: %s’ %(idx+1, animal))

Set comprehension

from math import sqrt
nums={int(sqrt(x)) for x in range(30)}
print(nums)

###Tuples###

tuple: ordered list of values

d={(x, x+1) for x in range(10)}
t=(5,6)
print(type(t)) #prints "<class 'tuple'>"
print(d[t]) #print "5"
print(d[1,2]) #print "1"

##summary##

  1. Functions

    def sign(x): if x>0 return ‘positive’ else if x<0 return ‘negative’ else return ‘zero’

    for x in [-1, 0 ,1] print(sign(x))

    #prints “negative”, “zero”, “positive”

  2. take optional keyword def hello(name, loud=False): if loud: print(‘HELLO, %s!’ %name.UPPER()) else: print(‘Hello, %s’ %name)

    hello(‘Bob’) #prints “Hello, Bob’ hello(‘Fred’, loud=True) #prints “HELLO, FRED”

#Classes#

class Greeter(object):
	def __init__(self, name):
		self.name=name #create an instance variable

def greet(self, loud=False)
	if loud:
		print('HELLO, %s!' %name.upper())
	else:
		print('Hello, %s' %name)
g=Greeter('Fred')
g.greet() #prints "Hello, Fred"
g.greet(loud=True) #prints "HELLO, FRED"

#Numpy#