分享興趣,傳播快樂,增長見聞,留下美好!
親愛的您,這裡是LearningYard學苑。
今天小編為大家帶來今日分享(三十八),歡迎您的通路!
Share interests, spread happiness, increase knowledge,
and leave a good legacy!
Dear you, this is The LearningYard Academy.
Today Xiaobian brings you today's sharing (thirty eight), welcome your visit!
基本資料類型(上)
一、數字類型
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.
今天的分享就到這裡了。
如果您對今天的文章有什麼獨特的想法,
歡迎評論留言,讓我們相約明天,
祝您今天過得開心快樂!
That's it for today's sharing.
If you have any unique ideas for today's article, please leave a comment, let us meet tomorrow,
I wish you a happy day!
參考資料:《python程式設計》
翻譯:谷歌翻譯
本文由learningyard新學苑原創,部分文字圖檔來源于他處,如有侵權,請聯系删除