python数值类型_Python数值类型
python數(shù)值類(lèi)型
In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform.
在編程中,數(shù)據(jù)類(lèi)型是必不可少的概念。 根據(jù)我們希望變量執(zhí)行的任務(wù),各種類(lèi)型的數(shù)據(jù)可以存儲(chǔ)在變量中。
The built-in data types in Python are
Python中的內(nèi)置數(shù)據(jù)類(lèi)型是
Text Type
文字類(lèi)型
Numeric Type
數(shù)值類(lèi)型
Mapping Type
映射類(lèi)型
Sequence Type
序列類(lèi)型
Set Type
設(shè)定類(lèi)型
Boolean Type
布爾型
Binary Type
二進(jìn)制類(lèi)型
In this tutorial, we are going to learn about the various numeric types in Python with examples.
在本教程中,我們將通過(guò)示例學(xué)習(xí)Python中的各種數(shù)字類(lèi)型 。
To store numeric values, we need specific numeric data types. Python has some of the data types to define numeric values – these numeric data types can be used to store various numeric values.
要存儲(chǔ)數(shù)值,我們需要特定的數(shù)值數(shù)據(jù)類(lèi)型。 Python具有一些用于定義數(shù)值的數(shù)據(jù)類(lèi)型-這些數(shù)值數(shù)據(jù)類(lèi)型可用于存儲(chǔ)各種數(shù)值。
There are 3 numeric data types in Python:
Python中有3種數(shù)字?jǐn)?shù)據(jù)類(lèi)型:
int
整型
float
浮動(dòng)
complex
復(fù)雜
1)int (1) int)
Integer numeric type is used to store signed integers with no decimal points, like -5, 2, 78, etc.
整數(shù)數(shù)字類(lèi)型用于存儲(chǔ)無(wú)小數(shù)點(diǎn)的帶符號(hào)整數(shù),例如--5、2、78等。
Example:
例:
# Assigning integer values to Variables x = 8 y = -9# Manipulating the value of x x = x + y# Prints the updated value of x print("x= ", x)# Prints the Data Type of x print("Data type of x: ", type(x))Output
輸出量
x= -1 Data type of x: <class 'int'>2)浮動(dòng) (2) float)
Float numeric type is used to store floating-point values like 6.66, 58.9, 3.14, etc. Floats can also be in scientific notation, with E or e indicating the power of 10 (3.6e3 = 3.6x 103?= 3600).
浮點(diǎn)數(shù)字類(lèi)型用于存儲(chǔ)浮點(diǎn)值,例如6.66、58.9、3.14等。浮點(diǎn)數(shù)也可以用科學(xué)計(jì)數(shù)法表示,E或e表示10的冪(3.6e3 = 3.6x 10 3 = 3600)。
Example:
例:
# Assigning float values to Variables x = 8.9 y = -9.1# Manipulating the value of x x = x - y# Prints the updated value of x print("x= ", x)# Prints the Data Type of x print("Data type of x: ", type(x))Output
輸出量
x= 18.0 Data type of x: <class 'float'>3)復(fù)雜 (3) complex)
Complex numeric type is used to store complex numbers like 3.14j, 8.0 + 5.3j, 2+6j etc.
復(fù)數(shù)類(lèi)型用于存儲(chǔ)復(fù)數(shù),例如3.14j,8.0 + 5.3j,2 + 6j等。
Format: Real + Imaginary component j
格式:實(shí)數(shù)+虛數(shù)j
Note: The imaginary component of a complex number must be denoted by j. If we denote it using i it is considered as invalid syntax in Python.
注意:復(fù)數(shù)的虛部必須由j表示。 如果使用i表示它,則在Python中被視為無(wú)效語(yǔ)法。
Example:
例:
# Assigning complex values to Variables x = 1+8.5j y = 4+9j# Manipulating the value of x x = x + y# Prints the updated value of x print("x= ", x)# Prints the Data Type of x print("Data type of x: ", type(x))Output
輸出量
x= (5+17.5j) Data type of x: <class 'complex'>演示所有數(shù)值數(shù)據(jù)類(lèi)型的示例 (Examples demonstrating all numeric data types)
Let's look at some simple Python programs to demonstrate the numeric data types:
讓我們看一些簡(jiǎn)單的Python程序來(lái)演示數(shù)字?jǐn)?shù)據(jù)類(lèi)型:
Note: type() is a function used to determine the type of a variable
注意: type()是用于確定變量類(lèi)型的函數(shù)
Example 1: Program to print the data types of variables
示例1:打印變量數(shù)據(jù)類(lèi)型的程序
# Assigning Values to Variables a = 10 b = -1 c = 15.9 d = 6 + 8j# Printing data type of the Variables print("Data Type of a: ", type(a)) print("Data Type of b: ", type(b)) print("Data Type of c: ", type(c)) print("Data Type of d: ", type(d))Output
輸出量
Data Type of a: <class 'int'> Data Type of b: <class 'int'> Data Type of c: <class 'float'> Data Type of d: <class 'complex'>Example 2: Program to add complex numbers to integer and float type numbers
示例2:將復(fù)數(shù)添加到整數(shù)和浮點(diǎn)型數(shù)字的程序
a = 2 + 9j # complex number b = 2.8 # floating point number c = 9 # integer# addition of complex and integer comp_plus_int = a + c # addition of complex and float comp_plus_float = a + b# Printing result of addition and # datatype of the result print("Sum of complex and integer values= ",comp_plus_int) print("Data Type After addition: ", type(comp_plus_int)) print()print("Sum of complex and float values= ", comp_plus_float) print("Data Type After addition: ", type(comp_plus_float))Output
輸出量
Sum of complex and integer values= (11+9j) Data Type After addition: <class 'complex'>Sum of complex and float values= (4.8+9j) Data Type After addition: <class 'complex'>翻譯自: https://www.includehelp.com/python/numeric-types.aspx
python數(shù)值類(lèi)型
總結(jié)
以上是生活随笔為你收集整理的python数值类型_Python数值类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c# bool?和bool_C#中的bo
- 下一篇: 离散点自动生成等高线_有限自动机| 离散