Skip to content

Syntax Reference ​

PyCN keeps Python's syntax and replaces its vocabulary. English keywords are still accepted, and half-width and full-width punctuation can be mixed freely.

This page lists what the current parser implements. For a quick look at runnable code, see the examples.

Statements ​

PyCNPython
定义 函数名(参数):def name(params):
类 类名: / 类 类名(基类):class Name: / class Name(Base):
如果 条件: / 要不然 条件: / 否则:if / elif / else
迭代 变量 在 可迭代对象:for var in iterable:
返回 值 / 返回return value / return
跳出 / 继续 / 过break / continue / pass
导入 模块 作为 别名import module as alias
从 模块 导入 名称 作为 别名from module import name as alias
拉姆达 参数:表达式lambda params: expression
@装饰器 / 装饰 装饰器@decorator
变量 赋值为 值variable = value
甲,乙 赋值为 乙,甲a, b = b, a
对象.属性 赋值为 值obj.attr = value
字典【键】赋值为 值d[key] = value

Function parameters support *参数 for *args and **键值对参数 for **kwargs. Named parameters keep their Chinese names.

pycn
定义 求和(甲,乙,*参数,**键值对参数):
    返回 甲 加 乙

Keywords ​

PyCNPythonNotes
定义defFunction definition
如果if
要不然elif
否则else
迭代forMultiple loop variables are supported
在in
不是notUnary negation
和and
或or
空None
真True
假False
返回return
跳出break
继续continue
过pass
导入import
从from
作为as
类class
拉姆达lambda
装饰@Decorator token

The following keywords are recognized by the lexer but are not implemented by the parser yet: 当 (while), 是 (is), 尝试 / 意外情况 / 最终 (try / except / finally), 举起 (raise), 断言 (assert), 删除 (del), 全局的 (global), 非局部 (nonlocal), 产出 (yield), 等待 (await), 异步的 (async), 带上 (with), and 匹配 / 情况 (match / case).

Operators ​

PyCNPythonNotes
加+
减-
乘*
除以/
地板除//
取余 / 取模%
取幂**
赋值为=Assignment
等于==
不等于 / !=!=
小于 / 《<
小于等于 / 《=<=
大于 / 》>
大于等于 / 》=>=
按位与&
按位或``
按位异或^
按位取反~Unary
左移<<
右移>>
和and
或or
不是notUnary

Punctuation and literals ​

Chinese punctuation is optional: the lexer accepts both forms in the same file.

PyCNPython
( )( )
【 】[ ]
,,
::
“…” / ‘…’"..." / '...'
《 》< > in comparisons
!=!=

Braces { }, the dot . and the decorator @ stay half-width.

Data literals follow Python's model:

  • Numbers can be written in Arabic or Chinese numerals: 十二, 一百, 零点一, 负零点五.
  • Strings accept ASCII or Chinese quotes, and f-strings use the usual f"..." form.
  • Lists use 【】, dictionaries and sets use {}, and tuples use ().
  • 真, 假 and 空 map to True, False and None.
pycn
名字 赋值为 “张三”
成绩 赋值为 【九十,八十五,一百】
信息 赋值为 {"姓名": 名字, "成绩": 成绩}
打印(f"{名字} 的成绩是 {成绩【零】}")

Built-in functions ​

PyCNPythonPyCNPython
打印print范围range
整数int小数float
字符串str输入input
长度len列表list
字典dict集合set
元组tuple索引迭代enumerate
解析eval求和sum
最小值min最大值max
绝对值abs全部为真all
有一个为真any映射map
过滤filter拉链zip
打开open执行exec
类型type实例判断isinstance
子类判断issubclass目录dir
变量vars本地变量locals
全局变量globals帮助help
编号id排序sorted
父类super对象object
类方法classmethod静态方法staticmethod
属性property异常Exception

The built-in names can also be used as ordinary names where a variable is expected, such as function parameters.

List methods ​

PyCNPythonDescription
列表.添加(元素)list.append(element)Append one element
列表.插入(索引,元素)list.insert(index, element)Insert at an index
列表.移除(元素)list.remove(element)Remove the first match
列表.弹出() / 列表.弹出(索引)list.pop() / list.pop(index)Remove and return
列表.清空()list.clear()Remove all elements
列表.复制()list.copy()Shallow copy
列表.计数(元素)list.count(element)Count occurrences
列表.扩展(其它列表)list.extend(other)Extend with an iterable
列表.索引(元素)list.index(element)First index of an element
列表.反转()list.reverse()Reverse in place
列表.排序()list.sort()Sort in place

排序 maps to the sorted built-in when called directly, and to .sort() when used as an attribute.

Magic methods ​

PyCNPythonPyCNPython
魔法初始化__init__魔法新建__new__
魔法销毁__del__魔法字符串表示__str__
魔法表达式__repr__魔法字节表示__bytes__
魔法格式化__format__魔法等于__eq__
魔法不等于__ne__魔法小于__lt__
魔法小于等于__le__魔法大于__gt__
魔法大于等于__ge__魔法哈希__hash__
魔法布尔__bool__魔法属性获取__getattr__
魔法属性设置__setattr__魔法属性删除__delattr__
魔法属性获取项__getitem__魔法属性设置项__setitem__
魔法属性删除项__delitem__魔法长度__len__
魔法可迭代__iter__魔法迭代下一个__next__
魔法反转__reversed__魔法包含__contains__
魔法加__add__魔法减__sub__
魔法乘__mul__魔法除__truediv__
魔法地板除__floordiv__魔法取余__mod__
魔法取幂__pow__魔法左移__lshift__
魔法右移__rshift__魔法按位与__and__
魔法按位或__or__魔法按位异或__xor__
魔法取反__invert__魔法可调用__call__
魔法进入__enter__魔法退出__exit__
魔法拷贝__copy__魔法深拷贝__deepcopy__
魔法名称__name__

Current limitations ​

  • Keyword arguments in calls (name=value) and comprehensions are not supported.
  • while, try / except, with, async / await, yield, match, raise, assert, del, global and nonlocal are not implemented yet.
  • Multi-variable assignment targets must be plain names; attribute and index targets are assigned one at a time.
  • A local .pycn module is resolved by file name in the importing file's directory. Dotted module paths are not supported.

Document by Vincent-the-gamer | MIT Licensed