蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

核对运算规则和迁移

PYTHONPython 3.12legacy-operator-migration.py
legacy-operator-migration.py64 行
import json

a, b = 14, 4
print(json.dumps({"arithmetic_and_division": [
    a + b, a - b, a * b, a / b, a // b, -a // b, -a % b, 3 ** 4,
    "ab" * 2, 1 / 2, 1.0 / 2, 1 / float(2), -a == (-a // b) * b + (-a % b),
]}))
print(json.dumps({"comparisons": [
    a == b, a != b, a > b, a < b, a >= a, b <= a, type(a < b).__name__,
]}))

score = 8
plus = minus = product = divide = rem = power = floor = score
plus += 3
minus -= 3
product *= 3
divide /= 2
rem %= 3
power **= 3
floor //= 3
current = alias = [7]
current += [8]
current = current + [9]

visited = []
def target():
    visited.append("target")
    return 0

cells = [[0]]
cells[target()] += [1]
print(json.dumps({"augmented_assignments": [
    score, plus, minus, product, divide, rem, power, floor, alias, current,
    visited, cells,
]}))

a, b = 44, 11
print(json.dumps({"integer_bits": [
    a & b, a | b, a ^ b, ~a, (~a) & 255, a << 3, -45 >> 2, a >> 2,
]}))

trail = []
def observed():
    trail.append("right")
    return 9

print(json.dumps({"short_circuit": [
    [] and observed(), "ok" or observed(), 0 or observed(), 8 and observed(),
    not [], trail,
]}))
print(json.dumps({"membership": [
    "ab" in "cable", 9 not in [7, 8], 8 in (7, 8), "x" in {"x": 8}, 8 in {"x": 8},
]}))
a = [7, 8]
alias = a
copy = a[:]
print(json.dumps({"identity": [a is alias, a is copy, a is not copy, a == copy, id(a) == id(alias)]}))
print(json.dumps({"precedence": [
    2 ** 3 ** 2, -2 ** 2, 2 ** -2, -(+2), 18 / 3 * 2, 9 - 4 + 2,
    1 << 2 + 1, 1 | 2 & 4, 1 | 1 ^ 1, 1 < 3 <= 4, 2 == 2 != 3,
    a is a, "a" in "cat", True or False and False, not True and False,
    (14 + 4) * 7 / 2, ((14 + 4) * 7) / 2, (14 + 4) * (7 / 2), 14 + (4 * 7) / 2,
]}))
本例需在 Python 3.12 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Python 3.12

入口文件:projects/python-legacy-lab/legacy-operator-migration.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 legacy-operator-migration.py,或下载右侧课程工程包。
  2. 确认本地已准备 Python 3.12。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Python2.x

课程关联工程包(登记关联44章) · 含当前章节或实例 · mbskycom202611a446.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习