当前位置:Gxlcms > Python > python点滴

python点滴

时间:2021-07-01 10:21:17 帮助过:34人阅读

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 < b):
print 'a < b'
else:
print '$%$#$%'
for each :
array = [1,2,3,4]
total = 0
for i in array:
total +=i
print total
while:
while 1 > 2
pass
3、数据类型:
链表:array=[1,2,3,4] print array result:[1,2,3,4]
散列:
print set ([1,2,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 in dict.keys():
print '%s%s'%(key,dict[key]) result:ww36 ls25 zs12
4、文件读写:
f = file("D:\work\a.txt","r")
print f.read()
f.close() #文件流需关闭
with open ("D:\work\a.txt","r") as f:
print f.read()
with open ("D:\work\b.txt","w") as f:
f.write("i am ok!")

人气教程排行