於上一篇作了很基礎的python內容介紹,接下來將作一些關於python應用的語法,讓整個流程能自動化執行。
print()
是內建函數。def
關鍵字,後接函數名稱。
def display_investigation_message():
()
和冒號 :
。def display_investigation_message():
print("調查活動")
display_investigation_message()
def display_investigation_message():
print("investigate activity")
application_status = "potential concern"
email_status = "okay"
if application_status == "potential concern":
print("application_log:")
display_investigation_message()
if email_status == "potential concern":
print("email log:")
display_investigation_message()
application_log:
investigate activity
def func1():
func1() # 會導致無限迴圈
參數:在函數定義中用作計算的變數。
def remaining_login_attempts(maximum_attempts, total_attempts):
print(maximum_attempts - total_attempts)
maximum_attempts
和 total_attempts
是參數。實參:在呼叫函數時,傳入函數的資料。
remaining_login_attempts(3, 2) # 3 和 2 是實參
return 語句:用於從函數中返回值。
def remaining_login_attempts(maximum_attempts, total_attempts):
return maximum_attempts - total_attempts
device_id = "7ad2130bd" # 全域變數
def greet_employee(name):
total_string = "歡迎 " + name # 局部變數
return total_string
username = "elarson" # 全域變數
def greet():
username = "bmoreno" # 局部變數
print("2: " + username)
greet()
print("3: " + username) # 仍然是全域變數的值
print()
函數month = "September"
print("Investigate failed login attempts during", month, "if more than", 100)
type()
函數print(type("security"))
print(type(7))
print(type("This is a string"))
<class 'str'>
max()
和 min()
函數max()
傳回輸入中的最大值,min()
傳回輸入中的最小值。time_list = [12, 2, 32, 19, 57, 22, 14]
print(min(time_list)) # 傳回 2
print(max(time_list)) # 傳回 57
sorted()
函數time_list = [12, 2, 32, 19, 57, 22, 14]
print(sorted(time_list)) # 傳回 [2, 12, 14, 19, 22, 32, 57]
print(time_list) # 原始列表未改變
sorted()
函數不能處理包含多種資料類型的列表(例如 [1, 2, "hello"]
)。print()
:將參數列印到螢幕。type()
:傳回參數的資料類型。min()
和 max()
:分別傳回可迭代物件的最小和最大值。sorted()
:對可迭代物件的元素進行排序,並傳回排序後的結果。re
:用於搜尋模式。csv
:處理 .csv
檔案。glob
和 os
:與命令列互動。time
和 datetime
:處理時間戳記。statistics
:進行統計計算,如 mean()
和 median()
函數。import
:導入整個模組後,可以使用該模組內的所有函數。
import statistics
monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
mean_failed_attempts = statistics.mean(monthly_failed_attempts)
print("mean:", mean_failed_attempts)
from...import
:僅導入模組中的特定函數後,無需指定模組名稱即可直接呼叫函數。
from statistics import mean, median
monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
mean_failed_attempts = mean(monthly_failed_attempts)
median_failed_attempts = median(monthly_failed_attempts)
print("mean:", mean_failed_attempts)
print("median:", median_failed_attempts)
%pip install
安裝庫。
%pip install numpy
import
導入:
import numpy
re
、csv
、os
、glob
、time
、datetime
和 statistics
。import
導入整個模組。from...import
導入特定函數。numpy
、BeautifulSoup
等。#
開頭,適合簡單註解。
# 列印「computer_assets」清單的元素
computer_assets = ["laptop1", "desktop20", "smartphone03"]
for asset in computer_assets:
print(asset)
#
或使用三重引號 """
。
"""
remaining_login_attempts() 函數有兩個整數參數,
傳回剩餘登入嘗試次數
"""
count = 0
login_status = True
while login_status == True:
print("再試一次。")
count = count + 1
if count == 4:
login_status = False
username = "bmoreno"
login_attempts = 5
username_list = ["bmoreno", "tshah"]
def remaining_login_attempts(max_attempts, total_attempts):
return max_attempts - total_attempts