Python数据类型

‘’'内置数据类型
在编程中,数据类型是一个重要的概念。

变量可以存储不同类型的数据,并且不同类型可以执行不同的操作。

在这些类别中,Python 默认拥有以下内置数据类型:
‘’’

文本类型: str

test_str = ‘我是str’
print(‘test_str的数据类型是:’, type(test_str))

#test_str的数据类型是: <class ‘str’>

数值类型: int, float, complex

test_int = 20201105
print(‘test_int的数据类型是:’, type(test_int))

#test_int的数据类型是: <class ‘int’>

test_float = 666.222
print(‘test_float的数据类型是:’, type(test_float))

#test_float的数据类型是: <class ‘float’>

test_complex = 4 + 5j
print(‘test_complex的数据类型是:’, type(test_complex))

#test_complex的数据类型是: <class ‘complex’>

序列类型: list, tuple, range

test_list = [‘一’, ‘个’, ‘小’, ‘笨’, ‘egg’, ‘0-0’]
print(‘test_list的数据类型是:’, type(test_list))

#test_list的数据类型是: <class ‘list’>

test_tuple = (‘tuple’, ‘is’, ‘unchangeable’, ‘tuple是不可变的’)
print(‘test_tuple的数据类型是:’, type(test_tuple))

#test_tuple的数据类型是: <class ‘tuple’>

test_range = range(1024)
print(‘test_range的数据类型是:’, type(test_range))

#test_range的数据类型是: <class ‘range’>

映射类型: dict

test_dict = {‘key’: ‘book’, ‘value’: ‘Python大蟒蛇’}
print(‘test_dict的数据类型是:’, type(test_dict))

#test_dict的数据类型是: <class ‘dict’>

集合类型: set, frozenset

test_set = {‘我是’, ‘小’, ‘八哥’}
print(‘test_set的数据类型是:’, type(test_set))

#test_set的数据类型是: <class ‘set’>

test_frozenset = frozenset({‘frozenset’, ‘比’, ‘set’, ‘多一组括号!’})
print(‘test_frozenset的数据类型是:’, type(test_frozenset))

#test_frozenset的数据类型是: <class ‘frozenset’>

布尔类型: bool

test_bool = False
print(‘双性人test_bool的数据类型是:’, type(test_bool))

#双性人test_bool的数据类型是: <class ‘bool’>

二进制类型: bytes, bytearray, memoryview

test_bytes = b’I am bytes’
print(‘test_bytes的数据类型是:’, type(test_bytes))

#test_bytes的数据类型是: <class ‘bytes’>

test_bytearray = bytearray(2)
print(test_bytearray)
print(‘test_bytearray的数据类型是:’, type(test_bytearray))

#bytearray(b’\x00\x00’)
#test_bytearray的数据类型是: <class ‘bytearray’>

test_memoryview = memoryview(bytes(2))
print(test_memoryview)
print(‘test_memoryview的数据类型是:’, type(test_memoryview))

#<memory at 0x02D5F028>
#test_memoryview的数据类型是: <class ‘memoryview’>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值