编写分段函数子函数_编写自己的函数

本文介绍了如何根据需求编写自定义的分段函数子函数,适用于Python、C++、Java等编程语言,帮助开发者实现更灵活的代码逻辑。
摘要由CSDN通过智能技术生成

编写分段函数子函数

PYTHON编程 (PYTHON PROGRAMMING)

In Python, you can define your own functions.

在Python中,您可以定义自己的函数。

先决条件 (Prerequisites)

If you do not familiar with Python, the article below will give you a little bit of introduction to Python.

如果您不熟悉Python,则下面的文章将为您提供一些Python简介。

You will have the moment to follow these newfound skills in data science contexts.

您将有时间在数据科学环境中关注这些新发现的技能。

  1. define functions without parameters

    定义不带参数的功能
  2. define functions with a single parameter

    用单个参数定义函数
  3. define functions that return a single value and multiple values

    定义返回单个值和多个值的函数

To define a function, we begin with the keyword def followed by the function name, and a set of parentheses and a colon. This section of code is called a function header. To complete the function definition, you can write the function body and print the output.

为了定义一个函数,我们以关键字def开头,后跟函数名称,以及一组括号和一个冒号。 这部分代码称为函数标头 。 要完成功能定义,您可以编写功能主体并打印输出。

Image for post
Author 作者

The code inside the function body is executed if the function is called. When you call a function as you do with prebuild functions, this should return the value. What if you want to attach a parameter inside the function?

如果调用函数,则将执行函数体内的代码。 当您像使用预构建函数一样调用函数时,这应该返回值。 如果要在函数内附加参数怎么办?

Image for post
Author 作者

To add that functionality, you can add a parameter to the function definition in between the parenthesis. You see that we add a parameter num to the new function body as a variable. The function now accepts a single parameter and prints out its value. What if we do not want to print that value directly? Is it possible to return the value and assign it to a variable?

要添加该功能,可以在括号之间的功能定义中添加参数。 您会看到我们将参数num作为变量添加到了新函数体中。 该函数现在接受单个参数并输出其值。 如果我们不想直接打印该值怎么办? 是否可以返回值并将其分配给变量

Image for post
Author 作者

Your function can return the new value by adding the return keyword, followed by the value to return. We can assign to a variable when the result of the function called. Another essential aspect of writing functions in Python is docstrings. It is used to describe your function.

您的函数可以通过添加return关键字,后跟要返回的值来返回新值。 当函数的结果被调用时,我们可以分配一个变量 。 用Python编写函数的另一个重要方面是docstrings 。 它用来描述您的功能。

Image for post
Author 作者

These descriptions are your function documentation. Anyone who reads your functions will understand the function without having to trace through all the code. Function docstrings are placed in the next line after the function header and placed between triple quotation marks.

这些描述是您的功能文档。 读取您的函数的任何人都将理解该函数,而不必遍历所有代码。 函数文档字符串放在函数标题之后的下一行中,并放在三引号之间。

What if you want to pass multiple arguments to your functions and return not just one value, but various values from them?

如果您想将多个参数传递给函数并不仅返回一个值,还返回多个值,该怎么办?

You can achieve this by having your function to accept two parameters instead of just one. You may also consider changing your function name and docstrings to reflect this new behavior. You can call the function by giving two arguments because it has two parameters, as stated in the function header.

您可以通过让函数接受两个参数而不是一个参数来实现此目的。 您也可以考虑更改函数名称和文档字符串以反映此新行为。 您可以通过提供两个参数来调用该函数,因为它具有两个参数,如函数头中所述。

Image for post
Author 作者

The order in which the arguments are stated has to matches the order of the function header’s parameters. You can also specify your function to return multiple values. You can make this by creating objects which are known as tuples in your functions.

声明参数的顺序必须与函数头参数的顺序匹配。 您还可以指定函数以返回多个值。 您可以通过在函数中创建称为元组的对象来实现此目的。

A tuple may contain multiple values like a list. However, there are some differences:

元组可以包含多个值,例如列表。 但是,有一些区别:

  • Unlike a list, a tuple is immutable, and you cannot modify the values in a tuple once it has been created

    与列表不同,元组是不可变的,创建元组后就无法对其进行修改
  • While lists are declared using square brackets [], tuples are constructed using a set of parentheses ()

    虽然列表使用方括号[]声明,但元组是使用一组括号()构造的

You can unpack a tuple into numerous variables in one line, and assign it to the variables. Additionally, you can also access specific tuple elements as you do with lists. You can use zero-indexing to access the elements.

您可以在一行中将一个元组拆成多个变量 ,然后将其分配给这些变量 。 此外,您也可以像使用列表一样访问特定的元组元素。 您可以使用零索引来访问元素。

Are you ready to define your own functions?

您准备好定义自己的功能了吗?

关于作者 (About the Author)

Wie Kiang is a researcher who is responsible for collecting, organizing, and analyzing opinions and data to solve problems, explore issues, and predict trends.

Wie Kiang是一位研究人员,负责收集,组织和分析意见和数据以解决问题,探索问题和预测趋势。

He is working in almost every sector of Machine Learning and Deep Learning. He is carrying out experiments and investigations in a range of areas, including Convolutional Neural Networks, Natural Language Processing, and Recurrent Neural Networks.

他几乎在机器学习和深度学习的每个领域工作。 他正在许多领域进行实验和研究,包括卷积神经网络,自然语言处理和递归神经网络。

Connect on LinkedIn

LinkedIn上 连接

翻译自: https://towardsdatascience.com/writing-your-own-functions-40d381bd679

编写分段函数子函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值