当前位置: 首页 > wzjs >正文

四川省建设厅网站c盘优化大师

四川省建设厅网站,c盘优化大师,花瓣按照哪个网站做的,前端开发规范一:定义函数 #!/bin/python3def greet_user(username):print(f"hello, {username.title()}!")greet_user(jesse)# RESULThello, Jesse!使用关键字def来告诉Python要定义一个函数。这是函数定义,向Python指出了函数名,还可能在圆括…

一:定义函数

#!/bin/python3def greet_user(username):print(f"hello, {username.title()}!")greet_user('jesse')# RESULT
'''
hello, Jesse!
'''

使用关键字def来告诉Python要定义一个函数。这是函数定义,向Python指出了函数名,还可能在圆括号内指出函数为完成任务需要什么样的信息。

二:传递实参

1、位置实参

调用函数时,Python必须将函数调用中的每个实参都关联到函数定义中的一个形参。为此,最简单的关联方式是基于实参的顺序。这种关联方式称为位置实参。

def describe_pet(animal_type, pet_name):print(f"I have a {animal_type}.")print(f"My {animal_type}'s name is {pet_name}.")describe_pet('hamster', 'harry')
describe_pet('dog', 'willie')
# RESULT
'''
I have a hamster.
My hamster's name is harry.
I have a dog.
My dog's name is willie.
'''

2、关键字实参

关键字实参是传递函数的名称值对。

describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='dog', pet_name='willie')

3、默认值

def describe_pet(pet_name, animal_type='dog',):print(f"I have a {animal_type}.")print(f"My {animal_type}'s name is {pet_name}.")describe_pet('willie')
# RESULT
'''
I have a dog.
My dog's name is willie.
'''

4、等效的函数调用

describe_pet('willie')
describe_pet(pet_name='willie')describe_pet('willie', 'dog')
describe_pet(pet_name='willie', animal_type='dog')
describe_pet(animal_type='dog', pet_name='willie')

三:返回值

def get_formatted_name(first_name, last_name, middle_name=''):full_name = f"{first_name} {middle_name} {last_name}"return full_name.title()full_name = get_formatted_name('jimi', 'hendrix')
print(full_name) # RESULT
'''
Jimi  Hendrix
'''

四:传递列表

def greet_users(names):for name in names:msg = f"Hello, {name.title()}"print(msg)usernames=['hannah', 'ty', 'margot']
greet_users(usernames)# RESULT
'''
Hello, Hannah
Hello, Ty
Hello, Margot
'''

1、在函数中修改列表

将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永久性的。

def print_models(unprinted_designs, completed_models):while unprinted_designs:current_design = unprinted_designs.pop()print(f"Printing model: {current_design}")completed_models.append(current_design)def show_completed_models(completed_models):for completed_model in completed_models:print(completed_model)unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
# RESULT
'''
Printing model: dodecahedron
Printing model: robot pendant
Printing model: phone case
dodecahedron
robot pendant
phone case
'''

2、禁止函数修改列表

为了解决向函数传递参数并被修改而丢失之前内容的问题,可向函数传递列表的副本而非原件。这样,函数所做的任何修改都只影响副本,而原件丝毫不受影响。

切片表示法[:]创建列表副本,function(list_name[:])

def print_models(unprinted_designs):while unprinted_designs:current_design = unprinted_designs.pop()print(f"Printing model: {current_design}")def show_unprinted_designs(unprinted_designs):for unprinted_design in unprinted_designs:print(f"unprinted designs:{unprinted_design}")unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
print_models(unprinted_designs[:])
show_unprinted_designs(unprinted_designs)
# RESULT
'''
Printing model: dodecahedron
Printing model: robot pendant
Printing model: phone case
unprinted designs:phone case
unprinted designs:robot pendant
unprinted designs:dodecahedron
'''

五:传递任意数量的实参

对于预先不知道函数需要接受多少个实参的时候,Python允许函数从调用语句中收集任意数量的实参。例如函数只有一个形参*toppings

def make_pizza(*toppings):print(toppings)make_pizza('pepperoni')
make_pizza('mushrooms', 'green perppers', 'extra cheese')
# RESULT
'''
('pepperoni',)
('mushrooms', 'green perppers', 'extra cheese')
'''

1、结合使用位置实参和任意数量实参

如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。

def make_pizza(size, *toppings):print(f"Making a {size}-inch pizza with the following toppings:")for topping in toppings:print(f"- {topping}")make_pizza(15, 'pepperoni')
make_pizza(21, 'mushrooms', 'green perppers', 'extra cheese')
# RESULT
'''
Making a 15-inch pizza with the following toppings:
- pepperoni
Making a 21-inch pizza with the following toppings:
- mushrooms
- green perppers
- extra cheese
'''

2、使用任意数量的关键字实参

有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况,可将函数编写成能够接受任意数量的键值对。

def build_profile(first, last, **user_info):user_info['first_name'] = firstuser_info['last_name'] = lastreturn user_infouser_profile = build_profile('albert', 'einstein',location='princeton',field='physics')
print(user_profile)
# RESULT
'''
{'location': 'princeton', 'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein'}
'''

函数build_profile()的定义要求提供名和姓,同时允许根据需要提供任意数量的键值对。

形参**user_info中的两个星号让Python创建一个名为user_info的空字典,并将收到的所有名称值对放到这个字典中。

六:将函数存储在模块中

将函数存储在称为模块的独立文件中,再将模块导入到主程序中。import语句允许在当前运行的程序文件中使用模块中的代码。

1、导入整个模块

要让函数时可导入的,得先创建模块。模块的扩展名为.py文件,包含要导入到程序中的代码。

只需要编写一条import语句并在其中指定模块名,就可在程序中使用该模块中的所有函数。

# pizza.pydef make_pizza(size, *toppings):print(f"Making a {size}-inch pizza with the following toppings:")for topping in toppings:print(f"-{topping}")
# make_pizza.pyimport pizzapizza.make_pizza(15, 'peperoni')
pizza.make_pizza(21, 'mushrooms', 'green peppers', 'extra cheese')# RESULT
'''
Making a 15-inch pizza with the following toppings:
-peperoni
Making a 21-inch pizza with the following toppings:
-mushrooms
-green peppers
-extra cheese
'''

2、导入特定的函数

还可以导入模块中的特定函数,这种导入方法的语法如下:

from module_name import function_name

通过用逗号分隔函数名,可根据需要从模块中导入任意数量的函数:

from module_name import function_0, function_1, function_2

对于前面的make_pizza.py来说,如果只想导入要使用的函数,可以这样做:

from pizza import make_pizzamake_pizza(15, 'peperoni')
make_pizza(21, 'mushrooms', 'green peppers', 'extra cheese')

3、使用as给函数指定别名

如果导入函数的名称可能与程序中的现有名称冲突,或者函数名字太长,可指定简短而独一无二的别名:函数的另一个名称,类似于外号。要给函数取这种特殊外号,需要在导入它时指定。

from pizza import make_pizza as making_pizzamaking_pizza(15, 'peperoni')
making_pizza(21, 'mushrooms', 'green peppers', 'extra cheese')

4、使用as给模块指定别名

还可以给模块指定别名。

import pizza as pp.make_pizza(15, 'peperoni')
p.make_pizza(21, 'mushrooms', 'green peppers', 'extra cheese')

5、导入模块中的所有函数

from pizza import *make_pizza(15, 'peperoni')
make_pizza(21, 'mushrooms', 'green peppers', 'extra cheese')
http://www.dtcms.com/wzjs/14004.html

相关文章:

  • 邯郸城乡建设部网站首页百度推广手机登录
  • 初中学习网站大全免费黄页88推广多少钱一年
  • 青岛做网站大公司seo竞价推广
  • 免费做宣传的网站是搜索引擎优化怎么做的
  • 网站建设 中企动力 石家庄我想接app注册推广单
  • 网络设计原则是什么沈阳关键词seo排名
  • 微能力者恶魔网站谁做的互联网推广是做什么的
  • WordPress底部添加音乐seo如何快速出排名
  • 中国做陶壶的网站有哪些seo网站优化培训班
  • 白云手机网站建设百度推广登录入口登录
  • 手机电影网站源码模板雅虎搜索引擎中文版
  • 个人网页设计欣赏作品北京seo编辑
  • 中国建站平台网最新热搜新闻
  • 公司建设网站费用百度风云排行榜
  • 滨州聊城网站建设网络营销有哪些模式
  • 玉林英文网站建设最近的国内新闻
  • 下载的html文件打开乱码北京搜索引擎优化管理专员
  • 网站营运西安seo优化公司
  • 免费海外网站cdn加速怎么开自己的网站
  • 网站建设怎么做分录主流搜索引擎有哪些
  • 上海建设网站便宜的app推广联盟平台
  • 台州高端网站设计产品推广方案范文500字
  • 怎样把建好的网站上传到互联网seo刷关键词排名免费
  • 微信软件如何开发seo软件代理
  • 政府网站集约化建设存在问题企业seo网站推广
  • wordpress 数据库sqlseo按照搜索引擎的
  • 免费下载现成ppt网站网站安全检测中心
  • 泉州做网站排名推广普通话手抄报模板可打印
  • 大众点评网怎么做团购网站广告推销
  • 云南旅游网站建设公司淘宝代运营公司排名