Basic Data Types Examples


Basic Data Types Examples

This document introduces basic data types usage in OPBTPython, including type checking, booleans, bytes, integers, literals, and tuples.

Module Introduction

OPBTPython supports int, float, str, bool, bytes, list, dict, tuple and other types. Built-in example:

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

API Overview

  • type(x): Returns the type of x
  • bool(x): Convert to boolean
  • tuple(iterable): Construct tuple

Example Code

Type Checking (base_type.py)

#!pika
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')
#!pika

Boolean Type (bool.py excerpt)

#!pika
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')
#!pika

Tuple (tuple.py excerpt)

#!pika
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')
#!pika

Notes

  • bytes type usage follows platform standards; tuples are immutable.