Head First Python 讀書筆記

2021-08-21 04:16:33 字數 4307 閱讀 5709

idle整合開發環境

in 操作符 檢查乙個物件是否在另乙個物件中

**不用大括號分開,靠縮排 區分 作為乙個**塊,有 :的地方,**必須縮排

迭代處理乙個物件序列的三種典型方法

序列是乙個有序的物件集合

for迴圈 知道迴圈次數

#  迴圈數字列表,迭代變數 x 指的是被迭代物件中的item

for x in [1,2,3]:

print(x) # 1,2,3

# 迴圈字串

for char in

"icessun !":

print(char)

# 迭代指定次數 range() 函式

for num in range(5):

print("hi icessun!") # 輸出5遍 hi icessun!

檢視某乙個物件或者模組的屬性 dir(),某乙個屬性的幫助文件help()

# from time import sleep 匯入sleep函式,直接使用

time.sleep(random.randint(1,5)) # 隨機從[1,5]中產生乙個整數,睡眠秒數

- 三元表示式:

# 如果y>3 則x=10,否則 x=20

x=10

if y>3

else

20# x與y值之間進行交換

x,y = y,x

# 需求:if  else  語句執行 5 次 終止,每次顯示訊息之間暫停的隨機秒數

from datetime import datetime

import time,random

odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]

for i in range(5):

right_this_minute = datetime.today().minute

if right_this_minute in odds:

print("this minute seems a little odd.")

else:

print("not an odd minute.")

time.sleep(random.randint(1,6))

### 案例二

# 啤酒兒歌

word = "bottles"

for beer_num in range(99,0,-1):

print(beer_num,word,"of beer on the word.")

print(beer_num,word,"of beer.")

print("take one down.")

print("pass it around.")

if beer_num == 1:

print("no more bottles of beer on the wall.")

else:

new_num = beer_num -1

if new_num == 1:

word = "bottle"

print(new_num,word,"of beer on the wall.")

print()

程式的主要功能就是:獲取資料,處理資料,理解資料;一切皆物件。

- python中的內建資料結構:列表[ list],元組(truple ),字典k-v,集合

>>> letter="do not panic!"

>>> letters = list(letter)

>>> letters

['d', 'o', ' ', 'n', 'o', 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']

>>> print(letters[0:10:3])

['d', 'n', ' ', 'n']

>>> print(letters[3:])

['n', 'o', 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']

>>> print(letters[:10])

['d', 'o', ' ', 'n', 'o', 't', ' ', 'p', 'a', 'n']

>>> print(letter[::2])

d o ai!

>>> type(letter[::2])

'str'>

# 陣列倒序

>>> print(letter[::-1])

!cinap ton od

# 列表倒序

>>> print(letters[::-1])

['!', 'c', 'i', 'n', 'a', 'p', ' ', 't', 'o', 'n', ' ', 'o', 'd']

>>> print(letters[-10:-1:1])

['n', 'o', 't', ' ', 'p', 'a', 'n', 'i', 'c']

# start值一定要絕對值大於stop值

>>> print(letters[-1:-10:1])

# 列表轉字串

>>> ''.join(letters[0:3])

'do '

案例一
# 列表方法的使用

phrase = "don't panic!"

plist=list(phrase) # 轉為列表

print(phrase)

print(plist)

for i in range

(4):

plist.pop()

plist.pop(0)

plist.remove("'")

plist.extend([plist.pop(),plist.pop()])

plist.insert(2,plist.pop(3))

new_phrase=''.join(plist)

print(plist)

print(new_phrase)

##############

don't panic!

['d', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']

['o', 'n', ' ', 't', 'a', 'p']

on tap

案例二
phrase = "don't panic!"

plist=list(phrase)

print(phrase)

print(plist)

new_pharse = ''.join(plist[1:3])

new_pharse = new_pharse + '

'.join([plist[5],plist[4],plist[7],plist[6]])

print(plist)

print(new_pharse)

##############

don't panic!

['d', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']

['o', 'n', ' ', 't', 'a', 'p']

on tap

# 接受items()返回的鍵值對列表

for k,v in sorted(found,items()):

print(k,'was found ',v,'times.')

案例一

Head First Python(定製資料物件)

新的檔案格式 sarah sweeney,2002 6 17,2 58,2.58,2 39,2 25,2 55,2 54,2.18,2 55,2 55,2 22,2 21,2.22 如果使用split bif把資料抽取到乙個列表,第乙個資料項是名字,然後是出生日期,然後是計時資料。sarah get...

HeadFirstPython 資料持久化

usr bin env python coding utf 8 這一章主要是講如何將資料進行持久化。使用pickle對資料進行醃製。在對資料進行醃製之前,我們需要對資料進行格式化 針對資料 取出我們想要的資料。在中間的知識點是 檔案開啟與關閉,以及其異常處理。我用的是python2.7 在某些語法上...

Head First Python 讀書筆記(六)

class dog 建立了狗類 def init self,name str,age int none 初始化屬性name和age self.name name self.age age defsit self none 模擬小狗蹲下 print self.name.title is now sit...