本文共 1755 字,大约阅读时间需要 5 分钟。
学习内容:
1.学习了模块,包括制作自己的模块
2.学习了数据结构的列表、元组
[root@reed 0429]# for i in $(ls *.py);do echo "cat $i";cat $i;echo; donecat mymodule_dem2.py#!/usr/bin/pythonfrom mymodule import sayhi,version,datesayhi()print versionprint datecat mymodule_dem.py#!/usr/bin/pythonimport mymodulemymodule.sayhi()print 'version is',mymodule.versionprint 'date is ',mymodule.datecat mymodule.py#!/usr/bin/pythondef sayhi(): print 'hello,this is my module speaking'version='1.0'date='20140429'cat print_tuple.py#!/usr/bin/pythonage=27name='reed'print '%s is %d years old'%(name,age)cat using_list.py#!/usr/bin/pythonshoplist=['apple','mango','carrot','banana']print 'i have',len(shoplist),'items to purchase.'print 'these items are:',for item in shoplist: print item,print '\ni alse have to buy rice.'shoplist.append('rice')print 'my shopping list is now',shoplistprint 'i will sort my list'shoplist.sort()print 'sorted shopping list is',shoplistprint 'the first item i will buy is',shoplist[0]olditem=shoplist[0]del shoplist[0]print 'i bought the',olditemprint 'my shopping list is now',shoplistcat using_name.py#!/usr/bin/pythonif __name__=='__main__': print 'this program is beging run by itself'else: print 'i am being imported from another module'cat using_sys.py#!/usr/bin/pythonimport sysprint 'the command line arguments are:'for i in sys.argv: print iprint '\n\nThe PYTHON PATH is',sys.path,'\n'cat using_tuple.py#!/usr/bin/pythonzoo=('wolf','elephant','penguin')print 'number of animals in the zoo is',len(zoo)new_zoo=('monkey','dolphin',zoo)print 'number of animals in the zoo is',len(new_zoo)print 'all animals in new zoo are',new_zooprint 'animals brought from old zoo are',new_zoo[2]print 'animals brought from old zoo is',new_zoo[2][2]
转载地址:http://kapla.baihongyu.com/