Python teach
Learn it step by step
學習python
4.
*
5.
**** Learn it.
Learn it step by step
學習python
1.
使用自定義模塊#!/usr/lib/evn python
#coding:utf-8
#FileName:test.py
#Funxtion:Learn to use define module.
#History:26-11-2013
import mymodule
mymodule.sayhi()
print'Version',mymodule.version
測試:
$ python test.py
Hi,this is mymodule speaking
Version 0.1
*
2. (Output tuple) 如何使元組輸出
!/usr/lib/evn python
#coding:utf-8
#FileName:test.py
#Funxtion:Learn to use output tuple.
#History:26-11-2013
age =22
name ='Swaroop'
print '%s is %d years old ' %(name,age)
print 'Why is %s playing with that python?' %name
測試:
$ python test.py
Swaroop is 22 years old
Why is Swaroop playing with that python?
*
3.如何使用元組
#!/usr/lib/evn python
#coding:utf-8
#FileName:test.py
#Funxtion:Learn to use tuple.
#History:26-11-2013
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 new zoo is ',len(new_zoo)
print 'All animals in new zoo are ',new_zoo
print 'Animals brought from zoo are',new_zoo[ 2]
print 'Last animal brought from zoo are',new_zoo[ 2][ 2]
測試:
$ python test.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from zoo are penguin
*4.
如何使用字典
#!/usr/lib/evn python
#coding:utf-8
#FileName:test.py
#Funxtion:Learn to use dict
#History:26-11-2013
ab={
'Swaroop':'[email protected]',
'Larry':'[email protected]',
'Mask':'[email protected]',
'Sport':'[email protected]'
}
print 'Swaroop address is %s' % ab['Swaroop']
#Adding a key/value pair
ab['Guido']='[email protected]'
#Deleting a key/value pair
del ab['Sport']
print '\nThere are %d contacts in the address-book\n'%len(ab)
for name,address in ab.items():
print'Contact %s at %s' %(name,address)
if 'Guido' in ab:
print'\n Guido address is %s' %ab['Guido']
測試:
$ python test.py
Swaroop address is [email protected]
There are 4 contacts in the address-book
Contact Swaroop at [email protected]
Contact Larry at [email protected]
Contact Mask at [email protected]
Contact Guido at [email protected]
Guido address is [email protected]
5.
如何使用 list
#!/usr/lib/evn python
#coding:utf-8
#FileName:test.py
#Funxtion:Learn to use list .
#History:26-11-2013
shoplist =['apple','mango','carrot','banana']
print 'These items are:',
for item in shoplist:
print':_',item
print '\n I also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now:_',shoplist
print 'I will sort my list now.'
shoplist.sort()
print 'Sorted shopping list is',shoplist
print 'The first item I will by is',shoplist[ 0]
olditem=shoplist[ 0]
del shoplist[ 0]
print 'I brought the ',olditem
print 'My shopping list is now',shoplist
測試:
shoplist =['apple','mango','carrot','banana']
print 'These items are:',
for item in shoplist:
print':_',item
print '\n I also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now:_',shoplist
print 'I will sort my list now.'
shoplist.sort()
print 'Sorted shopping list is',shoplist
print 'The first item I will by is',shoplist[ 0]
olditem=shoplist[ 0]
del shoplist[ 0]
print 'I brought the ',olditem
print 'My shopping list is now',shoplist
**** Learn it.