数据类型是Python的基础之一,你可以利用数据类型做很多事情
事实上,所有的Python数据类型都可以被看做成一个对象。
一种数据类型就像某种数据的具体化,这些数据就是我们想要储存的记忆。Python有一些内置数据格式分类!
现在,让我们查看所有的数据类型, 通过使用type()
去展示可用的数据类型。
Sigle quotes
(单引号) 或者 double qoutes
(双引号),取决于你的选择例如:
greetings = ‘Hello, world‘
print(greetings) # Hello, world
print(type(greetings)) # <class ‘str‘>
例如:
day = 4
print(day) # 4
print(type(day)) # <class ‘int‘>
例如:
pi = 3.14
print(pi) # 3.14
print(type(pi)) # <class ‘float‘>
例如:
complex_number = 5 + 10j
print(complex_number) # (5+10j)
print(type(complex_number)) #<class ‘complex‘>
例如:
avengers = [‘Captain Amercian‘, ‘Iron Man‘, ‘Thor‘, ‘HUlk‘, ‘Black widow‘, ‘Hawkeye‘]
print(avengers) # [‘Captain Amercian‘, ‘Iron Man‘, ‘Thor‘, ‘HUlk‘, ‘Black widow‘, ‘Hawkeye‘]
print(type(avengers)) # <class ‘list‘>
例如:
avengers = (‘Captain Amercian‘, ‘Iron Man‘, ‘Thor‘, ‘HUlk‘, ‘Black widow‘, ‘Hawkeye‘)
print(avengers) # (‘Captain Amercian‘, ‘Iron Man‘, ‘Thor‘, ‘HUlk‘, ‘Black widow‘, ‘Hawkeye‘)
print(type(avengers)) # <class ‘tuple‘>
range
表述不可更改的一序列数例如:
ten = range(0, 10)
print(ten) # range(0, 10)
print(type(ten)) # <class ‘range‘>
dict
在Python中表示字典例如:
status = {"Learing":"Programming", "Language":"Python", "Day":4}
print(status) # {‘Learing‘: ‘Programming‘, ‘Language‘: ‘Python‘, ‘Day‘: 4}
print(type(status)) # <class ‘dict‘>
例如:
avengers = {‘Black Window‘, "Iron Man", "Thor", "Hawkeye", "Hulk", "Captain American"}
print(avengers) # {‘Hawkeye‘, ‘Iron Man‘, ‘Hulk‘, ‘Black Window‘, ‘Captain American‘, ‘Thor‘}
print(type(avengers)) # <class ‘set‘>
frozenset()
函数生成frozenset()
函数 接收可迭代,返回一个不可更改的被冻结的对象(它像一个集合对象,仅仅是不可以被更改)例如:
fruits = [‘apple‘, ‘banana‘, ‘cherry‘]
frozen = frozenset(fruits)
print(frozen) # frozenset({‘apple‘, ‘banana‘, ‘cherry‘})
print(type(frozen)) # <class ‘frozenset‘>
bool
在Python中代表着布尔值例如:
learning = True
print(learning) # True
print(type(learning)) # <class ‘bool‘>
distraction = False
print(distraction) # False
print(type(distraction)) # <class ‘bool‘>
bytes
数据类型可以用两种方式生成bytes()
函数例如:
# using bytes() function
hello = bytes("hello", "utf-8") # bytes(data, encoding)
print(hello) # b‘hello‘
print(type(hello)) # <class ‘bytes‘>
# using predix
Hello = b"Hello"
print(Hello) # b‘Hello‘
print(type(Hello)) # <class ‘bytes‘>
bytearray()
函数返回一个字节数组对象例如:
# using bytesarray() function
day4 = bytearray(4)
print(day4) # bytearray(b‘\x00\x00\x00\x00‘)
print(type(day4)) # <class ‘bytearray‘>
memoryview()
函数从一个具体的对象中返回一个记忆对象例如:
day4 = memoryview(bytes(4))
print(day4) # <memory at 0x000001FF555EDF40>
print(type(day4)) # <class ‘memoryview‘>
或许你已经发现,一些数据格式可以通过使用他们的指令实现。相同的技术可以被应用到每种数据类型上。
例如:
喜欢的朋友可以点赞,转发,留言哟!
往期推荐:
原文:https://www.cnblogs.com/RankFan/p/14794202.html