2021 02 24 Python(簡單函式)

2021-10-19 23:17:03 字數 2864 閱讀 4285

將一件事情的步驟封裝在一起並得到結果,函式名代表了這個函式要做的事情,函式體是實現函式功能的流程一種方法或者一種功能函式可以重複使用,通過函式名知道該函式的作用,所以這裡取乙個好聽的函式名是很有必要的

舉個例子:把大象裝進冰箱 ----->函式名

開啟冰箱,把大象放進去,關上冰箱----->函式體

內建函式:如id,max,int,str,ranage等自定義函式:自己開發實現想要的功能

簡單的案例:

students = ,

2: ,

3: ,

4: ,

5: }def check_user_info(**kwargs):

if "name" not in kwargs:

return "沒發生學生姓名"

if "age" not in kwargs:

return "沒發生學生年齡"

if "***" not in kwargs:

return "沒發生學生性別"

if "class_number" not in kwargs:

return "沒發生學生班級"

return true

def get_all_students():

for id_, value in students.items():

print("學號:{},姓名:{},年齡:{},班級:{},性別:{}".format(

id_, value["name"], value["age"], value["class_number"], value["***"]

))return students

# get_all_students()

def add_student(**kwargs):

check = check_user_info(**kwargs)

if check != true:

print(check)

return

id_ = max(students) + 1

students[id_] =

# add_student(name="xiaobai",age=19,class_number="a",***="boy")

# get_all_students()

def delete_student(student_id):

if student_id not in students:

print("{}並不存在".format(student_id))

else:

user_info = students.pop(student_id)

print("學號是{},{}同學的資訊已經被刪除了".format(student_id,

user_info["name"]))

# delete_student(1)

# add_student(name="xiaobai",age=19,class_number="a",***="boy")

# get_all_students()

def update_student(student_id, **kwargs):

if student_id not in students:

print("並不存在這個學號:{}".format(student_id))

check = check_user_info(**kwargs)

if check != true:

print(check)

return

students[student_id] = kwargs

print("同學資訊更新完畢")

update_student(1, name="dewei_zhang", age=16, ***="boy", class_number="b")

get_all_students()

def get_user_by_id(student_id):

return students.get(student_id)

print(get_user_by_id(3))

def search_users(**kwargs):

values = list(students.values())

key = none

value = none

result =

if "name" in kwargs:

key = "name"

value = kwargs[key]

elif "age" in kwargs:

key = "age"

value = kwargs[key]

elif "***" in kwargs:

key = "***"

value = kwargs[key]

elif "class_number" in kwargs:

key = "class_number"

value = kwargs[key]

else:

print("沒有發現搜尋的關鍵字")

return

for user in values:

if user[key] == value:

return result

print("------")

users = search_users(age="16")

print(users)

2021 02 24 python 基礎學習總結

識別符號 區分大小寫 字母或下劃線開頭 變數 可以是中文 保留字 false none true peg parser and as assert async await break class continue def del elif else except finally for from g...

極簡美學 python

檢查兩個字串的組成元素是不是一樣的 from collections import counter defsimilarconsistence first,second return counter first counter second print similarconsistence abbc...

Python級簡入門

注 需要有基本的程式設計基礎 第一步 hello world程式 print hello world 其中,print函式會列印 顯示 出雙引號與單引號中的文字內容。也可以列印出乙個變數,但是不能加雙引號或單引號。第二步 input函式,如 input answer it here 其中,answe...