python 小知識點筆記

2021-07-27 04:47:55 字數 575 閱讀 3253

len() 和  range()經常和for迴圈用在一起處理字串

>>> for i in range (len('asdf')):

...    print 'asdf'[i]

但是for迴圈也只能遍歷索引,或者元素,不能同時遍歷索引和元素。函式enumerate()解決了這個問題

>>> for i , ch in enumerate('asdfghh'):

...     print i , ch

... 

0 a1 s

2 d3 f

4 g5 h

6 h列表解析: 可以在一行裡面使用for迴圈並且得到結果

>>> a = [x*2 for x in range(4)]

>>> for i in a:

...   print i

... 02

46函式在呼叫之前必須先定義。

如果函式中沒有 return 語句, 就會自動返回 none 物件。

加號對於所有標準型別都起作用。如:

>>> [1, 22]+[22, 33]

[1, 22, 22, 33]

python小知識點學習筆記

time模組的一些用法 coding utf 8 import time a time.time 返回時間戳 print ab time.ctime a 返回時間字串 print bc time.strptime b 時間字串轉為時間物件 print cd time.strftime y m d h...

Python小知識點

1.時間戳 從1970年到現在的秒數 time2 time.time print time2 date9 datetime.datetime.now print date9.timestamp 上面是兩種用到時間戳的 stamp 郵戳。timestamp 時間戳,時間線。2.執行緒休眠 爬蟲 獲取對...

Python小知識點

1.預設引數 必須放在引數列表的隊尾 普通形參必須放在預設引數的前面 def test a,b 3 passtest test 2.函式引數可以為任意型別 testb testa 3.args返回的是乙個元組 4.map函式裡面需要兩個值 值1 必須是函式 值2 序列 容器 作用 將序列裡面的每個元...