如何使用Def在Python中定义,使用函数?

Functions are fundamental feature of Python programming language. Functions provides reusability of code parts. Functions provides some abstraction from code. We can define some code block and use it with a single line without copy and pasting the whole code block.In this tutorial we will look how to define and use Python functions or methods.

函数是Python编程语言的基本功能。 函数提供了代码部分的可重用性。 函数提供了一些来自代码的抽象。 我们可以定义一些代码块并在一行中使用它,而无需复制和粘贴整个代码块。在本教程中,我们将研究如何定义和使用Python函数或方法。

使用Def定义功能 (Define Function with Def)

def keyword is used to identify function start in python. After the def keyword we provide the function name and parameters. Parameters are provided in brackets ( .. ) . Parameters are separated with commas , . Parameters are optional and if we do not need them we can omit them. Functions definition ends with double dot : .

def关键字用于标识python中的函数启动。 在def关键字之后,我们提供函数名称和参数。 参数在方括号( .. ) 。 参数用逗号分开, 。 参数是可选的,如果我们不需要它们,我们可以忽略它们。 函数定义以双点结尾:

After the first line we provide function body or code block. Function body is indented to specify the body area. Here is the syntax of the function definition.

在第一行之后,我们提供函数主体或代码块。 函数主体缩进以指定主体区域。 这是函数定义的语法。

def FUNCTION_NAME(PARAMETER1, PARAMETER2, ...):
   CODE1
   CODE2
   ...

Now we will make an example to learn functions. We will create a functions where it has no parameter and only single line of code in its body or code block. The functions name will be myprint .

现在我们将举一个学习函数的例子。 我们将创建一个函数,该函数的主体或代码块中没有参数,只有一行代码。 函数名称将为myprint

def myprint(): 
   print("I am working boss")

This is a simple for python but important for us.

这对python很简单,但对我们很重要。

通话功能 (Calling Function)

Calling functions are easier than creating them. We will just provide the name of the function and add brackets end of this name. If we need to provide parameters we can put them into brackets.

调用函数比创建它们容易。 我们将只提供函数的名称,并在该名称的后面加上方括号。 如果需要提供参数,可以将其放在方括号中。

In this example we will call the function, myprint , we have previously created. As it has no parameter we do not put anything into brackets.

在此示例中,我们将调用我们先前创建的函数myprint 。 由于它没有参数,因此我们不会将任何内容放在方括号中。

myprint()

带参数的功能 (Functions With Parameters)

What makes functions strong is parameters. We can provide any number of parameters in any data type. Now create an example which have two parameters named a and b . Parameters type are integer. This function named is sum . This function will sum up given parameters.

使功能强大的是参数。 我们可以在任何数据类型中提供任意数量的参数。 现在创建一个示例,该示例具有两个名为ab参数。 参数类型是整数。 此函数名为sum 。 此函数将汇总给定的参数。

def sum(a,b): 
   print(a+b)

返回功能 (Return Function)

In previous example we have called the function and print some text to the output. Sometimes this is not we want. We may need to get or return some data but not put into output. We can use return in order to return data like string, integer, object, list etc. from functions. We will just put return into function with what we want to return.

在前面的示例中,我们调用了该函数并将一些文本打印到输出中。 有时这不是我们想要的。 我们可能需要获取或返回一些数据,但不将其放入输出中。 我们可以使用return来从函数返回诸如字符串,整数,对象,列表等数据。 我们只是将return与所需的return值一起使用。

LEARN MORE  Php - Class
了解更多PHP-类

In this example we will not print sum and return from function and then print it.

在此示例中,我们将不打印求和并从函数返回然后再打印它。

def sum(a,b): 
   return(a+b)
Return Function
Return Function
返回功能

Return generally put into the end of the function but there is no restriction about its location. We can use return multiple times. Multiple usage with if-else is popular.

return通常放在函数的末尾,但对其位置没有限制。 我们可以多次使用returnif-else多种用法很受欢迎。

空功能 (Empty Function)

Function must have code blocks or bodies to work  correctly. But in some situations we have not complete the function bodies but want to create functions. We can call these functions empty functions. We can use pass keyword to provide empty body which will do nothing.

函数必须具有代码块或主体才能正常工作。 但是在某些情况下,我们还没有完成功能主体,而是想创建功能。 我们可以将这些函数称为空函数。 我们可以使用pass关键字来提供不做任何事情的空主体。

def iamempty(name):   
   pass
Empty Function
Empty Function
空功能

As we can see there is operation or output from function named iamempty

我们可以看到名为iamempty函数有操作或输出

关键字参数(Keyword Arguments)

While providing arguments or parameters the sequence is important. We need to put parameters in required sequence according to the function definition. But there is alternative usage where we do not need to comply with sequence. We can also skip some parameters and use their default values. We will provide parameters with their keyword.

在提供自变量或参数时,顺序很重要。 我们需要根据功能定义按要求的顺序放置参数。 但是还有其他用法,我们不需要遵守顺序。 我们还可以跳过一些参数并使用其默认值。 我们将为其关键字提供参数。

def sayhello(name,age):                  
   print("Hello {},{}").format(name,age)
Keyword Arguments
Keyword Arguments
关键字参数

We called function like below and all of them created same output.

我们像下面这样调用function,它们全部创建了相同的输出。

sayhello(name="ismail",age=35)

sayhello(age=35,name="ismail")

默认参数 (Default Arguments)

While calling functions we need   to provide all arguments or parameters to the function. If we  do not provide we  will get error. In some situations some parameters may be the same most of time and providing them in each function call is a trivial task. In order to make this type of usage more practical we can set some default values for arguments and while calling them if these parameters are not defined default values can be assumed by the function.

在调用函数时,我们需要向函数提供所有参数或参数。 如果我们不提供,则会出现错误。 在某些情况下,某些参数在大多数情况下可能是相同的,因此在每个函数调用中提供这些参数都是一项琐碎的任务。 为了使这种用法更加实用,我们可以为参数设置一些默认值,如果未定义这些参数,则在调用它们时可以由函数采用默认值。

In this example we will assume name as adam if it is not provided. Default arguments are specified after normal arguments in function definition.

在此示例中,如果未提供名称,则将其假定为adam 。 默认参数在函数定义中的普通参数之后指定。

def sayhello(age,name='adam'):           
   print("Hello {},{}").format(name,age)
Default Arguments
Default Arguments
默认参数

可变长度参数(Variable Length Arguments)

Up to now we have defined functions with specific number of arguments. We strictly provided the parameters. But sometimes this is not a solution and prevent us to work with multiple variable length arguments. We can use variable length arguments to address this. We will put variable name argument at the end of parameters and put asterisk left side like *var .

到目前为止,我们已经定义了带有特定数量参数的函数。 我们严格提供了参数。 但这有时不是解决方案,并且使我们无法使用多个可变长度参数。 我们可以使用变长参数来解决这个问题。 我们将变量名参数放在参数的末尾,并在星号的左侧放置*var

In this example we will create a function with variable argument.

在此示例中,我们将创建一个带有可变参数的函数。

def sayhello(name,age,*other): 
   print("Hello {},{}").format(name,age) 
   for var in other: 
      print(var)
Variable Length Arguments
Variable Length Arguments
可变长度参数

We see that we loop all provided options arguments and print the to the terminal. The count of arguments is not important .

我们看到我们循环了所有提供的选项参数,并将其打印到终端。 争论的数量并不重要。

LEARN MORE  C Variables and Definition
了解更多C变量和定义

翻译自: https://www.poftut.com/define-use-functions-python-def/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值