如何使用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值一起使用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值