您的位置:首页 > 编程语言 > Python开发

python基本语法

2015-07-29 18:17 597 查看
#!/usr/bin/python
# -*- coding=UTF8 -*-

import os
import commands
import re
from optparse import OptionParser

def mytest():
a=input("input a number:    ")
s=[]
if a < 10:
for i in range(a):
s.append(i)
elif a < 100:
for i in range(a)[0:a:10]:
s.append(i)
else:
while a > 0:
a=a/100
s.append(a)
return s

print([e for e in mytest() if e>1])  #重点

res=map((lambda x: x+3),mytest())
del res[0]
print('re=%s retype=%s' % (str(res),str(type(res))))

print(dir("")) #打印string的全部属性和方法

m = re.search('[0-9]','abcd4ef')    #参考http://www.cnblogs.com/vamei/archive/2012/08/31/2661870.html
print(m.group(0))

class myclass(object):
cname="myclass"
def __init__(self):
print("myclass init")
return
def show(self,str):
self.str=str
print(str)
return

myc=myclass()
myc.show("show")
print(myc.str)
print(myc.cname)

ret=os.system("ls")
print(ret)
print os.popen('ls').readlines()
files=commands.getoutput("ls")
files=files.split('\n')
print(files)

def main():
try:
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-f", "--file", dest="filename",
help="read data from FILENAME")
(options, args) = parser.parse_args()
if options.filename:
print "reading %s..." % options.filename
fpath=options.filename
if os.path.exists(fpath)==True:
f=open(fpath,"r+")
print(f.readlines())
f.write("#writefile")
f.close()
except KeyboardInterrupt:
print "用户中断"
except Exception, e:
print e

return

if __name__ == "__main__":
main()


推荐python教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: