1、print語句:
print 'hello world!'
print "hello world!"
print 'hello %s!'%('world')
2、流程控制:
if:
a = 1;b = 2
if (a == b):
print 'a = b'
elif (a > b):
print 'a > b'
elif (a print 'a elif (a print 'a print '$%$#$%'
for each :
array = [1,2,3,4]
total = 0
for i in array:
total +=i
for i in array:
total +=i > 2
pass
3、資料型態:
鍊錶:array=[1,2,3,4] print array result:[1,2,3,4]
雜湊:
print set ([11,2,2,2,2,2,5 列連列: 3,4,4]) result:set([1,2,3,4])
set1 = set([1,2,3,4,5]) set2 = set([1,2,3,4 ,6])
print set1-set2 result:set([5])
print set1&set2 result:set([1,2,3,4])
print set1|set2 result:set([1,2,3, 4,5,6])
字典:
dict = {}
dict['zs'] = 12
dict['ls'] = 25
dict['ww'] = 36
for key inp. ):
print '%s%s'%(key,dict[key]) result:ww36 ls25 zs12
4、檔案讀取:
f = file("D:worka.txt","r") print f.read()
f.close() #檔案流需要關閉
with open ("D:worka.txt","r") as f:
print f.read()
with open ("D:workb .txt","w") as f:
f.write("i am ok!")