• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 商业

BUSINESS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 14/44 演示：数字

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

Numbers in Code
《代码中的数字》
So I’ve got Visual Studio open.

Let’s go and take a look at some code.

So I’m going to create my variable 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.

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.

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,

the keyboard shortcut for commenting out code.

And now I can store a couple of numbers.

First number equals five.

Second number equals six.

And then I can add those numbers together,

so I can do a print statement of first number plus second number.

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.

You can change that to a multiply

and then you see 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.

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.”

And I’m going to take the number they entered and store it in that variable first_num.

And then, I say, “please enter another number.”

And I decide to store that in the variable second_num.

So all I’ve done is taking the value for first number and second number and taking it from the input statement.

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.

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.

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

and you see it come back with 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,

you can see it actually correctly does a math and returns 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.

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.

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,

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,

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

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.

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