也不知道自己哪根筋搭错了,放着好好的电视剧不专心看,非要看看python是什么东西。并写一个hello world程序。不知道这个让很多高手一用就喜欢的脚本语言到底有多好用。
__author__="zhangyt"
__date__ ="$2009-3-12 23:46:47$"
if __name__ == "__main__":
print "汉字";
把上面的内容保存成hello.py文件,用python hello.py 结果会提示中文错误。
# -*- coding:gb2312 -*-
__author__="zhangyt"
__date__ ="$2009-3-12 23:46:47$"
if __name__ == "__main__":
print "汉字";
把文件内容第一行加一句 # -*- coding:gb2312 -*-
就能运行中文了。
具体的原因我还没来得及看, 今天先睡觉了。
2009-03-15
1.python清屏
import os
os.system('clear')
这是在linux下的。 在windows下只需要把clear换成cls
2009-10-13
1.总是听说 闭包 这个说法
,但不知道是什么意思,今天总算有耐心查一查,并尝试一下。按我的理解,闭包应该是编译器或者是解释器的支持功能。例如python支持闭包,是python支持。例如有如下代码
def ExFunc(n):
sum=n
def InFunc():
return sum + 1;
return InFunc
可见函数ExFunc中包含有InFunc并返回它。而InFunc又引用了外部变量sum是在ExFunc中的。那么解释器执行ExFunc时,其实返回的是InFunc,而InFunc又不包含sum变量,岂不会是出错。结果不出错就需要连通外带环境中的sum一同解释执行,这就是 “Closure”,封闭执行,其实叫做封装闭合执行更为恰当。封装了上下文
并作为一个单独是实例执行
。
理解起来可真是很辛苦。不过进行函数编程的人,遇到类似情况,闭包便很自然的出现。理解的深刻,就要身临其境。
2009-11-25
早就听说并且尝试过python官网,除了首页,其它的都打不开了。而且到现在依然打不开。作为很多人钟爱的一门动态编程语言,在天朝遭此厄运,是让不仅仅包括程序员在内的有志之士感到惋惜的。不过今天同时也发现了一个地址,可以下载,地址不公布。怕被那些狗贼看见(在天朝的古代,某些人经常被称为“狗贼”)。
2010-07-02
python 操作文件的例子 取自(byte of python)
读与写
#!/usr/bin/python
poem = '''\
programming is fun
when the work is done
if you wanna make your work alse fun:
use python!
'''
f = file('poem.txt','w') #open for writing
f.write(poem) #write the text to file
f.close()
f = file('poem.txt')
#if no mode is specified read modeis assumed by default
while True:
line = f.readline()
if len(line) == 0:
break
print line,
#notiece comma to avoid automatic new line added by python
f.close()
对象存储
#!/usr/bin/python
import cPickle as p
shoplistfile = 'shoplist.data'
shoplist = ['apple','mango','carrot']
f = file(shoplistfile,'w')
p.dump(shoplist,f)
f.close()
del shoplist
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
python 字符串操作 自写
#!/usr/bin/python
# -*- coding:UTF-8 -*-
print '\n复制'
s1 = 'haha'
s2 = s1
print s2
s1 = 'hehe'
print s2
print '\n连接'
s3 = s1 + s2
print s3
print '\n查找位置'
n = s3.index('a')
print n
print '\n比较'
print cmp(s1,s2)
print '\n扫描字符串是否包含指定的字符'
s4 = '12345678'
s5 = '456'
print len(s4 and s5)
print '\n字符串长度'
print len('strlen')
print '\n转换成大写字母'
s6 = 'ytzhang'
s6 = s6.upper()
print s6
print '\n追加指定长度的字符串'
s7 = '12345'
s8 = 'absdef'
s7 += s8[0:3]
print s7
print '\n字符串查找'
s9 = 'abcdefg'
s10 = 'cde'
print s9.find(s10)
分享到:
2009-03-13 00:30
浏览 1130
评论