未登录,请登录后再发表信息
最新评论 (0)
播放视频

14/44 演示:数字

Demo: Numbers | Python for Beginners [14 of 44]

Numbers in Code
《代码中的数字》
So I’ve got Visual Studio open.
好 我已经打开了Visual Studio软件
Let’s go and take a look at some code.
让我们一起来看一些代码
So I’m going to create my variable pi.
首先我要创建一个变量pi
14159
14159
I know of is more digits and that but I’ve gotta stop somewhere.
我知道后面还有更多数字 但到这里就可以了
And I’m storing that value in a variable
我把这个值存储在一个变量中
and then I can just print that value up on the screen
然后将这个值输出到屏幕上
using ctrl S to save here.
用ctrl+s键保存
I love using keyboard shortcuts.
我喜欢用键盘快捷键
And we’re just trying to get that message go away.
接着我们试图将这个提示关掉
There we go.
可以了
And now that I’m here. Run my code and you will see that it displays.
运行代码 现在大家可以看到显示的内容
There’s my number 3.14159 on the screen.
屏幕上显示的正是我输入的数字3.14159
So again, just showing yes of course I can store numbers in variables as well.
刚刚只是再演示一遍用变量来储存数字是没问题的
And of course, once I’m storing numbers in variables,
当然 一旦我们用变量储存数字了
let me just comment out this code.
先注释掉这段代码
Ctrl KC, by the way,
顺便说一下 Ctrl+K+C
the keyboard shortcut for commenting out code.
是用来注释代码的键盘快捷键
And now I can store a couple of numbers.
现在我可以存一些数字进去了
First number equals five.
第一个数字等于5
Second number equals six.
第二个数字等于6
And then I can add those numbers together,
接下来我就可以把这些数字加在一起
so I can do a print statement of first number plus second number.
即用print语句 第一个数字加上第二个数字
Because of course the main reason we want to store numbers
当然 我们想储存数字的主要原因
is because we’re going to need to do math with them.
是我们接下来要对它们进行数学运算
So now I go ahead and I run this code using the up arrow.
我们继续 我用方向上键 运行这段代码
Recall my last command and it comes back and returns 11.
调用上一条语句 然后就会返回11
You can change that to a multiply
你可以将加号换成乘号
and then you see 30.
然后得到30
I can change that to a double exponent which means to the power of.
我还可以将乘号换成两个指数 也就是幂
Save that code.
保存代码
Rerun.
重新运行
And apparently five to the power of six is 15,625.
显然 5的6次方是15625
I admit that’s beyond the capabilities of what I can do in my head.
不得不承认这已经比我大脑的运算能力要强了
But that’s why I like computers, they’ll figure that stuff out for me.
但这是正是我喜欢计算机的原因 它们会替我执行这些计算任务
Computers are good at math.
计算机很擅长运算
So this is a fairly common situation.
当然 这是一个相当简单的情况
But as I mentioned, one of the problems you will run into is
但是正如我提到的 你可能会遇到的问题之一是
let’s say I start asking the user to enter the values for the first and second number.
比如说 我要求用户输入第一和第二个数的值
So I say, “Hey, emm…” Do an input statement and ask for user, “please enter a number.”
那么我会说:“ 嘿 唔…” 调用input语句 要求用户 “请输入一个数字”
And I’m going to take the number they entered and store it in that variable first_num.
然后将他们输入的数字存入变量first_num
And then, I say, “please enter another number.”
然后我会说:“请输入另一个数字”
And I decide to store that in the variable second_num.
然后将它存入变量second_num
So all I’ve done is taking the value for first number and second number and taking it from the input statement.
总之我做的就是获取第一个和第二个数字 再通过input语句对变量赋值
The rest of my code is the same.
剩下的代码和之前一样
And yet now when I run,
但当我运行代码时
I’m gonna change this to a actuals probably give me an error.
我故意将其改为一个可能会导致错误的代码
We’ll try this
我们试一下
and it goes enter number.
然后就到了输入数字这一步
I enter five and number six and it goes on.
我输入数字5和数字6 然后继续运行
Unsupported operand type for the asterisk outer skirt score the power command.
不支持的操作符类型 星号代表的幂指令
It’s like it’s got a string and a string.
似乎它们被处理成两个字符串了
Well, what’s susan to the power of i back.
好了 现在把幂符号加在i上
So the problem is it has two strings.
所以问题是这里是两个字符
I can demonstrate this by changing to the power of symbols to a plus sign.
我可以把指数符号改成加号证明这一点
Because now you’ll see the two strings concatenated together.
因为现在你能看到两个字符串连在一起了
So now if I run and I’ll just move this to top the screen.
如果我现在运行它 我先把这个挪到屏幕上方
So you can see it better
这样你就可以看得更清楚
and I enter a number five and the number six
然后我输入数字5和6
and you see it come back with 56.
然后你可以看到返回值是56
So this is the problem I was talking about where the numbers are being treated as strings,
我在说的问题就是 这里数字是被当成字符串对待
because the input function always returns a string,
因为输入的程序总是返回一串字符串
even if that string contains a number.
即使这个字符串由数字构成
A little confusing I know.
我知道这有点容易混淆
So let’s go in and fix that.
所以让我们一起深入探索它
So what we have to do is we have to go in here and say treat this number as an integer.
所以我们需要做的是 我们从这步开始 然后把这个数字声明为整数
and treat this number as an integer
同时也按整数处理
or take this variable and convert it to an integer.
或者把这个变量转换成一个整数型变量
Now, when we enter five and six,
现在 当我们输入数字5和数字6后
you can see it actually correctly does a math and returns 11.
你可以看到它做了正确的数学运算 得到11
If you and we change that to a float rather than an integer,
如果我们把这个换成浮点数 而不是整数
the only difference is an integer is for whole numbers,
唯一的区别是整型数字适用于整数
a float can contain decimal numbers.
而浮点数可以包含小数
So the only difference is going to be that when I entered the numbers, it shows 11.0.
所以这里的唯一区别就是 当我输入数字后 它会显示11.0
Just a way of me recognizing.
这是我的判别方法
It’s a number of it can contain decimals a floating point number.
浮点数是包含小数点的
So here I’ve used datatype conversion to take a number of stored in a string to treat it as a number.
这里我用了数据类型转换 将存储在字符串中的数据视为数字
But there was one other scenario where we had to do datatype conversion as well.
但是也还有其他情况 让我们也不得不进行数据类型转换
So let’s take a look at that one.
所以现在我们看看这个例子
So I’m going to comment out this code,
那么现在我要把这段代码注释掉
ctrl KC to do that.
按ctrl+KC键完成这一步
And what I’m going to do is I’m going to say days in February.
我要做的是 我将会问二月份的天数
Help if I could type, February.
像我这样打出 二月
If I could spell it like it spell February as well
就像我这样拼写二月
apparently equals 28 and then I say print days in February,
显然等于28 然后把二月份中的天数打出来
there we go, plus
好了 相加
and I want to concatenate that total days in February.
我想把二月份的总天数的数据连接起来
So sometimes we want to display a number inside a string on the screen.
有时我们想在屏幕上显示一个字符串中的数字
So now when I go and I run this,
那么现在我开始运行
go down here just clear the screen again with the CLS,
一直到这里 用CLS命令清空屏幕
and we see it again it blows up.
然后我们看到它再一次崩溃了
Take a look at that error message says unsupported operand type.
看一下这个错误提醒 说不支持的操作数类型
You have a plus sign is the operand
操作是加号
and you’re trying to give it an integer and a string.
然后你要试着给它赋给一个整数和一个字符串
So it’s confused Python doesn’t know is this two numbers I should add up with math
所以这个行为有些迷惑 Python不知道这个两个数字应该用运算符加起来
or two strings I should concatenate together,
还是作为两个字符串链接起来
because it has one number and one string.
因为这里有一个数字和一个字符串
So we have to use the string function
所以我们必须用字符串功能
to convert days in February into a string datatype.
把二月份天数的数据类型都转换成字符串型
So now, when I go and run it,
所以现在 我来运行它
it comes back perfectly happily and comes back and says 28 total days in February.
它的结果很完美 返回值为二月份里总共有28天
So now you have you build you use numbers in your code,
现在你已经了解了在代码中使用数字的方法
but do be prepared to start doing battle with some data type conversions.
但一定要做好准备去处理数据的类型转换
String, int and float could become your new best friends.
字符串 整型数和浮点型数将成为你新的好伙伴

发表评论

译制信息
视频概述

Visual Studio中数据类型简介以及一些实际使用案例

听录译者

收集自网络

翻译译者

木槿

审核员
视频来源

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

相关推荐