學習一門語言,是需要付出大量精力的
昨天大概看了2個小時視頻學習,發(fā)現(xiàn)只聽他講和跟著他操作是很難把知識轉(zhuǎn)化為自己所用的。
字符串str、列表list、元組tuple的方法還沒有全部學會,今晚繼續(xù)加油,把下列都試試,完成3道基礎(chǔ)題
# 字符串
words = "today is a wonderfulday"
print(words.strip('today'))? ? ? # 如果strip方法指定一個值的話,那么會去掉這兩個值
print(words.count('a'))? ? ? ? ? # 統(tǒng)計字符串出現(xiàn)的次數(shù)
print(words.index('is'))? ? ? ? # 找下標
print(words.index('z'))? ? ? ? ? # 找下標如果元素不找不到的話,會報錯
print(words.find('z'))? ? ? ? ? # 找下標,如果元素找不到的話,返回-1
print(words.replace('day','DAY'))# 字符串替換
# 列表
sample_list = ['a', 1, ('a', 'b')]? # 創(chuàng)建列表
sample_list = ['a', 'b', 0, 1, 3]? #? Python列表操作
value_start = sample_list[0]? # 得到列表中的某一個值
end_value = sample_list[-1]? # 得到列表中的某一個值
del sample_list[0]? # 刪除列表的第一個值
sample_list[0:0] = ['sample value']? # 在列表中插入一個值
# 元組
#元組也是一個list,他和list的區(qū)別是元組的元素無法修改
tuple1 = (2, 3, 4, 5, 6, 4, 7)
print(type(tuple1))
print(tuple1[:7])
print(tuple1[: 5: -1])
for i in range(6):
? ? ? ? print(tuple1[i])
for i in tuple1:
? ? ? ? print(i)
# 字典
D.get(key, 0)? ? ? # 同dict[key],多了個沒有則返回缺省值,0。[]沒有則拋異常
D.has_key(key)? ? ? # 有該鍵返回TRUE,否則FALSE
D.keys()? ? ? ? ? ? # 返回字典鍵的列表
D.clear()? ? ? ? ? # 清空字典,同del dict
D.copy()? ? ? ? ? ? # 拷貝字典