Python 入個門(二)

2021-10-02 04:44:11 字數 2683 閱讀 2053

python程式設計 從資料分析到資料科學 摘抄及拓展

5.3強型別語言

# print(2+'3')  這種寫法是錯誤的,python不支援資料型別的自動轉化

print(3

+true

)#4

5.8python中的關鍵字

使用模組keyword中的屬性kwlist檢視,這裡返回的是乙個列表。

import keyword

print

(keyword.kwlist)

'''['false', 'none', 'true', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

'''

5.9檢視已定義的所有變數
print

(dir()

)#['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

a =1

#定義變數

print

(dir()

)#['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a']

del a #刪除變數

print

(dir()

)#['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

7.4序列的拆包式賦值
a, b, c =1,

2,3print

(a, b, c)

# 1 2 3

a,*b, c =1,

2,3,

4,5,

6print

(a, b, c)

# 1 [2, 3, 4, 5] 6

7.5兩個變數值的調換
a, b =1,

2a, b = b, a

9.1特殊運算子

進製轉化

a =

10# 十進位制轉化為其他進製

print

(bin

(a))

# 0b1010

print

(oct

(a))

# 0o12

print

(hex

(a))

# 0xa

# 其他進製轉化為十進位制

print

(int

(str

(a), base=16)

)

9.2內建函式

_dir(builtins)_可以用來檢視內建函式

#pow 乘方

print

(pow(2

,10))

# 1024

print(2

**10

)# 1024

# round

print

(round

(2.991))

# 3print

(round

(2.991,2

))# 2.99 第二個引數為保留幾位小數,不寫預設為0

#結合的順序

print(2

**2**3

)# 256 (按數學的方式寫出來先算最上面的次方)

print((

2**2)

**3)# 64

對於and,如果沒有假值,返回的是最後乙個真值,如果有假值,則返回的是第乙個假值。

對於or,如果沒有真值,返回的是最後乙個假值,如果有真值,則返回的是第乙個真值。

print(1

+9and3+5

)# 8

print(0

and3+5

)# 0

if語句,三元運算

python中的else可以出現在while,for,try-catch語句 中

print(1

+9and3+5

)# 8

print(0

and3+5

)# 0

python 快速判斷閏年
import calendar

print

(calendar.isleap(

2019))

#false

Python 入個門(三)

python程式設計 從資料分析到資料科學 摘抄及拓展 12.2 關於else的用法 a 1 3,5 7,2 4,6 8 b sorted a,reverse 0 reverse為0從小到大排序,預設值為0 print list b 1,2,3,4,5,6,7,8 print a 1,3,5,7,2...

樹刨入個門?

重兒子 重兒子是每個節點的兒子節點中子樹節點數最大的那乙個。樹鏈刨分,把乙個樹砍成好多份變成乙個陣列p,p陣列裡存的是點的訪問到的順序,但是點的順序是根據什麼來的?先訪問重兒子,因為這樣可以盡可能多的一下找好長的鏈。經典問題 1.給樹上兩個節點之間的最短邊上加上val 2.查詢兩點的最短距離 用到好...

《Python自然語言處理中文版》初入此門

python自然語言處理 一書由edward loper,ewan klein和steven bird三人合著,同時此三人也是python 語言註明的工具包nltk的主要貢獻者。因此,本書倒更像是一本python nltk教程。中文版由陳濤翻譯,作者在學習之餘翻譯了此書並出版,無論質量如何都是一件有...