数据类型定义变量的类型。由于一切都是Python中的对象,因此数据类型实际上是类。变量是类的实例。
在任何编程语言中,都可以对不同类型的数据类型执行不同的操作,其中某些数据类型与其他数据类型相同,而某些数据类型可能非常特定于该特定数据类型。
Python默认具有以下内置数据类型。
类别 | 数据类型/类名 |
---|---|
Text/String | str |
Numeric | int , float , complex |
List | list , tuple , range |
Map | dict |
Set | set , frozenset |
Boolean | bool |
Binary | bytes , bytearray , memoryview |
字符串可以定义为用单引号,双引号或三引号引起来的字符序列。三引号(“””)可用于编写多行字符串。
$title('str'数据类型)
x = 'A'
y = "B"
z = """
C
"""
print(x) # prints A
print(y) # prints B
print(z) # prints C
print(x + y) # prints AB - concatenation
print(x*2) # prints AA - repeatition operator
name = str('john') # Constructor
sumOfItems = str(100) # type conversion from int to string
int
保留长度不受限制的带符号整数。float
保留浮点精度数字,并且它们的精度最高为15个小数位。complex
–复数包含实部和虚部。$title(数字类型)
x = 2 # int
x = int(2) # int
x = 2.5 # float
x = float(2.5) # float
x = 100+3j # complex
x = complex(100+3j) # complex
[ ]
,
:
]运算符可用于访问列表中的数据。+
)和重复操作符(*
)的工作原理类似的str
数据类型。sublist
,一个的取出list
使用切片运算符。list
的-除了tuple
是一个只读的数据结构,我们不能修改一个元组的项目的规模和价值。另外,项目用括号括起来(, )
。
$title(集合数据类型)
randomList = [1, "one", 2, "two"]
print (randomList); # prints [1, 'one', 2, 'two']
print (randomList + randomList); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']
print (randomList * 2); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']
alphabets = ["a", "b", "c", "d", "e", "f", "g", "h"]
print (alphabets[3:]); # range - prints ['d', 'e', 'f', 'g', 'h']
print (alphabets[0:2]); # range - prints ['a', 'b']
randomTuple = (1, "one", 2, "two")
print (randomTuple[0:2]); # range - prints (1, 'one')
randomTuple[0] = 0 # TypeError: 'tuple' object does not support item assignment
{, }
。
$title(字典数据类型)
charsMap = {1:'a', 2:'b', 3:'c', 4:'d'};
print (charsMap); # prints {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
print("1st entry is " + charsMap[1]); # prints 1st entry is a
print (charsMap.keys()); # prints dict_keys([1, 2, 3, 4])
print (charsMap.values()); # prints dict_values(['a', 'b', 'c', 'd'])
{, }
。list
,没有index
set元素。这意味着我们只能循环访问的元素set
。digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
print(digits) # prints {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
print(type(digits)) # prints <class 'set'>
print("looping through the set elements ... ")
for i in digits:
print(i) # prints 0 1 2 3 4 5 6 7 8 9 in new lines
digits.remove(0) # allowed in normal set
print(digits) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
frozenSetOfDigits = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
frozenSetOfDigits.remove(0) # AttributeError: 'frozenset' object has no attribute 'remove'
False
和True
。它们用于表示真值。在数字上下文中,它们的行为分别类似于整数0和1。
x = True
y = False
print(x) #True
print(y) #False
print(bool(1)) #True
print(bool(0)) #False
bytes
文字的语法与文字的语法相同string
,只是'b'
添加了前缀。bytearray
对象总是通过调用构造函数来创建的bytearray()
。这些是可变的对象。
x = b'char_data'
x = b"char_data"
y = bytearray(5)
z = memoryview(bytes(5))
print(x) # b'char_data'
print(y) # bytearray(b'\x00\x00\x00\x00\x00')
print(z) # <memory at 0x014CE328>
该type()
函数可用于获取任何对象的数据类型。
获取数据类型:
x = 5
print(type(x)) # <class 'int'>
y = 'howtodoinjava.com'
print(type(y)) # <class 'str'>
地址:https://www.leftso.com/article/725.html