python數值類型
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.
在編程中,數據類型是必不可少的概念。 根據我們希望變量執行的任務,各種類型的數據可以存儲在變量中。
The built-in data types in Python are
Python中的內置數據類型是
Text Type
文字類型
Numeric Type
數值類型
Mapping Type
映射類型
Sequence Type
序列類型
Set Type
設定類型
Boolean Type
布爾型
Binary Type
二進制類型
In this tutorial, we are going to learn about the various numeric types in Python with examples.
在本教程中,我們將通過示例學習Python中的各種數字類型 。
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.
要存儲數值,我們需要特定的數值數據類型。 Python具有一些用于定義數值的數據類型-這些數值數據類型可用于存儲各種數值。
There are 3 numeric data types in Python:
Python中有3種數字數據類型:
int
整型
float
浮動
complex
復雜
1)int (1) int)
Integer numeric type is used to store signed integers with no decimal points, like -5, 2, 78, etc.
整數數字類型用于存儲無小數點的帶符號整數,例如--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)浮動 (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).
浮點數字類型用于存儲浮點值,例如6.66、58.9、3.14等。浮點數也可以用科學計數法表示,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)復雜 (3) complex)
Complex numeric type is used to store complex numbers like 3.14j, 8.0 + 5.3j, 2+6j etc.
復數類型用于存儲復數,例如3.14j,8.0 + 5.3j,2 + 6j等。
Format: Real + Imaginary component j
格式:實數+虛數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.
注意:復數的虛部必須由j表示。 如果使用i表示它,則在Python中被視為無效語法。
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'>
演示所有數值數據類型的示例 (Examples demonstrating all numeric data types)
Let's look at some simple Python programs to demonstrate the numeric data types:
讓我們看一些簡單的Python程序來演示數字數據類型:
Note: type() is a function used to determine the type of a variable
注意: type()是用于確定變量類型的函數
Example 1: Program to print the data types of variables
示例1:打印變量數據類型的程序
# 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:將復數添加到整數和浮點型數字的程序
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數值類型