Numeric Data Types | Python for Beginners [13 of 44]

Microsoft Developer
17 Sept 201905:51

Summary

TLDR本视频脚本介绍了如何在Python中处理数字。数字可以像字符串一样存储在变量中,并可以传递给函数如print进行输出。Python中的数学运算符与学校中学习的相似,包括加号、减号、斜杠和星号。脚本还解释了数据类型混合时可能遇到的问题,例如字符串和数字的加法会导致错误。为了解决这个问题,可以使用str函数进行类型转换。此外,脚本还讨论了int和float函数,它们可以将字符串转换为整数或浮点数,这对于执行数学运算至关重要。

Takeaways

  • 🔢 **变量赋值**:在Python中,数字和字符串一样可以存储在变量中,并且应该为变量赋予有意义的名称。
  • 📝 **数据类型**:`print` 函数可以打印字符串或数字类型的变量,但它不区分数据类型。
  • 🧮 **数学运算**:Python中进行数学运算的符号与学校学习的数学符号类似,例如加号(+)、减号(-)、除号(/)、乘号(*)和幂运算(**)。
  • 🚫 **数据类型冲突**:尝试将字符串和数字相加时,Python会报错,因为不清楚是要做数学加法还是字符串连接。
  • 🔄 **类型转换**:使用`str()`函数可以将数字转换为字符串,以便进行字符串连接。
  • 📈 **字符串连接**:将数字转换为字符串后,可以使用`+`号进行字符串连接。
  • 🔄 **逆向转换**:使用`int()`或`float()`函数可以将字符串转换回整数或浮点数。
  • ⚠️ **输入函数**:`input()`函数总是返回字符串,即使用户输入的是数字。
  • 💡 **数据类型的重要性**:在进行数学运算之前,确保数据类型正确,否则可能会导致错误的结果。
  • 📚 **数字的用途**:数字在Python中不仅用于数学计算,还用于索引数组和列表。

Q & A

  • 如何在Python中将数字存储在变量中?

    -就像字符串一样,数字也可以存储在变量中。应该立即养成给这些变量赋予有意义的名字的习惯。

  • print语句可以打印哪些类型的变量?

    -print语句可以打印包含字符串的变量和包含数字的变量,它会将接收到的内容输出到屏幕上。

  • 在Python中进行数学计算时,运算符有哪些?

    -在Python中,加法使用加号(+),减法使用负号(-),除法使用斜杠(/),乘法使用星号(*),而幂运算使用双星号(**)。

  • 为什么在Python中混合使用字符串和数字类型时会遇到问题?

    -当混合使用字符串和数字类型时,Python会混淆,因为它不确定应该执行加法运算还是字符串连接。

  • 如何将数字转换为字符串?

    -在Python中,可以使用str函数将数字转换为字符串,这样就可以进行字符串连接操作。

  • 将数字存储为字符串后,进行加法运算会发生什么?

    -将数字存储为字符串后,使用加号(+)进行运算会将两个字符串连接在一起,而不是进行数学加法。

  • input函数返回的数据类型是什么?

    -input函数总是返回字符串类型,即使用户输入的是数字。

  • 如何将字符串转换为数字进行数学运算?

    -可以使用int函数将字符串转换为整数,或者使用float函数将字符串转换为浮点数,然后进行数学运算。

  • 在Python中,数字还有哪些用途?

    -除了进行数学运算外,数字在Python中还用于索引数组和列表,以及指向特定的位置或行。

  • 为什么在处理数字和字符串混合时需要小心?

    -在处理数字和字符串混合时需要小心,因为如果数据类型使用不当,可能会导致错误的运算结果,比如将数字作为字符串连接而不是进行数学加法。

Outlines

00:00

💻 字符串与数字在Python中的处理

本段落介绍了如何在Python中处理数字和字符串。首先,数字和字符串一样可以存储在变量中,并且推荐给变量赋予有意义的名称。数字可以像字符串一样被传递给函数,如print函数,它能够打印出变量的内容。接着,讲解了如何在Python中进行数学运算,包括加法、减法、乘法和指数运算,这些运算符与学校中学习的相似。特别指出了当混合使用不同类型的数据时,如字符串和数字,Python可能会混淆,因此需要进行类型转换。例如,将数字转换为字符串可以使用str函数,这样就能正确地将数字与字符串拼接。此外,还提到了当使用input函数获取用户输入时,输入的内容总是字符串类型,因此需要使用int或float函数将字符串转换为数字以进行数学运算。

05:02

🔢 数字的正确使用及其在数组和列表中的应用

此段落强调了在使用数字时确保其数据类型正确的重要性。通过使用int函数将字符串转换为整数,或者使用float函数转换为浮点数,可以正确执行数学运算。此外,提到了数字在编程中的两种常见用途:执行数学运算(如计算工资、税收等)和在处理数组、列表时用于索引定位。最后,提醒读者注意数字的正确使用,尤其是在数组和列表中索引值的使用。

Mindmap

Keywords

💡变量

变量在编程中用于存储数据值,可以是字符串、数字等。在视频中,变量被用来存储数字,以便进行数学计算或传递给函数,如print函数。变量的命名应该有意义,以便于理解代码的意图。例如,视频中提到'我们总是想要给那些变量起一个有意义的名字',强调了变量命名的重要性。

💡print语句

print语句在Python中用于输出信息到控制台。它可以输出字符串或数字类型的变量。视频中提到print语句可以接收包含字符串或数字的变量,并将其打印到屏幕上,例如'print语句可以接收一个包含字符串的变量,一个包含数字的变量'。

💡数学计算

数学计算是编程中常见的操作,用于执行加、减、乘、除等运算。视频中通过加号(+)、减号(-)、斜杠(/)、星号(*)和双星号(**)来演示了如何在Python中进行基本的数学运算,如'6加2打印出8,6的2次方,6平方,会给我返回36'。

💡数据类型

数据类型指明了变量存储的数据的种类,如整数、浮点数、字符串等。视频中提到了字符串和数字两种数据类型,并指出当它们混合使用时可能会引起混淆,如'当你开始组合不同的数据类型'。

💡类型转换

类型转换是指将一种数据类型的值转换为另一种数据类型的操作。在视频中,当需要将数字和字符串混合使用时,需要进行类型转换,如使用str函数将数字转换为字符串,以便能够进行字符串的连接操作,例如'我们只需要调用str函数,我们传递给它的是二月的天数,它目前是一个数字'。

💡字符串

字符串是由字符组成的数据序列,通常用于存储文本信息。在视频中,字符串用于存储文本,并可以与数字一起使用进行连接操作,如'如果我说print语句,让我们在屏幕上显示28天在二月'。

💡int函数

int函数用于将其他数据类型转换为整数。在视频中,int函数被用来将字符串类型的数字转换为整数,以便进行数学计算,如'使用int函数,如果我说打印第一个数字的整数版本加第二个数字的整数版本'。

💡float函数

float函数用于将其他数据类型转换为浮点数。在视频中,float函数被用来处理可能包含小数点的数字,如'Float会将其转换为浮点数'。

💡索引

索引用于访问序列(如列表或数组)中特定位置的元素。在视频中,索引被提及作为数字的另一个用途,用于在数组和列表中定位元素,如'你可能需要指向一个索引值'。

💡input函数

input函数用于从用户那里接收输入。在视频中,input函数被用来获取用户的输入,但需要注意的是,input函数总是返回字符串类型的数据,如'如果你使用那个聪明的input函数,问用户输入一个值'。

Highlights

Python中数字可以像字符串一样存储在变量中。

变量应有有意义的名称。

print语句可以打印字符串或数字变量。

在代码中进行数学计算很常见,如计算工资或税。

Python中的数学运算符与学校学习的相似,加号表示加法,减号表示减法。

Python中乘法使用星号,指数使用双星号。

数字可以进行多种数学运算。

当字符串和数字混合使用时,Python可能会混淆。

错误信息会提示数据类型不匹配。

需要进行类型转换,使用str函数将数字转换为字符串。

存储数字为字符串时,会失去数值运算的能力。

input函数返回的总是字符串类型。

使用int或float函数可以将字符串转换为数字。

确保在正确的时间使用正确的数据类型很重要。

数字在数组和列表中用于索引。

数字编码是编程中的一个重要概念。

Transcripts

play00:04

>> So now you've learned how to work with strings,

play00:07

let's take a look at how we work with

play00:09

numbers inside our Python code as well.

play00:11

Just like strings numbers can be stored inside variables.

play00:15

We always want to give those variables

play00:16

nice meaningful names get in the habit of that right away,

play00:19

and we can pass those variables

play00:21

into functions like the print statement.

play00:23

So the print statement can take a variable that contains a string,

play00:26

a variable that contains a number.

play00:27

It really doesn't matter either way,

play00:28

it'll just print what it's received out onto the screen.

play00:32

Of course when you're working with

play00:34

numbers in your code at some point you're probably

play00:36

going to have to do math calculations to figure out

play00:39

people's salaries or taxes and

play00:41

things are pretty common inside our code.

play00:44

So we could do math of the numbers and the good news is,

play00:47

it's going to look a lot like it did when you were in school.

play00:49

You've got the plus sign for addition,

play00:51

the negative sign for subtraction,

play00:53

you've got the slash for division,

play00:55

and then the only thing that's a little bit

play00:57

different is it's an asterisk symbol for

play00:59

multiplication and exponent or to

play01:01

the power of is a double asterisk.

play01:04

So if I have two numbers here, six and two,

play01:07

6 plus 2 prints out an 8,

play01:09

6 to the power of 2,

play01:11

6 squared, will give me back 36.

play01:14

So we can do lots of

play01:15

different math operations inside

play01:17

once we start playing with numbers.

play01:19

One thing you will run into

play01:21

very quickly once you start working with numbers,

play01:24

is that when you start combining different data types.

play01:27

So a string data type in a number datatype,

play01:30

Python will get confused.

play01:32

So in this case, I have,

play01:34

hey, how many days in February?

play01:35

28, and I just say, hey, print statement.

play01:38

Let's display on the screen,

play01:40

28 days in February.

play01:41

We've already printed numbers,

play01:43

we've already printed strings yet when

play01:45

we run this code it blows up.

play01:47

But why? Well, if you

play01:50

actually look at the error message it'll give you a hint.

play01:52

Unsupported operand type for plus int and string.

play01:56

What it's telling you is it's actually

play01:58

the plus symbol that's confused.

play02:00

Five plus six, it knows

play02:02

a number plus a number I should be doing addition.

play02:05

Susan plus I back to

play02:07

strings it knows to concatenate the strings together.

play02:09

But when you give me one number,

play02:11

one string, I'm supposed to add them together like numbers?

play02:14

Or am I supposed to concatenate them like strings?

play02:16

I'm confused.

play02:18

So when you're working with Python,

play02:20

sometimes you have to tell it, hey,

play02:22

I know that the days in February 28 in this case is a number,

play02:25

but I want you to treat it like a string here.

play02:28

I want you to concatenate

play02:29

that number to the phrase days in February,

play02:31

so I see 28 days in February on the screen.

play02:34

So we have to do what's called type conversion.

play02:38

So get used to this,

play02:39

you're going to see it a lot in your code,

play02:41

it's going to come up all over the place.

play02:44

So all we have to do for Python

play02:47

is to convert any data type to a string,

play02:50

you just call these string function.

play02:51

So we call the str function,

play02:53

we pass it days in February which is currently a number,

play02:56

and now it's going to treat that as a string.

play02:59

Now we say, you want to concatenate

play03:00

string plus another string, I know what to do,

play03:03

put those together and

play03:04

displays our message on the screen correctly.

play03:07

So string function is going to save you a lot when you

play03:10

start working with numbers and strings mixing the two up.

play03:14

Now, when I do this I'm actually storing a number as a string,

play03:19

and as soon as you store a number as a string,

play03:22

that also creates mentioning effects.

play03:24

So if I take a first number and I stored the number 5 in it,

play03:27

if I put quotes around that number five it now

play03:30

treats it as a string that contains the number 5,

play03:33

and a second number equal six,

play03:35

a string that contains number 6.

play03:38

Well, what happens when you concatenate

play03:40

two strings together with a plus sign?

play03:42

It puts the two strings together, so I get 56.

play03:46

Well, here you can see what's going on,

play03:49

but sometime this is happening

play03:51

back behind the scenes and it'll catch you.

play03:53

Let's say you're using that clever input function

play03:56

to ask the user to enter a value.

play03:58

Turns out the input function will always return a string.

play04:03

So if you ask a user to enter a number and they enter five,

play04:06

and then you enter a second number and you enter six,

play04:09

and then you say let's print out the result

play04:10

of first number plus second number,

play04:12

it's going to print out 56.

play04:15

So just like earlier I needed a way of saying,

play04:18

hey, I know you think it's

play04:19

number but I want you treat it like a string.

play04:21

Sometimes you have a number stored in

play04:23

a string and you need to treat it like it's a number,

play04:26

I want to do math with it.

play04:28

So there is a different data type conversion,

play04:31

datatype conversion changing from one data type to another.

play04:35

So last time I was using string to convert from number to string,

play04:38

now I need to use something to convert from string to number.

play04:41

I have two choices of functions to work with here.

play04:44

The int function will convert it into an integer,

play04:47

a whole number ;1, 2, 3, 4.

play04:51

Float will convert it into a number of

play04:54

floating number or something that might have decimal places;

play04:56

1.0, 1.5, 2.6 or anything like that would be a float.

play05:02

So by using the int function and if I say print integer version

play05:06

of first number of plus integer version

play05:08

second number or float plus float,

play05:10

you can see it actually does the math

play05:12

correctly and adds 5 plus 6 together and gives me 11.

play05:16

So be very careful when you're working with numbers to

play05:20

make sure it's using it as

play05:21

the correct data type at the right time.

play05:23

If you're wondering what other situations

play05:25

you might be using numbers in, well,

play05:26

there's basically two situations; one,

play05:29

you're doing math operations to calculate salaries,

play05:31

calculate tax, and so on,

play05:33

and the other place you're going to run into

play05:35

numbers is when we start getting into things

play05:37

like arrays and lists and

play05:38

you might need to point to an index value.

play05:40

Say start at this position,

play05:41

go to this position or go to this row here,

play05:44

you'll find numbers useful for that as well.

play05:45

So let's step back from this and go take a look at numbers encode.

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Python编程数据类型字符串数字变量函数数学运算类型转换代码错误编程入门
Benötigen Sie eine Zusammenfassung auf Englisch?