[简明python教程]学习笔记2014-04-29 23:41:40

学习内容:

1.学习了模块,包括制作自己的模块

2.学习了数据结构的列表、元组

[root@reed 0429]# for i in $(ls *.py);do echo "cat $i";cat $i;echo; done
cat mymodule_dem2.py
#!/usr/bin/python
from mymodule import sayhi,version,date
sayhi()
print version
print date
cat mymodule_dem.py
#!/usr/bin/python
import mymodule
mymodule.sayhi()
print 'version is',mymodule.version
print 'date is ',mymodule.date
cat mymodule.py
#!/usr/bin/python
def sayhi():
        print 'hello,this is my module speaking'
version='1.0'
date='20140429'
cat print_tuple.py
#!/usr/bin/python
age=27
name='reed'
print '%s is %d years old'%(name,age)
cat using_list.py
#!/usr/bin/python
shoplist=['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',shoplist
print 'i will sort my list'
shoplist.sort()
print 'sorted shopping list is',shoplist
print 'the first item i will buy is',shoplist[0]
olditem=shoplist[0]
del shoplist[0]
print 'i bought the',olditem
print 'my shopping list is now',shoplist
cat using_name.py
#!/usr/bin/python
if __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/python
import sys
print 'the command line arguments are:'
for i in sys.argv:
        print i
print '\n\nThe PYTHON PATH is',sys.path,'\n'
cat using_tuple.py
#!/usr/bin/python
zoo=('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_zoo
print 'animals brought from old zoo are',new_zoo[2]
print 'animals brought from old zoo is',new_zoo[2][2]


本文出自 “[reed@卢伟开~]#rm -rf /” 博客,谢绝转载!

你可能感兴趣的:(python)