Dear learners, in this tutorial we are going to learn Python Function and Arguments. Previously we learned about Python loop. Hope that you will enjoy learning.

亲爱的学习者,在本教程中,我们将学习Python函数和参数。 先前我们了解了Python循环 。 希望您会喜欢学习。

Python函数和参数 (Python Function and Arguments)

Basically function is used to do some specific work. We can split a large task to some work distribution and do them separately which will enable us to debug program easily. The basic structure of a Python function is given below:

基本上,功能用于完成一些特定的工作。 我们可以将大型任务划分为一些工作分配,然后分别进行处理,这使我们可以轻松地调试程序。 Python函数的基本结构如下:

def function_name( arguments ) :#perform tasks

Python function arguments can be one or more. If it is more than one, they has to be separated by comma. For example, you want to calculate result = x * ( y + z ).

Python函数参数可以是一个或多个。 如果不止一个,则必须用逗号分隔。 例如,您要计算result = x *(y + z)

Let, p = y + x
So, result = x * p

p = y + x
因此, 结果= x * p

First, we have to calculate p, and then, putting the value of p we have to calculate result. To do so, we will create separate functions. The following code will illustrate about Python function.

首先,我们必须计算p ,然后,将p的值放入我们必须计算result 。 为此,我们将创建单独的功能。 以下代码将说明有关Python函数的信息。

'''
create a function for adding two variables. It will be needed to calculate p=y+z
'''
def calculateP( y , z ) :return int( y ) + int( z )'''
create a function for multiplying two variables. It will be needed to calculate p=y+z
'''
def calculateResult( x, p ) :return int( x ) * int( p )#Now this is the beginning of main program
x = input('x: ')
y = input('y: ')
z = input('z: ')#Now Calculate <strong>p</strong>
p = calculateP ( y , z )#Now calculate <strong>result</strong>
result = calculateResult( x , p )#Print the result
print(result)

The output of the following code will be

以下代码的输出将是

================== RESTART: /home/imtiaz/Desktop/pyDef.py ==================
x: 2
y: 2
z: 3
10
>>>

传递可变数量的参数 (Passing Variable Number of Arguments)

If you don’t know how many arguments you have to pass in the function then you can allow the function to take variable number of arguments. To do this you need to add an asterisk(*) right before your argument name. But there is some condition, that your special argument has to be the last argument of the function and it is allowed to have only one argument of that kind in one function. The following example will help you to understand the variable number of arguments in python function.

如果您不知道必须在函数中传递多少个参数,则可以允许该函数采用可变数量的参数。 为此,您需要在参数名称之前添加一个星号(*)。 但是有一个条件,您的特殊参数必须是该函数的最后一个参数,并且在一个函数中只能有一个这种类型的参数。 以下示例将帮助您了解python函数中可变数量的参数。

def varFunc(name,*args):print("This is the first argument "+str(name))#This print will make you understand that the args is a listprint(args)for item in args:print(item)print("First time:")
varFunc("of 1st function call",2, 3, 4, 5)print("Second time:")
varFunc("of 2nd function call","asd","Bcd")print("Third time:")
varFunc("and only argument of 3rd function call")

The Output will be:

输出将是:

将键值对作为可选的Python函数参数传递 (Passing Key-Value pair as Optional Python Function Arguments)

You can also pass arguments by keywords, so that the order you send argument does not matter. To do so, you have to add two asterisks(**) right before the special argument when you define a function. The following example will clear your concept.

您还可以通过关键字传递参数,因此发送参数的顺序无关紧要。 为此,在定义函数时,必须在特殊参数之前添加两个星号(**)。 以下示例将清除您的概念。

def varFunc(name, roll, **option):print("Name: "+name)print("Roll: "+str(roll))if "age" in option :print("Age: "+ str(option.get("age")))if "gender" in option:print("Gender: "+ str(option.get("gender")))print("First Person")
varFunc("Alice", 234, age=18, gender="female")print("\nSecond Person")
#See, the order of argument age and gender is different now
varFunc("Bob", 204, gender="male", age=21)print("\nThird Person")
#We will not pass age as and argument
varFunc("Trudy", 204, gender="male")

Output will be

输出将是

================== RESTART: /home/imtiaz/Desktop/key_value_arg.py ==================
First Person
Name: Alice
Roll: 234
Age: 18
Gender: femaleSecond Person
Name: Bob
Roll: 204
Age: 21
Gender: maleThird Person
Name: Trudy
Roll: 204
Gender: male
>>>

This is all about Python Function and Arguments. Hope that you understood well. For any query feel free to use the comment section below.
Keep you enthusiasm always up for learning new things!

这一切都与Python函数和参数有关。 希望你能理解。 对于任何查询,请随时使用下面的评论部分。
始终保持学习新事物的热情!

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/14256/python-function-arguments

Python函数和参数相关推荐

  1. [教程]Python函数的参数

    Python函数的参数 位置参数.默认值参数.可变参数.关键字参数和命名关键字参数 1 函数结构 def 函数名(参数):函数体返回值 2 位置参数 略 3 默认值参数 略 4 可变参数 # 可变长度 ...

  2. python 函数 默认参数

    python 函数 默认参数 def add(a=1, b=2): # a,b设置默认参数c = a + breturn c c = add(a=2) # 如果不填,就是默认参数 print(c) 4

  3. python函数不定参数求和

    # 先来看python函数定参数求和 def dup1(m ,n ,l):total = 0total = m + n + lreturn total print(dup1(4 ,6 ,8)) #打印 ...

  4. python函数中可变参数的传递方式是_详解Python函数可变参数定义及其参数传递方式...

    Python函数可变参数定义及其参数传递方式详解 python中 函数不定参数的定义形式如下 1. func(*args) 传入的参数为以元组形式存在args中,如: def func(*args): ...

  5. boost::python模块包装几个 C++ 函数 将二维数组操作为采用 NumPy 数组的 Python 函数作为参数

    boost::python模块包装几个 C++ 函数 将二维数组操作为采用 NumPy 数组的 Python 函数作为参数 实现功能 C++实现代码 实现功能 boost::python模块包装几个 ...

  6. Python函数的参数传递方式

    为什么80%的码农都做不了架构师?>>>    Python函数的参数传递方式 一.普通 def add(a,b):return a+b print(add(1,2)) #输出结果: ...

  7. python函数的参数可以接收哪些类型的数据_python强势来袭-0015-函数中的参数-送礼开始...

    接上一讲,这节我们专门介绍python函数的参数处理 本节主要内容 函数的形式参数和实际参数 函数的参数定义规则 函数参数的默认值 函数的可变参数 函数的关键字参数 函数的命名关键字参数 1.3. 函 ...

  8. python 函数中参数的传递方式(三分钟读懂)

    python 函数中参数的传递方式 第一种:指定默认值 def fn1(a = 1): # 函数中的(a)是形参 和外面的变量无关 (这里的是定义函数中指定默认值)print("fn1&qu ...

  9. python函数设置默认参数_深入讲解Python函数中参数的使用及默认参数的陷阱

    这篇文章主要介绍了Python函数中参数的使用及默认参数的陷阱,文中将函数的参数分为必选参数.默认参数.可变参数和关键字参数来讲,要的朋友可以参考下 C++里函数可以设置缺省参数,Java不可以,只能 ...

最新文章

  1. 云原生背景下故障演练体系建设的思考与实践—云原生混沌工程系列之指南篇
  2. 一个程序员面试因为吸烟而被拒
  3. CVPR 2021 | 基于随机标签的神经架构搜索
  4. EVENT:10218 dump uba of applied undo
  5. 服务器版dll修复工具,dll修复工具
  6. matlab混沌指数的计算,Matlab编程之混沌系统李雅普诺夫指数分析
  7. Excel批量复制公式、删除空白行
  8. 招标流程及注意事项_2016招投标流程及注意事项.ppt
  9. 鸿蒙方将腐皮雀跃而有,鸿蒙是谁:生于庄子,火于华为
  10. 【Unity3D】3dsmax中带Vray材质的3D模型的导入
  11. Photon与Unity核心技术之角色更换武器
  12. 智能家居普及的最大障碍:如何“排座次”
  13. 逃避追债?贾跃亭把法拉第未来股权转给了外甥,但他还有5套豪宅! | 焦点
  14. win7回收站右键没有清空回收站选项
  15. tweenmax笔记
  16. Error与Exception的异常定义以及简介(简单理解介绍是为了下一节的异常处理与捕捉)
  17. HTML 媒体(Media)
  18. * word 2000 与 word 2003 版本兼容性问题
  19. IOS NDDictionary使用中value遇到no summary如何判断为NULL
  20. 带AI的俄罗斯方块源码

热门文章

  1. 每天CookBook之JavaScript-032
  2. Debian Squeeze 安装
  3. 基于C#语言的可编程表达式计算器设计
  4. [转载] 包含对象的json格式_如何把JSON数据格式转换为Python的类对象?
  5. 欧拉线性筛 与 欧拉函数 + 几道例题
  6. MyEclipse的html页面 design视图中 关闭可视化界面
  7. 如何在centos7上安装源码包
  8. VS2017不能打开stdio.h等文件
  9. 杭州恒生数米基金网招聘1-3年本科.NET软件工程师
  10. asp.net页生命周期