簡(jiǎn)介Python代碼兩大實(shí)際應(yīng)用手冊(cè)
本人很喜歡Python代碼,在工作中也很喜歡總結(jié)關(guān)于Python代碼的經(jīng)驗(yàn)教訓(xùn),下面就這個(gè)問(wèn)題來(lái)詳細(xì)說(shuō)一般在定義類(lèi)和函數(shù)頭時(shí),Python代碼的實(shí)際應(yīng)用方面的介紹,希望大家瀏覽以下文章會(huì)有所收獲。
字符串——單引號(hào)和雙引號(hào)的作用相同(與perl不同)
- perl -p -i -e 's/old/new/g' filename
引號(hào)指示一個(gè)多行字符串,一般在定義類(lèi)和函數(shù)頭時(shí)做解釋說(shuō)明。
Python代碼
- #!/usr/bin/python
- #Filename:mymodule.py
- class myModule:
- """show
- this is only one simple example"""
- pass
- p = myModule()
- print p
- #!/usr/bin/python
- #Filename:mymodule.py
- class myModule:
- """show
- this is only one simple example"""
- pass
- p = myModule()
- print p
python的變量:使用變量時(shí)只需要賦值,不需要聲明或定義數(shù)據(jù)類(lèi)型。
python內(nèi)置的三種數(shù)據(jù)結(jié)構(gòu):
list、tple和dict。
一、list常用的幾種方法:
- append,count,extend,index,insert,pop,remove,reverse,sort
展示list用法的簡(jiǎn)單例子:
Python代碼
- #!/usr/bin/python
- #Filename:using_list.py
- 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 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 buy is ',shoplist[0]
- olditem = shoplist[0]
- del shoplist[0]
- print 'I bought the',olditem
- print 'My shopping list is now',shoplist
- #!/usr/bin/python
- #Filename:using_list.py
- 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 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 buy is ',shoplist[0]
- olditem = shoplist[0]
- del shoplist[0]
- print 'I bought the',olditem
- print 'My shopping list is now',shoplist
二、tuple與list十分相似,只是tuple和字符串一樣是不可變序列。元素間用逗號(hào)分隔,為了便于識(shí)別一般會(huì)在tuple起始和結(jié)束位置加括號(hào)。
元組最通常的用法是用在打印語(yǔ)句中。
Python代碼
- #!/usr/bin/python
- #Filename:print_tuple.py
- age = 26
- name = 'SongYang'
- print '%s is %d years old.' %(name,age)
- print '''''%s loves that girl who he is missing.
- Why is %s playing with that python?''' % (name,name)
- #!/usr/bin/python
- #Filename:print_tuple.py
- age = 26
- name = 'SongYang'
- print '%s is %d years old.' %(name,age)
- print '''%s loves that girl who he is missing.
- Why is %s playing with that python?''' % (name,name)
Python代碼有很多值得學(xué)習(xí)的地方,這里我們主要介紹Python代碼 ,包括介紹實(shí)際應(yīng)用方面的介紹。
【編輯推薦】