# 編寫一個求平均值的函數
def f(*m):
s = 0
lst = []
for i in m:
while str(i).isnumeric():
s += i
lst.append(i)
else:
print('輸入端內的內容包含有非法數字')
return (s / len(lst))
# 定義一個函數 函數的作用是把輸入的列表變成一連串字典的key
lst = eval(input('請輸入一個列表: '))
def f(lt):
return (dict.fromkeys(lt, 0))
print(f(lst))
創建一個模塊,包含一個階乘函數f1(n),一個列表刪除值函數f2(lst,x),一個等差求和函數f3(a,d,n)
def f1(n):
y = 1
for i in range(1,n+1):
y = y * i
return y
def f2(lst,x):
while x in lst:
lst.remove(x)
return lst
def f3(a,b,n):
an = a
s = 0
for i in range(n-1):
an = an + b
s = s + an
return s
import sys
sys.path.append('C:/Users/11786/Desktop')
#將自建的模塊寫入系統路徑裏面 注意此時反斜槓的方向
import tmodel
print(tmodel.f1(10))
print(tmodel.f2([1,2,3333,3333,4,5,6],3333))
print(tmodel.f3(10,5,5))
#python標準模塊 -random函數
import random
x = random.random()
y = random.random()
print(x, y*100)
m = random.randint(0,100)#隨機取整數值在0-100之間
print(m)
lst = list(range(20))#隨機在給定的範圍內選取值
s = random.choice(lst)
print(s)
sli = random.sample(lst,6)#隨機在給定的列表範圍內選取若干個數值
print(sli)
lst
random.shuffle(lst)
#pip:python工具包管理工具 用於安裝和卸載python工具包
#在cmd 中運行 ,安裝包 pip install xxx
#python函數模塊- time模塊
import time
for i in range(5):
print("hello")
time.sleep(0.11)#讓time語句執行的時間往後順延time.sleep()
print(time.ctime())#生成當時時間的時間戳 其類型為字符串
print(time.localtime( ))#將當前的時間轉為當前時區的struct_time wday0-6表示週日到週六
#ydat 1-366表示一年中的第幾天 isdst表示是否為夏令時 默認為-1
print(time.strftime('%Y-%m-%d %H:%M-%S',time.localtime()))
#time.strftime(a,b)
#a 為格式字符串格式
#b 為時間戳 一般為localtime()
#具體時間格式參照python基本語法裏面的
本文章為轉載內容,我們尊重原作者對文章享有的著作權。如有內容錯誤或侵權問題,歡迎原作者聯繫我們進行內容更正或刪除文章。