Python入門上(第三天)

2021-10-24 11:24:37 字數 1308 閱讀 2524

一、推導式

列表推導式

[ expr for value in collection [if condition] ]
元組推導式

( expr for value in collection [if condition] )
字典推導式

集合推導式 

其它 

- `next(iterator[, default])` return the next item from the iterator. if default is given and the iterator is exhausted, it is returned instead of raising stopiteration.
這裡需要注意元組推導式和next()推導式,跟其他推導式的不同

x = [[i, j] for i in range(0, 3) for j in range(0, 3)]

print(x)

# [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]

x[0][0] = 10

print(x)

# [[10, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]

b = 

print(b)

#

e = (i for i in range(10))

print(e)

# at 0x0000007a0b8d01b0>

print(next(e)) # 0

print(next(e)) # 1

for each in e:

print(each, end=' ')

# 2 3 4 5 6 7 8 9

e = (i for i in range(10))

print(e)

# at 0x0000007a0b8d01b0>

print(next(e)) # 0

print(next(e)) # 1

for each in e:

print(each, end=' ')

# 2 3 4 5 6 7 8 9

Python入門第三天

pyhton3 第三方庫文件檢視 python m pydoc p 1234然後再瀏覽器中輸入 localhost 1234 字串 s.title 將字串的首字母大寫 s.upper 轉化為大寫 s.lower 轉化為小寫 s.swapcase 大小寫交換 s.isalnum 檢查是否全部是字母或數...

python入門第三天 繼承

繼承物件導向的程式設計最好的用處則是 的重用 實現重用的重要方法是通過繼承機制 class schoolmember def init self,name,age self.name name self.age age print initialized schoolmember s self.na...

第三天(Python打卡)

遞迴 def factorial n if n 1 return 1 else return n factorial n 1 number int input 請輸入乙個正整數 result factorial number print d 的階乘是 d number,result 解決漢諾塔問題 ...