基础数据类型示例

January 30, 2026

基础数据类型示例

本文档介绍 OPBTPython 中基础数据类型的使用,包括类型检查、布尔、字节、整数、字面量和元组。示例来源:doc/pikapython.com/examples/builtins/

模块简介

OPBTPython 支持 int、float、str、bool、bytes、list、dict、tuple 等类型。内嵌示例:

a = 1
b = 1.0
c = "test"
d = True
e = (1, 2, 3)

API 概览

  • type(x):返回 x 的类型
  • bool(x):转换为布尔
  • tuple(iterable):构造元组

示例代码

类型检查(base_type.py)

assert type('test') == str
assert type(1) == int
assert type(1.0) == float
assert type(True) == bool
assert type([]) == list
assert type({}) == dict
assert type(()) == tuple
print('PASS')

布尔类型(bool.py 节选)

assert isinstance(True, bool)
assert bool(1) == True
assert bool(0) == False
assert bool("") == False
assert (True and False) == False
assert (True or False) == True
if True:
    print('ok')

元组(tuple.py 节选)

t = tuple()
l = [1, 2, 3, 4, 5]
t = tuple(l)
assert t[0] == 1
t = tuple('test')
assert (1, 2, 3) + (4, 5, 6) == (1, 2, 3, 4, 5, 6)
assert 2 * (1, 2, 3) == (1, 2, 3, 1, 2, 3)
print('PASS')

注意事项

  • 字节类型 bytes 用法以平台为准;元组不可变。

相关链接