1. Simple expression
3e-10 #10的-10次 10//3 #10/3的整数部分 10%3 #余数
2.Sets
x = {} #dictionary not set
x = set() #set
sum({1,2,3})
sum({1,2,3}, 3)
num = {1,2,3}
2 in num #return True
2 not in num #return False
{1,2,3} | {3,4,5} #set union
{1,2} & {1,2,3} # set intersection
S = {1,2,3}
S.add(4) #add element
S.remove(2) #remove element
S.update({4,5,6}) #the argument should be set
S.intersection_update({5,6,7,8})
U = S.copy() #we can copy S to U in order to avoid operating S
{2*x for x in {1,2,3}} #set comprehension
S = {1, 2, 3, 4}
T = {3, 4, 5, 6}
S_intersect_T = { x for x in S for y in T if x == y } #intersect using set comprehension
3.Lists
L = list() #2 kinds of list initialization L = [] L = [1,2,3] sum(L) [1,2,3] + ['a','b'] dictionary = {1:'a', 2:'b'} L = list(dictionary) # the entries of list is the keys of dictionary L[1] #obtaining elements of a list by indexing L[1:10] # obtaining elements (index 1 to 10) L[1:10:2] # return [1,3,5,7,9] L.append(x) # add x to the end of the list L.pop(i) #delete the ith element and return the remainder
4.Dictionary
d = {} # initialization
d = dict()
{1:2, 1:3} #return {1:3}
square_dict = {i:i**2 for i in range(100)}
d = {1:1, 2:2}
1 in d #return True p.s. do not use .has_key() since it is no longer used in Python 3.x
1 in d.keys() # return True
d.items()
d.values()
d.keys()
5.Others
list(range(3)) #return [0, 1, 2] 3 is not included list(zip([1,2,3], [2,3,4])) # return[(1, 2), (2, 3), (3, 4)]