ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

Python – 函数基础 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

Python - 函数基础

Python - Function Basics

大家好 欢迎来到本期Python教程
Hello and welcome to another basic Python tutorial.
这次我们讲解函数及其使用
This one we’re gonna talking about functions and their use.
定义函数是python中不可或缺的一部分
Defining functions is pretty much integral to Python.
因为它是一种面向对象的编程语言
As it’s an object-oriented programming language.
但同时它也是一种脚本语言
However it is also a scripting language and you don’t
你不必定义各种函数 只需要一行行的编写代码即可
have to have various functions and stuff. You could just code line by line.
但是使用函数可以节省许多时间
But if you use functions that can save a lot of time.
并且使代码更灵活 所以我们接下来就学习函数
and make your code a lot more dynamics. So with that let’s go ahead and get into it.
函数定义很简单 只需使用”def”
A function is quite simple you just use def.
这就是说你要定义一个函数 函数名可以随便起
And that says you’re about to define a function. Now you name your function whatever
一种好的变量及函数命名方式是驼峰拼写法
proper way to name variables and functions is with camel casing.
所以我们就命名为“learnFunctions”
So we’re going to say learnFunctions.
在函数定义的最后
And then the parentheses is required
必须要有圆括号 括号里面
at the end of a definition of a function. And within the parentheses you set any
可以放任意参数 但你也可以不放任何参数
unique parameters that you want. But you don’t have to set any parameters.
最后以冒号结束
You finish it off with a colon.
接下来就是写该函数要做什么了
And you’re ready to type whatever you want your function to do.
这个函数我们就写“print”
So for this we’ll just say print
“as simple as it gets”
‘as simple as it gets’
我们可以在里面写多行语句
And then just to show you you do like multiple lines.
“it can be called and used as a sort of short hand”
‘it can be called and used as a sort of short hand’
保存并运行它
So save that. We’ll run it.
显然没有任何输出 因为我们没有调用它
And then obviously since we haven’t…all it does.
函数不一定会运行 因为它只在这被定义了
A function doesn’t necessarily run just because it’s there.
函数只有被调用时才会得到预期结果
You’ve defined it you’ve said what you want to do when that function is called.
所以现在什么也没发生
So nothing that happens yet.
但当你调用这个函数时
But when you call that function.
就会出现这个 显然你不想每次调用函数时都要输入函数名
It then does this. Now obviously you don’t want to have to type out every time you want to call a function.
你可以在代码中这样做
So what you’ll do in your code
把“learnFunctions”写在这
is you’ll have learnFunctions like that.
然后运行 显然它正确执行了
Then you’ll run it and sure enough it plays it.
实际上大多数人会
And then actually what most people are going to have
把它放在main函数里面
fully is it’s going to be encased in the main
就像这样 “learnFunction”会被放在main函数中
like this. These learnFunctions will be in the main loop and then you’ll have a
一会儿这还会有一个“main”函数 可能稍后我会演示
a…Well name equals main. Down here. But I’ll show you guys that maybe later.
这不是必须的
It’s not really necessary. Anyways.
回到函数 这是最基本的函数
So back to functions. So that’s the most basic function.
关于函数 你还可以这样做
Now there are other things that you can do
使它们更灵活一些
with functions to make them a little bit more dynamic.
一种方法是指定一些参数
One of those is so you can specify a sort of
你也可以把它们理解为变量
like…It’s a parameter but you can kind of think of it like a variable in there.
假设你加入了“name”参数
So what happens is…Let’s say you have name and then
你就可以写一些与该变量有关的代码
what you can do is start to kind of code around that variable.
比如说你想输出“hello”
So let’s say you want it to say ‘Hello’
然后把“name”写在这
And then whatever the name is that you type into there.
这样做 然后运行
So you do that. I will run it. And
接下来你要传一个名字给这个参数
Oops. Oh and then so now what you do is you type whatever the name is
因为它需要一个参数
into the parameter since it requires one parameter.
比如说是“viewers”
So we’ll say ‘viewers’.
它就会输出“Hello viewers”
So then it spits out ‘Hello viewers’. OK.
现在只有一个参数
So then…So that’s one now. You can actually have
实际上你可以添加任意数量的参数
as many parameters as you want in here.
你可以添加“digit1” “digit2”
So kind of another idea is let’s say digit1, digit2
然后你想要输出
and then what you want it to do is print
“math addition:”
‘math addition:’
你要输出digit1加上digit2
And let’s say you want to print out digit1 plus digit2
保存 运行
Save that. Run it.
看这儿
Come up here.
我们就让3加上5
learnFunctions on…Let’s just do three and five.
“math addition” 3加5得8 完全正确
math addition: 3 and 5 equals 8. Sure enough it does.
你也可以计算乘法
You can also do multiplication.
前面不用改 只需要改这里就可以了
So you could say…We’ll just leave that there and then obviously you can do this basically.
你还可以这样改 “math multiplication of:”
And the other thing you can do is…Let’s say ‘math multiplication of:’
然后是 digit1乘以digit2
And then you say ‘digit1’ times ‘digit2’. Something like that.
运行 哪错了? 这里需要逗号
Okay. Run it. What do we do wrong? Need that comma.
运行 现在它计算的就是3乘以5 “multiplication of 3 x 5”
Run it. And now it’s obviously gonna do 3 times 5. Multiplication of 3 times 5
结果为15 很简单
equals 15. Pretty simple stuff.
你还可以使它更灵活一点
The other thing you can do to make it a little bit more dynamic
就像…
is something like…
比如说你这样定义函数
Let’s say so you define your function like that.
然后在这定义一个“main”函数
And then what we’ll do is define a main function here.
输入“num1=”
And we’ll say num1 equals
把输入值转换为整型数
the integer version of input.
input使用户可以输入 输入时就会调用input函数
And what input is is it allows like user input it’s going to call for input.
稍后我会演示 你们可以看到
So I’ll show you guys that in a second or you’ll see it what happens.
“enter first number to multiply:”
‘enter first number to multiply: ‘
不是“multiple” 是“multiply”
multiple…multiply
封闭两个括号 这是一个整型数 然后输入“num2=”
Close that off. Close that off. That’s integer. And then you’ll say num2 equals
复制 粘贴 再输入一次
Just copy and paste. Type that out again.
“enter the second number to multiply:”
‘enter the second number to multiply: ‘
然后调用 learnFunctions函数
And then what we’ll do is we’ll call learnFunctions.
参数为num1和num2
And in here will say num1 and num2.
这就很灵活了 运行 调用main函数
So now it’s very dynamic. We’ll run this. We’ll call the main loop.
拉到这 调用main函数
Scroll up here. We’ll call the main loop.
输入乘法第一个数 我们输入8
enter the first number to multiply. Let’s say we want to do eight.
输入第二个数是5
And the second number to multiply let’s do five.
8与5的乘积是40
And the multiplication of 8 times 5 equals 40.
可以看到 我们写了一个非常灵活的函数
So now as you can see we’ve kind of made this pretty dynamic function.
你可以在函数里写任何东西
You can do this with all kinds of stuff and make…it’s just
如果你想要编码灵活些 函数一定是必须的
absolutely necessary to use it if you’re going to be coding anything even sort of dynamic.
希望你们喜欢 能学到一些东西 下次见
So let’s hope you guys enjoyed. Hoping maybe learn something and until next time.

发表评论

译制信息
视频概述

本节介绍了函数的定义及使用

听录译者

[B]刀子

翻译译者

[B]hugue

审核员

审核团O

视频来源

https://www.youtube.com/watch?v=bK5RJUpGWAc

相关推荐