1. 程式人生 > 實用技巧 >多測師講解python_模組(匯入模組和內建模組)_高階講師肖sir

多測師講解python_模組(匯入模組和內建模組)_高階講師肖sir

#自定義模組
# from aaa import * #指定匯入某個包中具體的類、函式、方法
#
# A.fun1(2,2)

#import +模組名 :
# # import +模組名+.+.+
# # 匯入後的模組如果沒有被引用:一般是顯示為灰色,如被呼叫則亮色
# # 把某個模組匯入到當前的檔案中,讓當前檔案所引用
# # 模組只需要匯入一次即可(你把模組註釋掉了,那麼就必須要重新匯入)
# # 模組匯入後在當前檔案中可以重複使用模組中的類方法函式等等
# # 在檔案中模組只需要匯入一次即可,無需重複的匯入(除非你註釋掉了)'''
# # from +模組名稱 import +   *(所有的意思)
# # from +模組名稱 import +指定的類、函式、方法。。

匯入time 模組

# # # 匯入time模組 時間模組常用的模組
# # # 在ui自動化中經常使用sleep() 執行緒等待括號中的就是秒數
# import time # 匯入time模組
# # '''如匯入某個模組後,模組沒有爆紅且有聯想,且呈現灰暗色
# # 則表示當前模組成功匯入到此檔案中,且還沒有被引用到'''
# print('寶安3班大佬!!')
# time.sleep(10) #執行緒等待sleep(s)
# print('寶安3班大佬都是萬元戶')

匯入time模組中所有的函式,類,方法

time 模組

from time import sleep #指定匯入
#from time import * #匯入當前模組中的所有
print('多測師大佬!!')
sleep(4)
print('寶安大佬都是萬元戶')

import time #匯入time模組
from time import * #匯入time模組
from time import sleep as s #通過as取別名



# sleep(3) #休眠 單位是秒
# time.sleep(3)
# sleep(3)

# # # asctime() #轉換為asc碼顯示當前時間
import time #匯入time模組
#from time import * #匯入time模組
print(time.asctime())
#print(asctime())


#import time #匯入time模組
from time import * #匯入time模組
#print(time.strftime('%Y-%m-%d-%H-%M-%S'))
print(strftime("%Y-%m-%d-%H-%M-%S"))
# # # 2020-09-08-10-20-23

案例

aaa模組中

def fun(a,b):
return a+b
def fun1(a,b):
return a*b
def fun2(a,b):
return a/b
def fun3(a,b):
return a-b

bbb模組中

import aaa
c=aaa.fun1(1,2)
print (c)

b=aaa.fun3(1,2)
print (b)

# #
#random  模組

import random #匯入random模組#
print(random.random())

import random
print(random.randint(1,5))
 根據給與的值:隨機取一位 取值的位數包含開始位和結束位會重複
取值範圍不會低 起始位,也不會高於結束位

import random
print(random.randrange(1,100,2)) # 指定生成奇數

import random
print(random.randrange(0,100,2)) #指定生成偶數
生成指定的奇數和偶數:隨機取一個值
根據range函式:起始,結束,隔位(只能取一個,range中可以去所有)
案例:

a=[1,2,3,4,5,6,7,8,9,10] #定義一個列表
print(random.sample(a,5)) #抽樣

a =[1,2,3,4,5]
print(random.choice(a)) #注意是L
# # # # # 在當前列表中隨機取一個值

a =[1,2,3,4,5,6]
random.shuffle(a)
print(a)
# # # 隨機取洗牌把列表中的值無規律的輸出


string

# '''1、digits
# 生成0-9的整數
# #print(string.digits) #0123456789

4、hexdigits
# 生成0-9的整數+大小寫的a-f和A-F'''
# print(string.hexdigits)#0123456789abcdefABCDEF

# 2、ascii_uppercase
# 生成26個大寫字母
# print(string.ascii_uppercase) #ABCDEFGHIJKLMNOPQRSTUVWXYZ

# 3、ascii_lowercase
# 生成26個小寫字母


print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz

print(string.ascii_letters)  #生成所有的大小寫字母
#顯示結果:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

print(string.printable)

作業:

#1、使用random模組隨機生成手機號碼、自己定義手機號碼開頭的前三位
#2、用random模組隨機生成6位數驗證碼'''