1. Dictionary
dic = {'lilei':90,'lily':100,'sam':57, ''tom" :90}
for key in dic
print dic[key]
print dic.keys(), return all keys
dic.values(), return all values
dic.items(), return all element
dic.clear(), clear dic, dic become {}
del dic['tom'] , delete 'tom' in dic, del is the keyword
2. file
f = open(filename, mode), "r" read, "w" write
content = f.read(N) , read N bytes data
content = f.readline(), read one line
content = f.readlines(), read all the lines
f.write('I like apple'), write to file
f.close()
3. range(), enumerate(), zip()
S = 'abcdefghijk'
for i in range(0,len(S),2):
print S[i]
for (index,char) in enumerate(S):
print index
print char
ta =[1,2,3]
tb = [9,8,7]
tc = ['a','b','c']
for (a,b,c) in zip(ta,tb,tc)
print(a,b,c)
ta = [1,2,3]
tb = [9,8,7]
zipped = zip(ta,tb)
print(zipped)
na,nb = zip(*zipped)
print(na,nb)
4. lambda
func = lambda x,y: x+ y
print func(3,4)
def teset(f,a,b):
print 'test'
print f(a,b)
test(func,3,5)
test((lambda x: x+3),[1,3,5,6])
5. map
re = map((lambda x,y: x+y),[1,2,3],[6,7,9])
6. filter
def func(a)
if a> 100:
return True
else:
return False
print filter(func,[10,56,101,500])
7. reduce
print reduce((lambda x,y: x+y),[1,2,5,7,9])
8. debug
re = iter(range(5))
try:
for i in range(100):
print re.next()
except StopIteration:
print 'Here is end ',i
print 'HaHaHa'
try:
...
except error1:
...
except error2:
...
else:
...
finally:
...
Thursday, October 3, 2013
Wednesday, October 2, 2013
Learning Python - part one
1. print 'Hello World'
a = 10
print a
print type(a)
a = 1.3
print a
print type(a)
basic type:
a = 10 int a =1.3 float a = True True/False a = 'Hello' string
type() - return data type
2. Sequence:
Tuple : s1 = (2, 1.3, 'love',9,12,false) , element can not be change
List: s2 = [True, 5, 'smile'] , element can change
print s1[0]
print s2[2]
s2[1] = 3.0
print s2
print s1[:5] -- from start to element 4
print s1[2:] -- from element 2 to last
print s1[0:5:2] -- from index 0 to element 4 , every 2 to get a element, such as element 0,2,4
print s1[2:0:-1] -- from element 2 to element 1
print s1[-1] -- last element
print s1[-3] -- the last third element
3. If else
i = 1
if i>0:
print 'positive i'
i = i+1
elif i ==0:
print 'i is 0'
i = i *10
else:
print 'negative i'
i = i -1
print 'new i:',i
4. for, while, continue, break
for a in [3,4.4, 'lift']:
print a
while i< 10:
print i
i = i+1
5. function
def square_sum(a,b)
c = a**2 + b ** 2
return c
print square_sum(3,4)
6. class , object
class Bird(object)
have_feather = True
way_of_reproduction = 'egg'
def move(self,dx,dy):
position =[0,0]
position[0] = position[0]+dx
position[1] = position[1] +dy
return position
summer = Bird()
print 'after move:', summer.move(5,8)
class Chicken(Bird):
way_of_move = 'walk'
possible_in_KFC = True
summer = Chicken()
print summer.have_feather
print summer.move(5,6)
7. dir(), help()
print dir(list)
print help(list)
n1 = [1,2,5,3,5]
n1.count(5), how many 5
n1.index(3), index of the first 3
n1.append(6), add 6 to the list
n1.sort(), sort
n1.pop(), remove the last element and return it
n1.remove(2), remove the first 2
n1.insert(0,9), insert 9 to position 0
a = 10
print a
print type(a)
a = 1.3
print a
print type(a)
basic type:
a = 10 int a =1.3 float a = True True/False a = 'Hello' string
type() - return data type
2. Sequence:
Tuple : s1 = (2, 1.3, 'love',9,12,false) , element can not be change
List: s2 = [True, 5, 'smile'] , element can change
print s1[0]
print s2[2]
s2[1] = 3.0
print s2
print s1[:5] -- from start to element 4
print s1[2:] -- from element 2 to last
print s1[0:5:2] -- from index 0 to element 4 , every 2 to get a element, such as element 0,2,4
print s1[2:0:-1] -- from element 2 to element 1
print s1[-1] -- last element
print s1[-3] -- the last third element
3. If else
i = 1
if i>0:
print 'positive i'
i = i+1
elif i ==0:
print 'i is 0'
i = i *10
else:
print 'negative i'
i = i -1
print 'new i:',i
4. for, while, continue, break
for a in [3,4.4, 'lift']:
print a
while i< 10:
print i
i = i+1
5. function
def square_sum(a,b)
c = a**2 + b ** 2
return c
print square_sum(3,4)
6. class , object
class Bird(object)
have_feather = True
way_of_reproduction = 'egg'
def move(self,dx,dy):
position =[0,0]
position[0] = position[0]+dx
position[1] = position[1] +dy
return position
summer = Bird()
print 'after move:', summer.move(5,8)
class Chicken(Bird):
way_of_move = 'walk'
possible_in_KFC = True
summer = Chicken()
print summer.have_feather
print summer.move(5,6)
7. dir(), help()
print dir(list)
print help(list)
n1 = [1,2,5,3,5]
n1.count(5), how many 5
n1.index(3), index of the first 3
n1.append(6), add 6 to the list
n1.sort(), sort
n1.pop(), remove the last element and return it
n1.remove(2), remove the first 2
n1.insert(0,9), insert 9 to position 0
Subscribe to:
Posts (Atom)