赞
踩
- def sayHello():
- """问候"""
- print('Hi')
-
- sayHello() # Hi
- def sayHello(friend):
- print("Hi," + friend.title() + '!')
-
- sayHello("Tom") # Hi,Tom!
- sayHello("Carl") # Hi,Carl!
关联方式:基于实参的顺序
- def score_info(stu_name, stu_score):
- print(stu_name.title() + ": " + stu_score)
-
- score_info('tom', '85')
- score_info('carl', '90')
- # Tom: 85
- # Carl: 90
- def group(user_name, user_age):
- print(user_name.title() + ': ' + user_age)
-
- group(user_name='tom', user_age='20')
- group(user_age='25', user_name='carl')
- # Tom: 20
- # Carl: 25
使用默认值,在形参列表里,必须先列出非默认值的形参,再是有默认值的形参。调用函数中的实参按位置实参来处理。
- def score(user_name, user_grade='A'):
- print(user_name.title() + ": " + user_grade)
-
- score('Ann')
- score('mary', 'B')
- score(user_name='carl')
- score(user_name='potter', user_grade='C')
- score(user_grade='C', user_name='lily')
- score('jack', user_grade='B')
- # Ann: A
- # Mary: B
- # Carl: A
- # Potter: C
- # Lily: C
- # Jack: B
- def user(username, age):
- info = username + ": " + age
- return info.title()
-
- user1 = user('tom', '13')
- print(user1)
- # Tom: 13
注意:
给实参指定一个默认值—空字符串,并移到形参列表末尾
python对于空字符串的布尔值为False
- def user_info(user_name, user_age, user_gender=''):
- if user_gender:
- info = user_name.title() + ": " + user_age + " " + user_gender
- else:
- info = user_name.title() + ": " + user_age
- return info
-
- user1 = user_info('tom', '18')
- print(user1)
- user2 = user_info('jack', '24', 'female')
- print(user2)
- # Tom: 18
- # Jack: 24 female
- def user(user_name, user_age, user_gender=''):
- person = {
- 'name': user_name.title(),
- 'age': user_age
- }
- if user_gender:
- person['gender'] = user_gender
- return person
-
- user1 = user('tom', '20', 'female')
- user2 = user('jack', '19')
- print(user1)
- print(user2)
- # {'name': 'Tom', 'age': '20', 'gender': 'female'}
- # {'name': 'Jack', 'age': '19'}
- def user(user_name, user_age):
- info = user_name.title() + ": " + str(user_age)
- return info
-
- while True:
- print("if you want to quit:'z'")
- userName = input('userName: ')
- if userName == 'z':
- break
- userAge = input('userAge: ')
- if userAge == 'z':
- break
- info = user(userName, userAge)
- print(info)
-
- # if you want to quit:'z'
- # userName: tom
- # userAge: 18
- # Tom: 18
- # if you want to quit:'z'
- # userName: z
- def sayHello(names):
- for name in names:
- print('Hello, ' + name.title() + '!')
-
- users = ['tom', 'jack']
- sayHello(users)
- # Hello, Tom!
- # Hello, Jack!
注意:python在函数中对列表做的任何修改都是永久性的
- def registerFun(unregisterList, registeredList):
- while unregisterList:
- registeredList.append(unregisterList.pop())
-
- def showRegistered(registeredList):
- for user in registeredList:
- print(user.title() + " has already registered!")
-
- unregisterList = ['tom', 'jack']
- registeredList = []
- registerFun(unregisterList, registeredList)
- showRegistered(registeredList)
- # Jack has already registered!
- # Tom has already registered!
注意:如果不想修改原始列表,在传递列表时可以传递列表的副本。传实参List时,使用List[:]即可。
- def registerFun(unregisterList, registeredList):
- while unregisterList:
- registeredList.append(unregisterList.pop())
-
- def showRegistered(registeredList):
- for user in registeredList:
- print(user.title() + " has already registered!")
-
- def showUnregister(unregisterList):
- for user in unregisterList:
- print(user)
-
- unregisterList = ['tom', 'jack']
- registeredList = []
- registerFun(unregisterList[:], registeredList)
- showRegistered(registeredList)
- # Jack has already registered!
- # Tom has already registered!
-
- print("Unregister users:\n")
- showUnregister(unregisterList)
- # tom
- # jack
注意:形参*param,表示将所有实参放入名为param的空元组中去。
- def get_users(*username):
- print(username)
-
- get_users('tom', 'jack', 'carl')
- get_users('mary')
- # ('tom', 'jack', 'carl')
- # ('mary',)
注意:python先匹配位置实参和关键字实参,再把余下的实参都收集到最后一个形参中,所以要把任意数量实参的形参放在函数定义中参数的最后位置。
- def comLanguage(number, *languageList):
- print(number)
- print(languageList)
-
- comLanguage(3, 'python', 'Java')
- comLanguage(1, 'c++')
-
- # 3
- # ('python', 'Java')
- # 1
- # ('c++',)
注意:我们想使用任意数量的关键字实参,则用**param,python对其创建了一个以param为名的空字典,用来接受任意数量的关键字实参。
- def user_info(user_name, user_age, **user_info):
- userInfo = {'userName': user_name.title(), 'userAge': user_age}
- for k, v in user_info.items():
- userInfo[k] = v
- return userInfo
-
- user = user_info('tom', 15, user_gender='male', user_hobby='soccer')
- print(user)
- # {'userName': 'Tom', 'userAge': 15,
- # 'user_gender': 'male', 'user_hobby': 'soccer'}
注意:你有一个模块a.py,并且想在b.py中使用a.py里的所有函数,则可以这样使用:a.function_name()即可。
hello_world.py中:
- def sayHello():
- print('你好')
apply.py中:
- import hello_world
- hello_world.sayHello() # 你好
注意:使用这种方法导入模块中的特定函数,可用from 模块名 import 函数1,函数2...。使用函数时,直接用函数名,不需要再用模块名.函数名了。
hello_world.py中:
- def sayHello():
- print('你好')
-
- def user():
- print('用户你好')
-
- def user1():
- print('用户1你好')
apply.py中:
- from hello_world import sayHello, user
-
- sayHello()
- user()
- # 你好
- # 用户你好
注意:关键字as将函数命名为你所提供的别名,语法:from 模块名 import 函数名 as 别名
apply.py中:
- from hello_world import sayHello as sH, user as us
-
- sH()
- us()
- # 你好
- # 用户你好
注意:import 模块名 as 别名
apply.py中:
- import hello_world as hw
-
- hw.sayHello()
- # 你好
注意:from 模块名 import *,使用模块里的函数时直接用函数名去调用
apply.py中:
- from hello_world import *
-
- sayHello()
- user()
- # 你好
- # 用户你好
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。