10個應該知道的Python的程式設計技巧

2021-10-07 00:14:24 字數 4457 閱讀 7523

1.變數的交換

不需要在用temp儲存a的值,再交換了

>>

> a =

1>>

> b =

2>>

> a, b = b, a

>>

>

print

(a,b)

21

2.字串的格式化

name =

'rose'

age =

19country =

'china'

print

("hello,i'm "

+ name +

".i'm from "

+ country

+". and i'm "

+str

(age)

+'.'

)print

("hello,i'm %s. i'm from %s. and i'm %d."

%(name, country, age)

)print

("hello,i'm {}. i'm from {}. and i'm {}."

.format

(name, country, age)

)print

("hello,i'm . yes i'm ."

.format

(name)

)print

(f"hi,i'm

. i'm from

. and i'm

.")

最後一行語句中的,裡面的表示式可以為任意形式的。age+4,函式返回值等等

print(f…)可以避免型別轉化的錯誤,推薦使用喲?

hello,i'm rose.i'm from china. and i'm 19

.hello,i'm rose. i'm from china. and i'm 19

.hello,i'm rose. i'm from china. and i'm 19

.hello,i'm rose. yes i'm rose.

hi,i'm rose. i'm from china. and i'm 19.00000

.

3.yield語法

def

fibonacci

(n):

a =0 b =

1 nums =

for _ in

range

(n):

a, b = b, a+b

return nums

for i in fibonacci(10)

:print

(i)###

deffibonacci

(n):

a =0 b =

1for x in

range

(n):

yield a

a, b = b, a+b

for i in fibonacci(10)

:print

(i)######列印斐波那契數列的前十項

yield(生產)的好處是,不用等所有的返回值都計算好再輸出,而是計算出乙個列印乙個,再返回函式中計算,再返回乙個值。

4.列表解析式

str.startswith(str, beg=0, end=len(string));

引數str – 檢測的字串。

strbeg – 可選引數用於設定字串檢測的起始位置。

strend – 可選引數用於設定字串檢測的結束位置。

fruit =[,

"banana"

,"orange"

,"pear"

]fruit =

[x.upper(

)for x in fruit]

print

(fruit)

fruit =[,

"banana"

,"orange"

,"pear"

]fruit =

[x.upper(

)for x in fruit if x.startswith(

"a")

]print

(fruit)

#####[,

'banana'

,'orange'

,'pear'][

]

5.enumerate函式

fruit =[,

"banana"

,"orange"

,"pear"

]for i, x in

enumerate

(fruit)

:print

(i, x)

####

1 banana

2 orange

3 pear

6.1反向遍歷

fruit =[,

"banana"

,"orange"

,"pear"

]for i, x in

enumerate

(reversed

(fruit)):

print

(i, x)

####

0 pear

1 orange

2 banana

6.2按順序遍歷

fruit =[,

"banana"

,"orange"

,"pear"

]for i, x in

enumerate

(sorted

(fruit)):

print

(i, x)

###1 banana

2 orange

3 pear

7.字典合併操作

a =

b =c =

for x in a:

c[x]

= a[x]

for x in b:

c[x]

= b[x]

print

(c)###

a =

b =c =

print

(c)####

這裡的兩個星號叫解包(unpacking),相當於將a,b字典中的內容直接填充到c這裡。

8.三元運算子

score =

66if score >=60:

s ="pass"

else

: s =

"fail"

print

(s)s =

"pass"

if score >=

60else

"fail"

print

(s)#####

pass

pass

s = 「pass」 if score >= 60 else 「fail」

可以看到,當條件滿足時執行前面的語句,當條件不滿足時執行後面的語句。

9.序列解包

name =

"zhang san"

str_list = name.split(

)first_name,last_name = name.split(

)print

(str_list)

print

(first_name)

print

(last_name)

###[

'zhang'

,'san'

]zhang

san

本人也是新手,不太懂0.0 先記在部落格裡,以後慢慢學習深入了,再回來看看。哈哈哈

10.with語句

f =

open

("111.txt"

,"r"

)s = f.read(

)f.close(

)

避免忘記close。

with

open

("111.txt"

,"r"

)as f:

s = f.read(

)

可自動關閉。

PHP大牛必須應該知道的10個科技術語

高科技行業中工作會聽到很多特定行業的行話,從這個角度來說,這些專業術語是有必要了解的,為了更好的使用這些東西,也就必須對其有一定的了解,下面是班吉溫伯格最近解釋了10個基本術語。api 應用程式程式設計介面有助於不同的軟體元件互相交流。api提供了一種簡單,標準化的方式提供的功能,而不需要大量複雜的...

每個 Linux 新手都應該知道的 10 個命令

全球網際網路使用者有 3.74 億 1 他們都以某種方式使用 linux,因為 linux 伺服器佔據了網際網路的 90 大多數現代路由器執行 linux 或 unix,top500 超級計算機 2 也依賴於 linux。如果你擁有一台 android 智慧型手機,那麼你的作業系統就是由 linux...

IT 這個行當裡你應該知道的10個小秘密

it 這個行當裡你應該知道的10個小秘密 如果你正準備投身到it這行,或者你還是個it新手,下面列出的很多 小秘密 也許會讓你驚訝不已,因為我們通常不會大聲的討論它們。如果你是個it老手,這些所說的估計你大部分都遇到過,而且很有可能還有自己的心得 當然,非常歡迎你花幾秒鐘的時間把你的所知道的其它小秘...