​​学习知多少(python篇):函数


分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。今天小编为大家带来“学习知多少(python篇):函数”,欢迎您的访问。

Share interests, spread happiness, increase knowledge, and leave a good legacy!

Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know (python):function”, welcome your visit.

函数简介:

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。

Function introduction:

Functions are organized, reusable pieces of code that implement a single, or related function. Functions can improve the modularity of the application and the reuse of code.


定义函数的规则:

Rules for defining functions:

1、函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。

1. The function code block starts with the def keyword, followed by the function identifier name and parentheses ().

2、函数内容以冒号起始,并且缩进。

2. The function content starts with a colon and is indented.

3、函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。

3. The first line of the function statement can optionally use a docstring - used to store the function description.

4、任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。

4. Any incoming parameters and arguments must be placed in the middle of parentheses. Parentheses can be used to define parameters.

5、return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。

5. return [expression] ends the function and optionally returns a value to the caller. Return without an expression is equivalent to returning None.


案例:

Case:


1、已知斐波拉契数列的前两项都是1,我们定义求斐波拉契数列的第n项(n<=50)的函数为fbnq,程序主体如下:

n=int(input("")) print(fbnq(n))请补充完成对fbnq函数的定义。

1. It is known that the first two items of the Fibonacci sequence are 1, and we define the function to find the nth term (n<=50) of the Fibonacci sequence as fbnq, and the program body is as follows:

n=int(input("")) print(fbnq(n)) Please complete the definition of the fbnq function.



2、我们定义求最大公约数的函数为hcf,给出程序主体如下:

num1=int(input("")) num2=int(input("")) print(hcf(num1,num2))请补充完成hcf函数的定义。

2. We define the function to find the greatest common pisor as HCF, and give the program body as follows:

num1=int(input("")) num2=int(input("")) print(hcf(num1,num2)) Please complete the definition of the hcf function.


3、输入两个正整数num1和num2(不超过500),求它们的最小公倍数并输出。

我们定义求最小公倍数的函数为lcm,给出程序主体如下:

num1=int(input("")) num2=int(input("")) print(lcm(num1,num2))请补充完成lcm函数的定义。

3. Input two positive integers num1 and num2 (not more than 500), find their least common multiple and output.

We define the function for finding the least common multiple as lcm, and give the program body as follows:

num1=int(input("")) num2=int(input("")) print(lcm(num1,num2)) Please complete the definition of the lcm function.


4、我们定义求n(n为正整数且n<=20)的阶乘的函数为fact,给出程序主体如下:

n=int(input("")) print(fact(n))请补充完成对fact函数的定义。

4. We define the function of finding the factorial of n (n is a positive integer and n<=20) as fact, and give the program body as follows:

n=int(input("")) print(fact(n)) Please complete the definition of the fact function.



5、冒泡排序是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

已知输入为一个列表,列表中的元素都为整数,我们定义冒泡排序函数为bubbleSort,将列表中的元素按从小到大进行排序后得到一个新的列表并输出,给出程序主体如下:

alist=list(map(int,input().split())) print(bubbleSort(alist))请补充完成对bubbleSort函数的定义。

5. Bubbling sorting is a simple sorting algorithm. It iterates through the series to be sorted, comparing two elements at a time, swapping them out if they are in the wrong order. The work of traversing the series is repeated until there is no more exchange, that is, the series has been sorted. The algorithm gets its name because smaller elements slowly "float" to the top of the sequence through exchange.

Given that the input is a list, the elements in the list are integers, we define the bubbling sort function as bubbleSort, sort the elements in the list from smallest to largest and get a new list and output, giving the program body as follows:

alist=list(map(int,input().split())) print(bubbleSort(alist)) Please complete the definition of the bubbleSort function.






今天的分享就到这里了。
如果您对今天的文章有独特的想法,
欢迎给我们留言,
让我们相约明天。
祝您今天过得开心快乐!

That's all for today's sharing.
If you have a unique idea for today’s article,
please leave us a message,
and let us meet tomorrow.
I wish you a happy day !


本文由learningyard新学苑原创,如有侵权,请联系我们!

翻译来源于谷歌翻译

部分来源于

百度文库

清华大学出版 董付国《Python程序设计基础》



编辑&排版|百味

审核|闫庆红

展开阅读全文

页面更新:2024-05-13

标签:函数   最小公倍数   圆括号   数列   遍历   表达式   主体   元素   定义   程序

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top