类型转换与关键字:μC/OS-III 操作系统概要
类型转换
数据处理中不可避免地需要进行类型转换。如表所示,μC/OS-III 提供了多种转换方法:
| 方法 | 描述 | 示例 | 结果 |
|---|---|---|---|
| int(x [,base]) | 将 x 转换为整数 | int(5) | 5 |
| long(x [,base] ) | 将 x 转换为长整数 | long(100000) | 100000 |
| float(x) | 将 x 转换为浮点数 | float(3.14) | 3.14 |
| chr(x) | 将整数转换为字符 | chr(65) | 'A' |
| unichr(x) | 将整数转换为 Unicode 字符 | unichr(931) | 'Α' |
| ord(x) | 将字符转换为其整数值 | ord('A') | 65 |
| hex(x) | 将整数转换为十六进制字符串 | hex(15) | '0xf' |
| oct(x) | 将整数转换为八进制字符串 | oct(10) | '0o12' |
| list(s) | 将序列 s 转换为列表 | list('hello') | ['h', 'e', 'l', 'l', 'o'] |
| set(s) | 转换为可变集合 | set([1, 2, 3]) | {1, 2, 3} |
| frozenset(s) | 转换为不可变集合 | frozenset([1, 2, 3]) | frozenset({1, 2, 3}) |
| complex(real [,imag]) | 创建复数 | complex(3, 4) | 3+4j |
| eval(str) | 计算字符串中的有效 Python 表达式 | eval('1 + 2') | 3
关键字
μC/OS-III 使用关键字,即一系列预定义的标识符,代表特定含义和方法。Python 关键字如表所示:
| 关键字 | 描述 | 示例 |
|---|---|---|
| False | 布尔值假 | if False: print('False') |
| None | 特殊值无 | if x is None: print('x is None') |
| True | 布尔值真 | if True: print('True') |
| and | 逻辑与 | if x and y: print('x and y are True') |
| assert | 断言检查 | assert x > 0, 'x must be positive' |
| async | 异步操作 | async def my_async_function(): pass |
| await | 等待异步操作 | await my_async_function() |
| break | 退出循环或语句 | break |
| class | 定义类 | class MyClass: pass |
| continue | 跳过当前循环迭代 | continue |
| def | 定义函数 | def my_function(): pass |
| del | 删除变量或属性 | del x |
| elif | else if 条件 | if x > 0: ... elif x < 0>
| else | else 语句 | if x > 0: ... else: ... |
| except | 处理异常 | try: ... except Exception as e: ... |
| finally | 无论是否出现异常,都执行 finally 块 | try: ... finally: ... |
| for | 循环 over 序列 | for x in my_list: ... |
| from | 导入模块或包 | from my_module import my_function |
| global | 声明全局变量 | global x |
| if | if 条件 | if x > 0: ... |
| import | 导入模块 | import my_module |
| in | 测试成员关系 | if x in my_list: ... |
| is | 身份比较 | if x is y: ... |
| lambda | 匿名函数 | lambda x: x ** 2 |
| nonlocal | 声明非局部变量 | nonlocal x |
| not | 逻辑非 | if not x: print('x is False') |
| or | 逻辑或 | if x or y: print('x or y is True') |
| pass | 空语句 | pass |
| raise | 引发异常 | raise Exception('my exception') |
| return | 从函数返回 | return x |
| try | 异常处理块 | try: ... except Exception as e: ... |
| while | while 循环 | while x > 0: ... |
| with | with 语句 | with open('my_file.txt') as f: ... |
| yield | 生成器函数 | def my_generator(): yield x
评论区