专业编程基础技术教程

网站首页 > 基础教程 正文

03 Python数据类型转换(python数据类型转换函数)

ccvgpt 2024-11-15 16:51:47 基础教程 7 ℃

好了欢迎大家回来。

在这个主题中我将解释类型转换分为类型转换和形式形式和隐式。

03 Python数据类型转换(python数据类型转换函数)

·从一种数据类型到另一种数据类型,可以将整数转换为浮点数,将浮点数转换为整数,字符串到整数整数到金条,为什么你可能想使用类型转换?当我们开始接受用户输入的课程时,用户输入始终是一个字符串,用户输入始终是一个字符串,我们想将其转换为整数或浮动。

·另一个例子是,如果有一个浮点数,需要将该数字四舍五入为整数,一种可能的方法是将其转换为整数,我们有两种不同的方法可以做到这一点:显式和隐式。

让我们介绍一些显式类型转换。我有四个变量,我们需要一根绳子,让我们说名字,输入你的名字,年龄那很好,float how about a grade point average GPA for short浮动,简称平均绩点怎么样?my GPA will be a solid um 1.9我的平均成绩是1.9分。

那么我们需要一个布尔值。what about student 学生?are we a student if we're a student that would be true我们是学生吗?如果我们是学生,那是真的吗?

if not that would be false if not that would be false 如果不是,那就是假的。

here's four variables这里有四个变量of the four basic data type we have a string在四种基本数据类型中,我们有一个字符串。

an integer a float and a boolean一个整数一个浮点数和一个布尔值。

if you ever need the data type of a variable如果您需要变量的数据类型,you can use the built in type function您可以使用内置类型函数。

type the word类型(type)followed by a set of parentheses后跟一组括号(第一组括号)。

place a variable within the set of parentheses在括号集中放置一个变量。

however when I run this we can't see anything但是当我运行这个时,我们什么也看不到。so we need to place所以我们需要放置this statement within a print statement打印语句中的此语句。

I'm going to type print add a set of parentheses我要输入print添加一组括号。

we will cut this line of code我们将剪切这行代码,and paste it within the print statement并将其粘贴到print语句中。

so we are printing the type of my variable name所以我们正在打印我的变量名的类型。

and that is STR which means it's a string,let's do that with Age GPA and student让我们用年龄平均成绩和学生来做。type function we have age,我们有年龄,GPA and student,GPA和学生,我们在这里"floPython says the data type of name is a string str,Python说name的数据类型是字符串str,age is an integer,年龄是一个整数,ing表示整数,GPA is a float,平均成绩是浮动的。

然后学生是公牛,意思是金条。let's do some explicit 变差,for practice I need to convert my variable,为了练习,我需要转换我的变量。age into a floating point number,年龄变成浮点数,I could take my variable age,我可以使用equals将其重新赋值。

then I will eplicitly cast age,那我就明确投年龄。into a float by typing float,通过键入浮点数进入浮点数,一组括号,a set of parentheses,add my variable to be,添加我的变量为,typecasted within the set of parentheses,在括号内排版。

then let's print whatever age currently is,那么让我们打印当前的年龄,look at that age is now 21.0,看看那个年龄现在是21.0。you can tell it's a loat because it has a decimal,你可以看出它是一个浮点数,因为它有一个小数。

portion if I were to use that type function again,部分如果我再次使用该类型函数,we can see what the data type of age is now,我们现在可以看到年龄的数据类型是什么,and my variable age is a floating point number,我的可变年龄是一个浮点数。

that's how to cast a value as a float,这就是如何将值转换为浮点数。

you type float at a set of parentheses,您在一组括号中键入浮点数,将您的值或变量放在该组中。那么您可能需要将其重新分配给某些东西。这里我们只是重新分配我们可变的年龄。我们知道如何将值转换为浮点数。让我们试试一个整数,我会把我的平均成绩换算成一个整数,我要考GPA,将续集设置为INT括号集类型。那我就打印我的平均成绩。igpa现在是1,所以整数是整数,我们不能包含二次部分,所以它被截断了。这就是如何将值转换为整数,让我们将值转换为字符串。how about this bullion这条金条怎么样?所以学生,等于将值或变量转换为字符串类型STR。加一组括号,您在该括号集中的值或变量。然后让我们打印我们的变量之后的任何内容,我们重新分配它。所以学生还是说真的,它看起来和以前一样,但现在它被视为一系列字符,而不是布尔值,所以人们还是观察我们的平均成绩为整数,而不是布尔值。只是为了向你证明学生是一根绳子。type function and surround our student variable,when we print it,当我们打印它时,student is now a string,学生现在是一个字符串,so to convert a value or variable into a string,因此,要将值或变量转换为字符串,you can use the built in string function,您可以使用内置的字符串函数,just type str嘣,给你。set a parentheses add your value or variable,设置括号添加您的值或变量,boom there you go。

we can convert a value or variable into a boolean,我们可以将值或变量转换为布尔值,so a boolean can only be true or false,所以布尔值只能是真或假。what if we were to convert a number into a boolean,如果我们要将一个数字转换为布尔值?what would happen 会发生什么?so let's take age,所以让我们把年龄,equals to cast the value or variable into a boolean,等于将值或变量转换为布尔值。

you type bool,set of parentheses add your value or variable,括号集添加您的值或变量。I'll lace edge within the set of parentheses,我将边缘放在括号内。then let's print whatever age is,那么让我们打印任何年龄,do you think the result is going to be true or false,你认为结果是真的还是假的?

it's actually true,这其实是真的。将数字转换为布尔值时,if that number is anything,如果那个号码是什么,but zero it will always be true,但零永远是真的。

假设年龄就像一百万,well it's still gonna be true,这仍然是真的,what about negative 1 million,负面100万。所以所以所以所以所以所以它代表的是年份在前在前,但如果年龄为零那就是假的。

假设我们要求用户输入他们的名字,我需要检查一下他们是否输入了什么东西。让我们把我的名字塑造成金条。这是真的,但如果这是一个空字符串,即使空格注册为真那也是假的。所以空格注册为真那也是假的。

打字检查某人是否打字检查某人是否or not if wecast the name as a boolean and it's true或者不,如果我们将名称转换为布尔值这是真的,这意味着他们输入了一些东西,我们稍后会有更多的练习。所有显示器类型转换都是手动的。

将值或变量转换为differentdata type by using one of these cast 装夹使用这些强制转换关键字之一的不同数据类型,we only covered four只覆盖了四个strings strings floatsand booleans字符串整数浮点数和布尔值。

好了现在我们来复习一下隐式类型划分。隐式类型转换是当一个值或变量被转换成一个值或变量被转换成一个type automatically自动使用不同的数据类型,for example let's say we have 例如,假设我们有x,x等于2,然后我们有y等于2.0怎么样?如果我重新分配X并将x设置为x怎么办?除以y,我们将整数除以浮点数,结果会是什么?is X going to be an integer or a float X是整数还是浮点数?so let'sprint 气管 X is,所以让我们打印X是什么?x现在漂浮着,即使我们最初将其分配为整数,这就是隐式类型转换。

a variable's data type变量的数据类型,can be converted when you perform certain operations on it.当你对它执行某些操作时,比如像这样的算术表达式,alrighteverybody so that is typecasting.这是将值1转换为1的过程。data type to another另一种数据类型,您可以将字符串转换为整数,将整数转换为浮点数,营帐ons to strings.浮标,金条对绳子。any combination of those.这些的任何组合,还有更多的数据类型我们没有涵盖。

but that's the general idea,但总的来说就是这样。so that is typecasting.这就是类型转换。hey if you found this video helpful.嘿,如果你觉得这个视频有帮助,please be sure to smash that like button.请务必打碎那个喜欢的按钮。leave a random 神门 down below.在下面留下随机评论。and subscribe if you'd like to become a fellow bre。如果你想成为兄弟,请订阅。

如果你想成为兄弟,请订阅。

(If you want to be a fellow bro)。

Tags:

最近发表
标签列表