這是這些天學(xué)python的筆記,為加深印象,我把學(xué)過(guò)的怕忘的知識(shí)整理記錄下來(lái)。
用的教材是《Python語(yǔ)言程序設(shè)計(jì)(英文版)》
第一句學(xué)的語(yǔ)言:
print("Welcome to Python")
Python的句尾都是沒(méi)有標(biāo)點(diǎn)符號(hào)(punctuation)的,但是在循環(huán)語(yǔ)句中是有標(biāo)點(diǎn)符號(hào)。
Python是一門(mén)注意縮進(jìn)的語(yǔ)言。在體內(nèi)的句子可以用tab和space來(lái)進(jìn)行縮進(jìn),但要按照一定的規(guī)則,否則就會(huì)報(bào)錯(cuò)??赡躳rint語(yǔ)句空格和沒(méi)有空格就會(huì)造成結(jié)果很大的區(qū)別,例如下面此條語(yǔ)句:
squares = []
for value in range(1,11):
????square = value**2
squares.append(square)
print(squares)
與的區(qū)別
squares = []
for value in range(1,11):
????square = value**2
????squares.append(square)
print(squares) ??

下面都是關(guān)于一些,縮進(jìn)編譯器報(bào)錯(cuò)提示
1. TabError: inconsistent use of tabs and spaces in indentation
錯(cuò)誤原因:tab和space混用不當(dāng),造成縮進(jìn)問(wèn)題。我試了一下,貌似用體內(nèi)用tab縮進(jìn)后再用space縮進(jìn)不會(huì)報(bào)錯(cuò),但若先用space再用tab則可能造成該錯(cuò)誤。
2. SyntaxError: invalid syntax
錯(cuò)誤原因:語(yǔ)法錯(cuò)誤,一行的最前方如果是...表明還在體內(nèi),此時(shí)若要寫(xiě)體外語(yǔ)句,則需要按下回車(chē),最前方為>>>時(shí)則可以另起一體。
3. IndentationError: unexpected indent
錯(cuò)誤原因:不需要縮進(jìn)時(shí)縮進(jìn)
4. IndentationError: expected an indented block
錯(cuò)誤原因:該縮進(jìn)時(shí)沒(méi)縮進(jìn)
5.循環(huán)后不必要的縮進(jìn)
2.1 title,upper,lower三個(gè)字符函數(shù)

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
append函數(shù),刪除函數(shù)
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
?motorcycles.append('ducati')
print(motorcycles)
Append往表中最后一位添加一個(gè)元素
Insert往表中添加一個(gè)元素,格式為 ?列表a。insert(n,’字符’)
Pop函數(shù)的用法,變量a=變量a.pop()
remove函數(shù)的用法。列表a。remove(“字符”)
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45?
定步長(zhǎng):函數(shù)range()
even_numbers = list(range(2,11,2))
print(even_numbers)?
2 ? ? 4 ?? 6 ? ? 8 ? ? 10
squares.py
squares.py
squares = []
?for value in range(1,11):
? ? square = value**2
? ?? squares.append(square)
? ?? print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]?