python筆記(2)
更新時(shí)間:2012年10月24日 22:07:04 作者:
python筆記,參考上一篇文章大家繼續(xù)
繼續(xù)List:
刪除元素:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
清空list
a[ : ] = []
del a[:]
list作為棧使用(后入先出):
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
用負(fù)數(shù)索引:
b=[1, 2, 3, 4]
b[-2] #3
"+"組合list:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
查出某元素在list中的數(shù)量:
lst.('hello') # hello 的數(shù)量
list排序:
sort()
#對鏈表中的元素進(jìn)行適當(dāng)?shù)呐判颉?
reverse()
#倒排鏈表中的元素
函數(shù)指針的問題:
def f2(a, L=[])
L.append(a)
return L
print(f2(1)) # 1
print(f2(2)) # 1, 2 L在這次函數(shù)調(diào)用時(shí)是[1]
print(f2(3)) # 1, 2, 3
函數(shù)中的參數(shù)中有:
*參數(shù)名 :表示任意個(gè)數(shù)的參數(shù)
** ?。罕硎綿ictionary參數(shù)
控制語句:
IF:
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
FOR:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
WHILE:
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
pass :空操作語句
while True:
pass
dictionary: 鍵值對的數(shù)據(jù)結(jié)構(gòu)
用list來構(gòu)造dictionary:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
有趣的比較:
x = [] #list
x[2] = 'foo' #出錯(cuò)
x = {} #dictionary
x[2] = 'foo' #正確
內(nèi)容比較雜,學(xué)到什么就記下來。完全利用工作中的空閑和業(yè)余時(shí)間來完成,更加充實(shí)了。
刪除元素:
復(fù)制代碼 代碼如下:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
清空list
復(fù)制代碼 代碼如下:
a[ : ] = []
del a[:]
list作為棧使用(后入先出):
復(fù)制代碼 代碼如下:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
用負(fù)數(shù)索引:
復(fù)制代碼 代碼如下:
b=[1, 2, 3, 4]
b[-2] #3
"+"組合list:
復(fù)制代碼 代碼如下:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
查出某元素在list中的數(shù)量:
復(fù)制代碼 代碼如下:
lst.('hello') # hello 的數(shù)量
list排序:
復(fù)制代碼 代碼如下:
sort()
#對鏈表中的元素進(jìn)行適當(dāng)?shù)呐判颉?
reverse()
#倒排鏈表中的元素
函數(shù)指針的問題:
復(fù)制代碼 代碼如下:
def f2(a, L=[])
L.append(a)
return L
print(f2(1)) # 1
print(f2(2)) # 1, 2 L在這次函數(shù)調(diào)用時(shí)是[1]
print(f2(3)) # 1, 2, 3
函數(shù)中的參數(shù)中有:
*參數(shù)名 :表示任意個(gè)數(shù)的參數(shù)
** ?。罕硎綿ictionary參數(shù)
控制語句:
IF:
復(fù)制代碼 代碼如下:
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
FOR:
復(fù)制代碼 代碼如下:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
WHILE:
復(fù)制代碼 代碼如下:
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
pass :空操作語句
復(fù)制代碼 代碼如下:
while True:
pass
dictionary: 鍵值對的數(shù)據(jù)結(jié)構(gòu)
用list來構(gòu)造dictionary:
復(fù)制代碼 代碼如下:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
有趣的比較:
復(fù)制代碼 代碼如下:
x = [] #list
x[2] = 'foo' #出錯(cuò)
x = {} #dictionary
x[2] = 'foo' #正確
內(nèi)容比較雜,學(xué)到什么就記下來。完全利用工作中的空閑和業(yè)余時(shí)間來完成,更加充實(shí)了。
相關(guān)文章
Python練習(xí)之操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了Python練習(xí)之操作MySQL數(shù)據(jù)庫,文章通過如何創(chuàng)建MySQL數(shù)據(jù)表?如何向MySQL表中插入數(shù)據(jù)?如何查詢MySQL中的數(shù)據(jù)?的三個(gè)問題展開了詳細(xì)的內(nèi)容介紹2022-06-06
python基于Tkinter庫實(shí)現(xiàn)簡單文本編輯器實(shí)例
這篇文章主要介紹了python基于Tkinter庫實(shí)現(xiàn)簡單文本編輯器,實(shí)例分析了Python使用Tkinter庫實(shí)現(xiàn)簡單桌面應(yīng)用程序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
python基礎(chǔ)教程之基本數(shù)據(jù)類型和變量聲明介紹
這篇文章主要介紹了python基礎(chǔ)教程之基本數(shù)據(jù)類型和變量聲明介紹,首先講解了變量聲明的一些知識,然后列出最常用的基本數(shù)據(jù)類型,需要的朋友可以參考下2014-08-08
解決Jupyter notebook中.py與.ipynb文件的import問題
這篇文章主要介紹了解決Jupyter notebook中.py與.ipynb文件的import問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python?內(nèi)置logging?使用詳細(xì)介紹
提供日志記錄的接口和眾多處理模塊,供用戶存儲各種格式的日志,幫助調(diào)試程序或者記錄程序運(yùn)行過程中的輸出信息,這篇文章主要介紹了Python?內(nèi)置logging?使用講解,需要的朋友可以參考下2022-07-07

