Python筆記--函式
阿新 • • 發佈:2018-12-13
本文講述python函式及基本使用。
1 python函式
- python函式是組織好,可複用功能的程式碼段。
- 格式:
def funciton_name(*argv):
print(para)
2 基本使用
- 定義並呼叫函式
#-*-coding:utf-8-*-
#定義函式
def greet_user(user):
print(user)
#呼叫函式
greet_user('xindaqi')
- 位置實參呼叫函式
#定義函式 def describe_pet(animal_type, pet_name): print("Animal type is " + animal_type) print("Animal name is " + pet_name) #位置實參 #按照順序賦值 describe_pet("Dog", "Li") describe_pet("Li", "Dog")
- 關鍵字實參呼叫函式
#關鍵字實參
#給關鍵字變數直接賦值
describe_pet(animal_type="Cat", pet_name="G")
#預設值:給形參賦值
def describe_goods(goods,made='China'):
print("I want to buy " + goods + "!")
print(goods + " made in " + made)
describe_goods(goods='iPhone')
- 函式返回值
#返回值 def return_0(user, age): info = user + " is " + age return info return_1 = return_0(user='xindaqi', age='22') print(return_1)
- 根據實際引數,傳入實參
#實參可選 def get_name(first_name, last_name, middle_name=''): if middle_name: full_name = first_name + ' ' + middle_name + ' ' + last_name else: full_name = first_name + ' ' + last_name return full_name.title() people_info = get_name('daqi', 'xin') print(people_info) people_info1 = get_name('john', 'lala', 'hi') print(people_info1)
- 呼叫函式,返回字典格式資料
#返回字典
def get_name_1(first_name, last_name):
people = {'first': first_name, 'last': last_name}
return people
people = get_name_1('daqi', 'xin')
print(people)
- 實參為列表呼叫函式
#實參傳遞列表
def get_name_2(names):
print(names)
usernames = ['xindq', 'xinxq', 'xinerqi']
get_name_2(usernames)
- 函式中修改列表及禁止修改列表
#函式中修改列表
for i in range(10):
print("==",end='')
print('\n')
def student_name(all_stus, new_stus):
while new_stus:
current_stu = new_stus.pop()
print(current_stu)
all_stus.append(current_stu)
def show_all_stus(all_suts):
for stu in all_suts:
print(stu)
new_stus = ['xindaqi', 'xinxiaoqi', 'xinerqi']
all_stus = []
#保持原列表
student_name(all_stus, new_stus[:])
print(new_stus)
#修改原列表
student_name(all_stus, new_stus)
print(new_stus)
show_all_stus(all_stus)
- 實參為任意數量呼叫函式
#任意數量的實參
def goods(*toppings):
for topping in toppings:
print("-" + topping)
goods('cup', 'shoes')
goods('box', 'pencil', 'paper', 'ruler')
- 位置實參和任意數量實參呼叫函式
#位置實參和任意數量實參
#函式接受不同型別實參,在函式定義時將接受任意數量實參的形參放在最後
def goods(num, *tops):
print(num)
for topping in tops:
print("-" + topping)
goods(12, 'cup', 'pencil')
- 任意數量關鍵字實參呼叫函式
#任意數量關鍵字實參
#**stu_info建立一個空字典,將接受的所有鍵-值對封裝在字典中
def stu_info(first, last, **stu_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in stu_info.items():
profile[key] = value
return profile
stu_info = stu_info('daqi', 'xin',
address='China',
email='[email protected]'
)
print(stu_info)
3 模組定義及使用
-
模組 Python模組是*.py檔案,包含定義的類、物件、方法和函式等python程式碼。 將第二章的程式碼封裝成fun.py模組。
-
匯入整個模組 import fun
-
匯入指定函式 from fun import stu_info
-
匯入全部函式 from fun import *
-
指定模組別名 import fun as F
-
使用 model_name.funciton_name()
-
程式碼段
import fun
fun.goods(250, 'cup')
from fun import stu_info
stu_info = fun.stu_info('daqi', 'xin', address='China')
from fun import *
goods(250, 'orange')
import fun as F
F.goods(250, 'breather cup')
4 總結
使用函式讓程式更簡潔,易讀,良好的函式命名規則有助於程式的測試和後期維護。建議將工程函式封裝使用。