今日分享(三十八)——python学习(5)

今日分享(三十八)----python学习(5)

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。

今天小编为大家带来今日分享(三十八),欢迎您的访问!

Share interests, spread happiness, increase knowledge,

and leave a good legacy!

Dear you, this is The LearningYard Academy.

Today Xiaobian brings you todays sharing (thirty eight), welcome your visit!

今日分享(三十八)----python学习(5)
03:29来自LearningYard学苑

基本数据类型(上)

一、数字类型

一、数字类型

1、整数类型

与数学中的整数概念一致,共有4种进制表示:十进制、二进制、八进制、十六进制Consistent with the concept of integers in mathematics, there are 4 base-based representations: decimal, binary, octal, hexadecimal

其中,pow(x,y)是python语言的一个内置函数,用于计算x的y次幂。

Among them, pow(x,y) is a built-in function of the Python language that calculates the power of y of x.

2、浮点数类型

与数学中的实数概念一致,表示带有小数的数值,Python 语言要求所有浮点数必须带有小数部分,小数部分可以是 0,用于区分浮点数和整数类型。Consistent with the concept of real numbers in mathematics, which represent numeric values with decimals, the Python language requires that all floating-point numbers must have a decimal part, which can be 0 to distinguish between floating-point numbers and integer types.浮点数有两种表示方法:十进制表示和科学计数法表示。There are two representations of floating-point numbers: decimal and scientific notation.

3复数类型

18世纪,数学家发明了“虚数单位”,记为j,并规定j=√-1,产生了数学分支。In the 18th century, mathematicians invented the "imaginary unit", denoted as j, and stipulated that j = √-1, giving rise to the branch of mathematics.复数可以看作二元有序实数对(a,b)表示 a+bj,a为实部,用z.real获得;b为虚部,用z.imag获得。Complex numbers can be seen as binary ordered real number pairs (a,b) representing a+bj, a is the real part, obtained with z.real, and b is the imaginary part, obtained with z.imag.复数类型中实数部分和虚数部分的数值都是浮点类型。The numeric values of both the real and imaginary parts of the complex number type are floating-point types.二、数字运算操作符1、内置运算操作符

在python中与其他语言不一致的是在“除”时,整数相除是浮点数类型,因此会有一个“//”表示整数除。

Inconsistent with other languages in Python is that when "divided", integer division is a floating-point type, so there will be a "//" for integer division.

二元操作符对应的增强赋值操作符三种数字类型之间存在一种逐渐扩展的关系:整数--->浮点数--->复数因为整数可以看成是浮点数没有小数的情况,浮点数可以看成是复数虚部为 0的情况。基于上述扩展关系,数字类型之间相互运算所生成的结果是“更宽”的类型,基本规则如下:Because integers can be thought of as floating-point numbers without decimals, floating-point numbers can be seen as cases where complex numbers have imaginary parts of 0. Based on the above extended relationship, the result of the mutual operation between numeric types is a "wider" type, with the following basic rules:

(1)整数之间运算,如果数学意义上的结果是小数,结果是浮点数。

(2)整数之间运算,如果数学意义上的结果是整数,结果是整数。

(3)整数和浮点数混合运算,输出结果是浮点数。

(4)整数或浮点数与复数运算,输出结果是复数。

(1) Operations between integers, if the result in the mathematical sense is a decimal, the result is a floating-point number.(2) Operations between integers, if the result in the mathematical sense is an integer, the result is an integer.(3) Integer and floating-point numbers are mixed, and the output result is a floating-point number.(4) Integer or floating-point numbers and complex number operations, the output result is complex numbers.

2、内置运算函数

数值运算操作符可以隐式地转换输出结果的数字类型,例如,两个整数采用运算符“”的除法将可能输出浮点数结果。

Numeric operation operators can implicitly convert the numeric type of the output result, for example, a division of two integers with the operator "" will likely output a floating-point result.

通过内置的数字类型转换函数可以显式地在数字类型之间进行转换。浮点数类型转换为整数类型时,小数部分会被舍弃(不使用四舍五入),复数不能直接转换为其他数字类型,可以通过.rcal 和.imag 将复数的实部或虚部分别转换。

The built-in numeric type conversion function allows you to explicitly convert between numeric types. When a floating-point number type is converted to an integer type, the fractional part is discarded (no rounding is used), the complex number cannot be converted directly to other number types, and the real or imaginary parts of the complex number can be converted separately through .rcal and .imag.

今天的分享就到这里了。

如果您对今天的文章有什么独特的想法,

欢迎评论留言,让我们相约明天,

祝您今天过得开心快乐!

Thats it for todays sharing.

If you have any unique ideas for todays article, please leave a comment, let us meet tomorrow,

I wish you a happy day!

参考资料:《python程序设计》

翻译:谷歌翻译

本文由learningyard新学苑原创,部分文字图片来源于他处,如有侵权,请联系删除

举报/反馈