視頻看無聊了,也不想再看廖雪峰的教程了。
現(xiàn)在開始記錄《Python編程:從入門到實(shí)踐》的筆記和進(jìn)度。
字符串:
name = "ada lovelace"
name=name.title() ????#Ada Lovelace
name.upper()? ? ? ?#ADA LOVELACE
name.lower()? ? ? ? #ada lovelace
fav_lan = '????Python????'
fav_lan.lstrip()? ? #'Python? ? '
fav_lan.rstrip()? ? #'? ? Python'
fav_lan.strip()? ? #'Python'
>>> 3 / 2
1.5
>>> 3 ** 3
27
>>> 10 ** 6
1000000
import this # The Zen?of Python
不要企圖編寫完美無缺的代碼;先編寫行之有效的代碼,再?zèng)Q定是對(duì)其做進(jìn)一步改進(jìn),還是轉(zhuǎn)而去編
寫新代碼。
第三章?列表
2. 在列表中插入元素
①方法append()在末尾添加
②使用方法insert()可在列表的任何位置添加新元素。為此,你需要指定新元素的索引和值。
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
方法insert()在索引0處添加空間,
并將值'ducati'存儲(chǔ)到這個(gè)地方。這種操作將列表中既有的每個(gè)元素都右移一個(gè)位置
['ducati', 'honda', 'yamaha', 'suzuki']
3.2.3 從列表中刪除元素
1. 使用del語句刪除元素
如果知道要?jiǎng)h除的元素在列表中的位置,可使用del語句。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0] #刪除了列表motorcycles中的第一個(gè)元素——'honda':
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
2. 使用方法pop()刪除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
①方法pop()可刪除列表末尾的元素,并返回該元素。
latest = motorcycles.pop()
②以使用pop()來刪除列表中任何位置的元素
temp = motorcycles.pop(0) #刪除索引為0的元素
4.?方法remove()根據(jù)值刪除元素
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('ducati')?? ? #刪除list中值為ducati的元素
注意:方法remove()只刪除第一個(gè)指定的值
3.3.1 使用方法 sort()對(duì)列表進(jìn)行永久性排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)? ? ?#反向排序
3.3.2 使用函數(shù) sorted()對(duì)列表進(jìn)行臨時(shí)排序
print(sorted(cars,reverse=True))
cars.reverse()? ? ?#永久性反轉(zhuǎn)列表
P61?