Numeric Data Types | Python for Beginners [13 of 44]
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
💻 字符串与数字在Python中的处理
本段落介绍了如何在Python中处理数字和字符串。首先,数字和字符串一样可以存储在变量中,并且推荐给变量赋予有意义的名称。数字可以像字符串一样被传递给函数,如print函数,它能够打印出变量的内容。接着,讲解了如何在Python中进行数学运算,包括加法、减法、乘法和指数运算,这些运算符与学校中学习的相似。特别指出了当混合使用不同类型的数据时,如字符串和数字,Python可能会混淆,因此需要进行类型转换。例如,将数字转换为字符串可以使用str函数,这样就能正确地将数字与字符串拼接。此外,还提到了当使用input函数获取用户输入时,输入的内容总是字符串类型,因此需要使用int或float函数将字符串转换为数字以进行数学运算。
🔢 数字的正确使用及其在数组和列表中的应用
此段落强调了在使用数字时确保其数据类型正确的重要性。通过使用int函数将字符串转换为整数,或者使用float函数转换为浮点数,可以正确执行数学运算。此外,提到了数字在编程中的两种常见用途:执行数学运算(如计算工资、税收等)和在处理数组、列表时用于索引定位。最后,提醒读者注意数字的正确使用,尤其是在数组和列表中索引值的使用。
Mindmap
Keywords
💡变量
💡print语句
💡数学计算
💡数据类型
💡类型转换
💡字符串
💡int函数
💡float函数
💡索引
💡input函数
Highlights
Python中数字可以像字符串一样存储在变量中。
变量应有有意义的名称。
print语句可以打印字符串或数字变量。
在代码中进行数学计算很常见,如计算工资或税。
Python中的数学运算符与学校学习的相似,加号表示加法,减号表示减法。
Python中乘法使用星号,指数使用双星号。
数字可以进行多种数学运算。
当字符串和数字混合使用时,Python可能会混淆。
错误信息会提示数据类型不匹配。
需要进行类型转换,使用str函数将数字转换为字符串。
存储数字为字符串时,会失去数值运算的能力。
input函数返回的总是字符串类型。
使用int或float函数可以将字符串转换为数字。
确保在正确的时间使用正确的数据类型很重要。
数字在数组和列表中用于索引。
数字编码是编程中的一个重要概念。
Transcripts
>> So now you've learned how to work with strings,
let's take a look at how we work with
numbers inside our Python code as well.
Just like strings numbers can be stored inside variables.
We always want to give those variables
nice meaningful names get in the habit of that right away,
and we can pass those variables
into functions like the print statement.
So the print statement can take a variable that contains a string,
a variable that contains a number.
It really doesn't matter either way,
it'll just print what it's received out onto the screen.
Of course when you're working with
numbers in your code at some point you're probably
going to have to do math calculations to figure out
people's salaries or taxes and
things are pretty common inside our code.
So we could do math of the numbers and the good news is,
it's going to look a lot like it did when you were in school.
You've got the plus sign for addition,
the negative sign for subtraction,
you've got the slash for division,
and then the only thing that's a little bit
different is it's an asterisk symbol for
multiplication and exponent or to
the power of is a double asterisk.
So if I have two numbers here, six and two,
6 plus 2 prints out an 8,
6 to the power of 2,
6 squared, will give me back 36.
So we can do lots of
different math operations inside
once we start playing with numbers.
One thing you will run into
very quickly once you start working with numbers,
is that when you start combining different data types.
So a string data type in a number datatype,
Python will get confused.
So in this case, I have,
hey, how many days in February?
28, and I just say, hey, print statement.
Let's display on the screen,
28 days in February.
We've already printed numbers,
we've already printed strings yet when
we run this code it blows up.
But why? Well, if you
actually look at the error message it'll give you a hint.
Unsupported operand type for plus int and string.
What it's telling you is it's actually
the plus symbol that's confused.
Five plus six, it knows
a number plus a number I should be doing addition.
Susan plus I back to
strings it knows to concatenate the strings together.
But when you give me one number,
one string, I'm supposed to add them together like numbers?
Or am I supposed to concatenate them like strings?
I'm confused.
So when you're working with Python,
sometimes you have to tell it, hey,
I know that the days in February 28 in this case is a number,
but I want you to treat it like a string here.
I want you to concatenate
that number to the phrase days in February,
so I see 28 days in February on the screen.
So we have to do what's called type conversion.
So get used to this,
you're going to see it a lot in your code,
it's going to come up all over the place.
So all we have to do for Python
is to convert any data type to a string,
you just call these string function.
So we call the str function,
we pass it days in February which is currently a number,
and now it's going to treat that as a string.
Now we say, you want to concatenate
string plus another string, I know what to do,
put those together and
displays our message on the screen correctly.
So string function is going to save you a lot when you
start working with numbers and strings mixing the two up.
Now, when I do this I'm actually storing a number as a string,
and as soon as you store a number as a string,
that also creates mentioning effects.
So if I take a first number and I stored the number 5 in it,
if I put quotes around that number five it now
treats it as a string that contains the number 5,
and a second number equal six,
a string that contains number 6.
Well, what happens when you concatenate
two strings together with a plus sign?
It puts the two strings together, so I get 56.
Well, here you can see what's going on,
but sometime this is happening
back behind the scenes and it'll catch you.
Let's say you're using that clever input function
to ask the user to enter a value.
Turns out the input function will always return a string.
So if you ask a user to enter a number and they enter five,
and then you enter a second number and you enter six,
and then you say let's print out the result
of first number plus second number,
it's going to print out 56.
So just like earlier I needed a way of saying,
hey, I know you think it's
number but I want you treat it like a string.
Sometimes you have a number stored in
a string and you need to treat it like it's a number,
I want to do math with it.
So there is a different data type conversion,
datatype conversion changing from one data type to another.
So last time I was using string to convert from number to string,
now I need to use something to convert from string to number.
I have two choices of functions to work with here.
The int function will convert it into an integer,
a whole number ;1, 2, 3, 4.
Float will convert it into a number of
floating number or something that might have decimal places;
1.0, 1.5, 2.6 or anything like that would be a float.
So by using the int function and if I say print integer version
of first number of plus integer version
second number or float plus float,
you can see it actually does the math
correctly and adds 5 plus 6 together and gives me 11.
So be very careful when you're working with numbers to
make sure it's using it as
the correct data type at the right time.
If you're wondering what other situations
you might be using numbers in, well,
there's basically two situations; one,
you're doing math operations to calculate salaries,
calculate tax, and so on,
and the other place you're going to run into
numbers is when we start getting into things
like arrays and lists and
you might need to point to an index value.
Say start at this position,
go to this position or go to this row here,
you'll find numbers useful for that as well.
So let's step back from this and go take a look at numbers encode.
Посмотреть больше похожих видео
[ML2021] Pytorch Tutorial 2
Data Structures: Crash Course Computer Science #14
Stream of Search (SoS): Learning to Search in Language
Lecture 1.1 — Why do we need machine learning — [ Deep Learning | Geoffrey Hinton | UofT ]
Python从入门到精通 第13集 数据类型 整数类型
Save and persist data with UserDefaults | Todo List #4
5.0 / 5 (0 votes)