网站做外链好嘛,免费网络短剧网站,广告推广平台哪个好,网站建设与管理下拉列表框复习python从入门到实践——函数function
函数是特别难的#xff0c;大家一定要好好学、好好复习、反复巩固。函数没学好#xff0c;会为后面造成很大困扰。 教科书中函数举例会稍微有点复杂。在此章复习中#xff0c;我将整理出容易疏漏和混淆的知识点#xff0c;并用最简…复习python从入门到实践——函数function
函数是特别难的大家一定要好好学、好好复习、反复巩固。函数没学好会为后面造成很大困扰。 教科书中函数举例会稍微有点复杂。在此章复习中我将整理出容易疏漏和混淆的知识点并用最简单的代码辅助大家理解。 本章涉及定义函数、实参与形参、return返回值、传递列表与切片、将函数导入模块。 文章目录 复习python从入门到实践——函数function1. 定义 def函数Syntax 2.实参与形参传递实参的方法可选实参通过*传递任意数量实参涉及字典 **可变关键字参数 3.return返回Syntax:Principle注意 4. 传递列表[:]切片含义 5.将函数导入模块 1. 定义 def函数
Syntax
def 函数():函数的具体表现(函数体)
函数() #调用区分函数和变量
函数 add_numbers(): 接受输入的参数(a,b)并且要有返回值return。后面会介绍。
def add_numbers(a, b):# 定义一个函数return a bresult add_numbers(3, 5)#使用函数
print(结果:, result)2.变量x,y 依靠储存各种数据。
# 定义两个变量
x 10
y 20sum_of_variables x y # 使用变量
print(变量之和:, sum_of_variables)2.实参与形参
形参类似于中文里概括性质的类别比如“同学” 实参是具体的例子比如“小明”属于”同学“”小明“是实参。
def great_user(username):显示问候语 #文本注释三引号描述函数做什么。print(fHello {username.title()}!) #函数的工作
great_user(Ashley) username 形参 Ashley 实参
传递实参的方法
1对应位置
def animal_lists(category,name):print(fMy {category}s name is {name.title()}.)
animal_lists(pig,feifei)2关键词用’连接
def animal_lists(category,name):print(fMy {category}s name is {name.title()}.)
animal_lists(namefeifei,categorypig)3最后一项是默认值
def animal_lists(name, categorydog,):#把默认值放到最后。print(fMy {category}s name is {name.title()}.)
animal_lists(namefeifei)可选实参
把可选可不选的参数放到最后一个 middle_name’ ’
def formatted_name(first_name,last_name,middle_name ):if middle_name:formatted_name f{first_name} {middle_name} {last_name}else:formatted_name f{first_name} {last_name}return formatted_name.title()
students formatted_name(Haifei,Wang)
print(students)
students formatted_name(Haifei,Wang,Pig)#最后一个是中间名
print(students)通过*传递任意数量实参
def feifei_behaviors(*behavior):#通过加星号调用多个实参print(f飞飞正在{behavior})
feifei_behaviors(拉粑粑)
feifei_behaviors(吃粑粑,吃屎,被茵茵看着拉屎)涉及字典 **可变关键字参数
def build_profile(first,last,**user_info):#**可变关键字参数除了First和last创建一个字典其中包含我们知道的有关用户的一切profile {}profile[first_name] firstprofile[last_name] lastfor key,value in user_info.items():profile[key] valuereturn profile
user_profile build_profile(albert,einstein,locationprinceton,fieldphysics)
print(user_profile)
3.return返回
Syntax:
def function_name():具体的statementreturn statement中的函数Principle a. The statements after the return() statement are not executed. 在return语句之后的语句不会被执行。 b. return() statement can not be used outside the function. return语句不能在函数外部使用。 c. If the return() statement is without any expression, then the NONE value is returned. 如果return语句没有任何表达式则返回NONE值。
举例
def formatted_name(first_name,last_name):formatted_name first_name last_namereturn formatted_name()
students formatted_name(Haifei,Wang)
print(students)结果: Haifei Wang
如果没有return语句
def formatted_name(first_name,last_name):formatted_name first_name last_name
students formatted_name(Haifei,Wang)
print(students)结果是None
注意
调用你定义的函数而不是变量
def city_country(city, country):full_name f{city},{country}return full_namewhile True:print(Please inter the city and country:)city input(city:)country input(country:)new_name city_country(city, country)#调用定义变量print(new_name) 4. 传递列表
普通方法 def循环发送列表调用列表
def greating_lists(names):#建立函数 # 问候 给列表中的用户打招呼for name in names:print(fHello! {name.title()}.)
usernames[feifei,ashley,tony]# 发送列表
greating_lists(usernames)#调用列表-实参[:]切片含义
基本 [start:stop:step] 用数学集合可以表示为[ ) 举例 [1:4] 从索引1包含到索引4不包含也就是索引1 2 3 这几个元素。
重要的容易忽略 表示列表序列 创建副本
original_list [10, 20, 30, 40, 50]# 使用切片创建副本
copied_list original_list[:]# 修改副本
copied_list[0] 100print(Original List:, original_list)
print(Copied List:, copied_list)原始列表 original_list 并没有被修改。这是因为切片创建了一个新的列表对象而不是直接引用原始列表。
5.将函数导入模块
Syntax from 文件 import 要导入的东西 import 原来的东西名字 as 你想改的名字 from 文件 import *所有函数 文章转载自: http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.wnhml.cn.gov.cn.wnhml.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.plnry.cn.gov.cn.plnry.cn