Other Built-in Functions Examples

January 30, 2026

Other Built-in Functions Examples

This document introduces print, in/not in, equality comparison, is/is not, slicing, max/min, multiplication, XOR, eval, ctypes, and debugging, exceptions, etc.

Module Introduction

Covers common built-in functions and operators. Built-in example:

#!pika
print('hello', 42)
assert 1 in [1, 2, 3]
assert max(1, 2, 3) == 3
#!pika

Example Code

#!pika
print('test')
print('my name is', 'old wang', 'my age is', 43)
print('format: %s,%04d,%.2f' % ('test', 123, 15.5))
#!pika

Note: Multi-parameter printing and % formatting.

in Operator (contains.py)

#!pika
# test for __contains__

assert 't' in 'test'
assert 'q' not in 'test'
assert 'te' in 'test'
assert 'tq' not in 'test'

assert b't' in b'test'
assert b'q' not in b'test'
assert b'te' in b'test'
assert b'tq' not in b'test'

assert bytearray(b't') in bytearray(b'test')
assert bytearray(b'q') not in bytearray(b'test')
assert bytearray(b'te') in bytearray(b'test')
assert bytearray(b'tq') not in bytearray(b'test')

assert 1 in range(10)
assert 10 not in range(10)

assert (1,) in [(1,), (2,)]
assert (1,) not in [(2,), (3,)]

assert 'a' in {'a': 1, 'b': 2}
assert 'c' not in {'a': 1, 'b': 2}

print('PASS')
#!pika

Note: in/not in used for strings, bytes, range, tuple lists, dict keys, etc.

Equality Comparison (eq.py)

#!pika
assert [1, 2, 3] == [1, 2, 3]
assert [1, 2, 3] != [1, 2, 4]

assert (1, 2, 3) == (1, 2, 3)
assert (1, 2, 3) != (1, 2, 4)

assert (1, 2, 3) != [1, 2, 3]
assert [1, 2, 3] != (1, 2, 3)
assert [1, 2, 3] != 1

assert 1 != (1, 2, 3)

assert ('a', 'b', 'c') == ('a', 'b', 'c')
assert ('a', 'b', 'c') != ('a', 'b', 'd')

dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 1, 'b': 2, 'c': 3}
dict3 = {'a': 1, 'b': 2, 'c': 4}

assert dict1 == dict2
assert dict1 != dict3

print("PASS")
#!pika

Note: List, tuple, dict ==/!= compare by value; different types (like list vs tuple) are not equal.

is and is not (is_not.py)

#!pika
assert (1 is not 1) == False
assert (1 is not 2) == True
assert (1 is not None) == True
assert (None is not None) == False
assert (None is not 1) == True
assert (1 is not 1.0) == True
assert (1.0 is not 1) == True
assert (1.0 is not 1.0) == False
print("PASS")
#!pika

Note: is/is not compare object identity, different from ==; usage with None and numeric types need to note runtime environment differences.

Slicing and Indexing (slice.py)

#!pika
my_string = "Hello, World!"
out_of_range = False

# Test indexing beyond string length
try:
    my_string[len(my_string)] == ""
except:
    print("1.IndexError: Index beyond string length")
    out_of_range = True

assert out_of_range

# Test slicing beyond string length
b = my_string[0:100]
print(b)

# Test using negative indices to access string
assert my_string[-1] == "!"
assert my_string[-5:-1] == "orld"
print("PASS")
#!pika

Note: Out-of-bounds indexing triggers exception; slicing can exceed length (truncated); negative indices count from end.

max/min (max_min.py)

#!pika
# Test max() function
assert max(1, 2, 3) == 3, "max() function error: max(1, 2, 3) should return 3"
assert max(-1, -2, -3) == -1, "max() function error: max(-1, -2, -3) should return -1"
assert max(5, 5) == 5, "max() function error: max(5, 5) should return 5"
assert max([1, 2, 3, 4]) == 4, "max() function error: max([1, 2, 3, 4]) should return 4"

# Test min() function
assert min(1, 2, 3) == 1, "min() function error: min(1, 2, 3) should return 1"
assert min(-1, -2, -3) == -3, "min() function error: min(-1, -2, -3) should return -3"
assert min(5, 5) == 5, "min() function error: min(5, 5) should return 5"
assert min([1, 2, 3, 4]) == 1, "min() function error: min([1, 2, 3, 4]) should return 1"

print('PASS')
#!pika

Note: max/min support multiple parameters or single iterable parameter (like list).

Multiplication Operation (mult.py)

#!pika
# mult.py
assert 1 * 2 == 2
assert 1 * 2 * 3 == 6
assert 1 * 2 * 3 * 4 == 24
assert 'a' * 3 == 'aaa'
assert 'a' * 3 * 2 == 'aaaaaa'
assert 3 * 'bb' == 'bbbbbb'
assert 3 * 'bb' * 2 == 'bbbbbbbbbbbb'
assert 3 * 'bb' * 2 * 3 == 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
assert b'x' * 3 == b'xxx'
assert b'x' * 3 * 2 == b'xxxxxx'
assert 3 * b'yy' == b'yyyyyy'
assert 3 * b'yy' * 2 == b'yyyyyyyyyyyy'

assert [1,2,3] == [1,2,3]
assert [1,2,3] != [1,2,4]
assert 2 * [1, 2, 3] == [1, 2, 3, 1, 2, 3]
assert [1, 2, 3] * 2 == [1, 2, 3, 1, 2, 3]
assert 3 * [1, 2, 3] == [1, 2, 3, 1, 2, 3, 1, 2, 3]
assert [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]

print('PASS')
#!pika

Note: Integer multiplication; string/bytes multiplied by integer repeats; list multiplied by integer repeats concatenation.

XOR (XOR.py)

#!pika
# '6' in binary is '110', '3' in binary is '011'.
# Bitwise XOR operation: '110' ^ '011' = '101' = '5' in decimal
assert 6 ^ 3 == 5

# '10' in binary is '1010', '6' in binary is '0110'.
# Bitwise XOR operation: '1010' ^ '0110' = '1100' = '12' in decimal
assert 10 ^ 6 == 12

# Start with '6' ('110' in binary)
value = 6

# Bitwise XOR and assign with '3' ('011' in binary), '110' ^ '011' = '101'
value ^= 3
assert value == 5

# Bitwise XOR and assign with '10' ('1010' in binary), '101' ^ '1010' = '1111'
value ^= 10
assert value == 15


print('PASS')
#!pika

Note: ^ is bitwise XOR, ^= is XOR and assign.

eval (eval.py)

#!pika
def bar():
    # local scope
    local = 5
    assert eval('local') == 5


bar()

assert eval('1+1') == 2


def foo():
    return 3


# global scope
assert eval('foo()') == 3

g_val = 4
assert eval('g_val') == 4


print('PASS')
#!pika

Note: eval(string) executes expression in current scope, can access local and global variables; whether supported follows runtime environment.

ctypes Buffer (ctypes.py)

#!pika
import ctypes
read_data = ctypes.c_buffer(b'', 16)
print("----read size----")
datalen = len(read_data.raw)
print(datalen)
print(read_data.raw)
print("----read data----")
for i in range(0,datalen):
    print(read_data.raw[i])
#!pika

Note: c_buffer(initial_value, length) creates fixed-length buffer, .raw accesses underlying bytes; depends on whether runtime environment provides ctypes module.

Debug Breakpoint (pdb_set_break.py)

#!pika
import PikaDebug as pdb
pdb.set_trace()
pdb.set_break('pdb_set_break', 48)
print('line 1')
print('line 2')
print('line 3')
print('line 4')
print('line 5')
#!pika

Note: Use PikaDebug to set breakpoints and tracing, specific line numbers and module names need to match runtime environment; may require corresponding debug support on OP-BTS.

Raise Exception in Init (init_raise.py)

#!pika
class Test:
    def __init__(self):
        raise


t = Test()
#!pika

Note: Executing raise in __init__ causes construction to fail, subsequent t = Test() will throw exception; can be used to validate construction parameters or resource availability.

Notes

  • Availability of eval, ctypes, PikaDebug, etc. depends on firmware and module configuration.
  • Use eval cautiously in embedded environments to avoid executing untrusted strings.