簡介Python代碼兩大實際應用手冊
本人很喜歡Python代碼,在工作中也很喜歡總結關于Python代碼的經驗教訓,下面就這個問題來詳細說一般在定義類和函數頭時,Python代碼的實際應用方面的介紹,希望大家瀏覽以下文章會有所收獲。
字符串——單引號和雙引號的作用相同(與perl不同)
- perl -p -i -e 's/old/new/g' filename
引號指示一個多行字符串,一般在定義類和函數頭時做解釋說明。
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的變量:使用變量時只需要賦值,不需要聲明或定義數據類型。
python內置的三種數據結構:
list、tple和dict。
一、list常用的幾種方法:
- append,count,extend,index,insert,pop,remove,reverse,sort
展示list用法的簡單例子:
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和字符串一樣是不可變序列。元素間用逗號分隔,為了便于識別一般會在tuple起始和結束位置加括號。
元組最通常的用法是用在打印語句中。
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代碼有很多值得學習的地方,這里我們主要介紹Python代碼 ,包括介紹實際應用方面的介紹。
【編輯推薦】