1.模組
。模組化指將乙個完整的程式分解成乙個個的小模組
。通過將模組組合,來搭建出乙個完整的程式
。模組化的優點
。方便開發
。方便維護
。模組可以復用
模組的建立
。在python當中乙個py檔案就是乙個模組
。在乙個模組中引入外部模組 import模組名(模組名就是py檔案)
。可以引入同乙個模組多次,但是模組的例項只會建立一次
。import 模組名 as 模組別名
。在⼀個模組內部都有⼀個__name__。通過它我們可以獲取模組的名字
。如果py⽂件直接運⾏時,那麼__name__預設等於字串』main』__name__屬性值為__main__的模組是主模組。⼀個程式中只有⼀個主模組
3.模組的使用
。訪問模組中的變數 語法是 模組名.變數名
。訪問模組中的函式 語法是 模組名.函式名
。訪問模組中的物件 語法是 模組名.物件名
。我們也可以引⼊模組中部分內容 語法 from 模組名 import 變數,變數…
。還有⼀種引⼊⽅式 語法 from 模組名 import 變數 as 別名
# @author : panhui
# @file : 模組.py
# 模組的建立
# 在python當中乙個py檔案就代表乙個模組
# 在乙個模組中引入其他模組
# 1.import 模組名(python的檔名)
# 可以引入同乙個模組多次,但模組只會執行一次
# import test_m
# import test_m
# import test_m
# print(test_m)
# 2.import 模組名 as 模組別名
# import test_m as test
# print(test_m)
# print(test)
# 模組的使用
# 訪問模組中的變數 語法 模組名.變數名
# print(test_m.a,test_m.b)
# print(test_m.username,test_m.password)
# 訪問模組中的函式 語法 模組名.函式名
# test_m.test2()
# test_m.test1()
# import requests
## requests.get()
# requests.session()
# 訪問模組中的物件 語法 模組名.物件名
# p = test_m.person()
# print(p.name)
# 我們也可以引入模組中的部分內容
# 語法 from 模組名 import 變數
# from test_m import person
# from test_m import test1
# from test_m import test2
# p1 = person()
# print(p1)
# test2()
# 3.語法 from 模組名 import 變數,變數,變數...
..# from test_m import person,test1,test2
# test2()
def test1()
:print
('我是主模組中的test1'
)# from test_m import *
# import test_m
# 4.語法 from 模組名 import 變數 as 別名
from test_m import test1 as new_test1
test1()
new_test1
# import ***
# import *** as yyy
# from *** import yyy,zzz,fff....
# from *** import *
# from *** import yyy as zzz
# @file : test_m.py
# print('這是我的第乙個模組')
# 在每乙個模組都有乙個__name__ 。通過它我們可以獲得當前引入模組的名字
print
(__name__) # __main__ 表示當前的檔案為主檔案
# 在模組中定義變數
# a = 1
# b = 123465
# username = 'panhui'
# password = '123456'
# 在模組中定義函式
def test1()
:print
('test1'
)def test2()
:print
('test2'
)# 在模組中定義類
class
person
: def __init__
(self)
: self.name =
'蒼老師'
# 以下**純屬測試程式
if __name__ ==
'__main__'
: p =
person()
test1()
test2
()
2.列表推導式
# @file : 列表推導式.py
# 舊的列表 --
> 新的列表
# 語法 1
[表示式 for 變數 in 舊列表]
2[表示式 for 變數 in 舊列表 if 條件]
# 找到長度大於3的人名
lst =
['jerry'
,'tony'
,'tom'
,'mok'
,'abcd'
]r =
[name.
capitalize()
for name in lst if
len(name)
>3]
# print(r)
# def fn(lst):
# new_lst =
# for name in lst:
# if len(name) > 3:
# return new_lst
# 1-
100 能被3整除 放到乙個新的列表
new_lst =
[i for i in range(1
,101
)if i %3==
0and i %6==
0]print
(new_lst)
3.生成器
# @file : 生成器.py
# 在python中有一邊迴圈一邊計算的機制,稱之為生成器 generator
# 如何建立生成器
# 1.通過列表推導式
# 需求:得到乙個0
-10之內 分別和3相乘的列表
# new_lst = [x * 3 for x in range(20)]
# print(type(new_lst))
g =(x *
3for x in range(10
))# print(type(g))
# 方式一__next__
() 獲得元素
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# # 方式二 next()
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# 定義生成器方式二 通過函式來完成
# 只要在函式中新增了yield關鍵字,就變成了乙個生成器函式
# 1.定義乙個函式,函式中使用yield關鍵字
# 2.呼叫函式,接收呼叫結果
# 3.得到的結果就是個生成器
# 4.通過next
()函式 __next__()
def fn()
: n =
0while true:
n +=1
# print(n)
yield n # return n + 暫停
# return n
n =fn()
print
(n)print
(next
(n))
print
(next
(n))
print
(next
(n))
print
(next
(n))
C 銳利體驗 第十四講 列舉
第十四講 列舉 列舉型別是c 中又一種輕量級的值型別,c 用列舉來表達一組特定的值的集合行為,比如windows窗體可選的狀態,按鈕控制項的風格等。下面的程式偽碼展示了典型的列舉用法 public enum writingstyle class essay 注意上面的列舉符號classical,mo...
MFC孫鑫第十四講UDP
srv include include void main if lobyte wsadata.wversion 1 hibyte wsadata.wversion 1 socket socksrv socket af inet,sock stream,0 socket socksrv socket...
機器學習基石第十四講筆記
lecture 14 regularization 規則化 14 1 規則化假說集 regularization the magic 從多次的hypothesis set走回到低次的hypothesis set,避免了overfit的發生。ill posed problems 指有很多的函式都滿足s...