编写分段函数子函数_编写自己的函数
編寫分段函數(shù)子函數(shù)
PYTHON編程 (PYTHON PROGRAMMING)
In Python, you can define your own functions.
在Python中,您可以定義自己的函數(shù)。
先決條件 (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.
您將有時間在數(shù)據(jù)科學環(huán)境中關(guān)注這些新發(fā)現(xiàn)的技能。
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.
為了定義一個函數(shù),我們以關(guān)鍵字def開頭,后跟函數(shù)名稱,以及一組括號和一個冒號。 這部分代碼稱為函數(shù)標頭 。 要完成功能定義,您可以編寫功能主體并打印輸出。
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?
如果調(diào)用函數(shù),則將執(zhí)行函數(shù)體內(nèi)的代碼。 當您像使用預構(gòu)建函數(shù)一樣調(diào)用函數(shù)時,這應該返回值。 如果要在函數(shù)內(nèi)附加參數(shù)怎么辦?
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?
要添加該功能,可以在括號之間的功能定義中添加參數(shù)。 您會看到我們將參數(shù)num作為變量添加到了新函數(shù)體中。 該函數(shù)現(xiàn)在接受單個參數(shù)并輸出其值。 如果我們不想直接打印該值怎么辦? 是否可以返回值并將其分配給變量 ?
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.
您的函數(shù)可以通過添加return關(guān)鍵字,后跟要返回的值來返回新值。 當函數(shù)的結(jié)果被調(diào)用時,我們可以分配一個變量 。 用Python編寫函數(shù)的另一個重要方面是docstrings 。 它用來描述您的功能。
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.
這些描述是您的功能文檔。 讀取您的函數(shù)的任何人都將理解該函數(shù),而不必遍歷所有代碼。 函數(shù)文檔字符串放在函數(shù)標題之后的下一行中,并放在三引號之間。
What if you want to pass multiple arguments to your functions and return not just one value, but various values from them?
如果您想將多個參數(shù)傳遞給函數(shù)并不僅返回一個值,還返回多個值,該怎么辦?
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.
您可以通過讓函數(shù)接受兩個參數(shù)而不是一個參數(shù)來實現(xiàn)此目的。 您也可以考慮更改函數(shù)名稱和文檔字符串以反映此新行為。 您可以通過提供兩個參數(shù)來調(diào)用該函數(shù),因為它具有兩個參數(shù),如函數(shù)頭中所述。
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.
聲明參數(shù)的順序必須與函數(shù)頭參數(shù)的順序匹配。 您還可以指定函數(shù)以返回多個值。 您可以通過在函數(shù)中創(chuàng)建稱為元組的對象來實現(xiàn)此目的。
A tuple may contain multiple values like a list. However, there are some differences:
元組可以包含多個值,例如列表。 但是,有一些區(qū)別:
- Unlike a list, a tuple is immutable, and you cannot modify the values in a tuple once it has been created 與列表不同,元組是不可變的,創(chuàng)建元組后就無法對其進行修改
- While lists are declared using square brackets [], tuples are constructed using a set of parentheses () 雖然列表使用方括號[]聲明,但元組是使用一組括號()構(gòu)造的
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?
您準備好定義自己的功能了嗎?
Other Interesting Articles#1 Function Arguments: Default, Keyword, and Arbitrary#2 Scope of Variable and LEGB Rule#3 Python: Procedural or Object-Oriented Programming?#4 Data Science with Python: How to Use NumPy Library#5 Do you have the Software Engineer and Data Scientist skills?關(guān)于作者 (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是一位研究人員,負責收集,組織和分析意見和數(shù)據(jù)以解決問題,探索問題和預測趨勢。
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.
他幾乎在機器學習和深度學習的每個領(lǐng)域工作。 他正在許多領(lǐng)域進行實驗和研究,包括卷積神經(jīng)網(wǎng)絡(luò),自然語言處理和遞歸神經(jīng)網(wǎng)絡(luò)。
Connect on LinkedIn
在 LinkedIn上 連接
翻譯自: https://towardsdatascience.com/writing-your-own-functions-40d381bd679
編寫分段函數(shù)子函數(shù)
總結(jié)
以上是生活随笔為你收集整理的编写分段函数子函数_编写自己的函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软即将停售 Win10 产品密钥 /
- 下一篇: 向量 矩阵 张量_张量,矩阵和向量有什么