Python知識點總結篇(二)

2021-09-24 07:51:52 字數 2184 閱讀 2961

cat = ['fat', 'black', 'loud']

size, color, disposition = cat

複製**

sort()sorted()方法的比較:sort(key = none, reverse = false)就地改變列表,sorted(iterable, key = none, reverse = false)返回新的列表,對所有可迭代物件均有效;

supplies = ['pens', 'staplers', 'flame-throwers', 'binders']

supplies.sort()

print(supplies)

supplies = ['pens', 'staplers', 'flame-throwers', 'binders']

sortedsupplies = sorted(supplies)

print(supplies)

print(sortedsupplies)

複製**

#序列轉元組

pets = ['k', 'm', 'n']

print(tuple(pets))

#元組轉序列

pets = ('k', 'm', 'n')

print(list(pets))

複製**

>>> spam = [0, 1, 2, 4, 5]

>>> chees = spam

>>> cheese[1] = 'hello'

>>> spam

[0, 'hello', 2, 4, 5]

>>> cheese

[0, 'hello', 2, 4, 5]

複製**

>>>

'i am bringing ' + str(picnicitems.get('cups', 0) + ' cups.'

i am bringing 2 cups.

>>>

'i an bringing ' + str(picnicitems.get('eggs', 0) + ' cups.'

i am bringing 0 eggs.

複製**

>>> spam = 

>>> spam.setdefault('color', 'black')

'black'

>>> spam

>>> spam.setdefault('color', 'white')

'black'

>>> spam

複製**

格式輸出:pprint()pformat()

import pprint

info =

pprint.pprint(info)

#下列這句和上句結果相同

#print(pprint.pformat(info))

複製**

#巢狀的字典和列表

deftotalbrought

(guests, item):

numberbrought = 0;

for k, v in guests.items():

numberbrought += v.get(item, 0)

return numberbrought

複製**

>>> print(r'that is carol\'s cat.')

that is carol\'s cat.

複製**

>>> print('''dear alice,

sincerely,

bob''')

dear alice,

sincerely,

bob複製**

Python知識點總結篇(三)

with 自動關閉檔案 with open log r as f 複製 管道 用於匹配多個表示式中的乙個,匹配多個分組 問號 實現可選匹配 import re batregex re.compile r bat wo man mo1 batregex.search the adventures of...

Python知識點總結篇(四)

def calc n print n if n 2 0 return calc n 2 calc 10 複製 def add x,y,func return func x func y result add 4,8,abs print result 複製 coding utf 8 usr bin p...

Python知識點總結篇(五)

常見結構 demo bin 存放專案的一些可執行檔案 demo 可執行程式,啟動demo調main.py demo 存放專案所有原始碼,原始碼中所有模組 包都在此處 tests 存放單元測試 init py test main.py init py 空檔案,有這個檔案就是包,沒有就是目錄 main....