1.打印hello world
#coding:gbk
def main():
print("hello world")
if __name__ == '__main__':
main()
2.计算从1到100的和
#coding:gbk
def calc(n):
total = 0
for i in range(n):
total += i
return total
def calc_1(n):
total = 0
i = 0
while i <= 100:
total += i;
i += 1
return total
def calc_2(n):
total = 0
for i in xrange(0, n):
total += i
return total
if __name__ == '__main__':
total = calc(100)
print(total)
total1 = calc_1(100)
print(total)
total2 = calc_2(100)
print(total)
3.读写文件操作
#coding:gbk
def file_cat(f_name):
fd = open(f_name, "rb+")
for l in fd.readlines():
print(l)
fd.close()
def file_cat_1(f_name):
fd = open(f_name, "rb+")
buf = fd.read();
if buf != None and len(buf) > 0:
print(buf)
fd.close()
def file_wite(f_name):
fd = open(f_name, "wb+")
fd.write("hello world\n")
fd.write("heihei")
fd.close()
if __name__ == '__main__':
f_name = "tmp"
file_wite(f_name)
file_cat(f_name)
file_cat_1(f_name)
4.调用shell脚本
#coding:gbk
import os
import commands
def do_cmd(cmd_str):
return os.system(cmd_str)
def do_cmd_1(cmd_str):
return os.popen(cmd_str).read()
def do_cmd_2(cmd_str):
return commands.getstatusoutput(cmd_str)
if __name__ == '__main__':
ret = do_cmd("ls -l")
res = do_cmd_1("ls -l")
ret, res = do_cmd_2("ls -l")
print(ret)
print(res)
print("ret = %d, res = %s"%(ret, res))
5.线程操作
#coding:gbk
import threading
class MyThread(threading.Thread):
"""docstring for MyThread"""
def __init__(self, i, n):
threading.Thread.__init__(self)
self.index = i
self.n = n
def run(self):
total = 0
for i in xrange(1, self.n):
total += i
print("%d thread calc %d from 1 total is %d\n"%(self.index, self.n, total))
if __name__ == '__main__':
n = 10
init_n = 100
ths = []
for x in xrange(1, n):
th = MyThread(x, init_n)
init_n += 500
ths.append(th)
for th in ths:
th.start()
for th in ths:
th.join()
6.其它基础操作
<pre name="code" class="python">#coding:gbk
def map_process():
maps = {"1":"bb", "2":"cc", "3":"dd"}
maps["4"] = "44"
for k in maps.keys():
print("key:%s, value:%s"%(k, maps[k]))
def vec_process():
vec = ["a", "b", "c", "d"]
vec.append("e")
vec.append("f")
vec.remove("b")
for str in vec:
print(str)
def str_process():
str_test = "hello world"
print("str:%s, len:%d"%(str_test, len(str_test)))
str_test = "123"
n = int(str_test)
n += 1
print("str_test:%s, n:%d, str(n):%s"%(str_test, n, str(n)))
if __name__ == '__main__':
map_process()
print("++++++++++++++++++++++++++++")
vec_process()
print("++++++++++++++++++++++++++++")
str_process()
7.正则表达式操作
#coding:gbk
import re
def regex_match(regex_str, pat_str):
return re.match(regex_str, pat_str) != None
def regex_search(regex_str, pat_str):
return re.search(regex_str, pat_str)
def regex_search_all(regex_str, pat_str):
return re.findall(regex_str, pat_str)
def regex_test():
regex_str = r"ab+"
regex_search_str = r"bc"
pat_str = "abbcbbc"
res = regex_match(regex_str, pat_str)
print("res = %d"%res)
res_obj = regex_search(regex_search_str, pat_str)
if res_obj != None:
print("search %s"%(res_obj.group()))
else:
print("can not search")
res_obj = regex_search_all(regex_search_str, pat_str)
if res_obj != None:
print "search ",res_obj
else:
print("can not search")
if __name__ == '__main__':
regex_test()