Files
task-3-2-1-Text-Processing-…/yzz1.py
2026-04-21 11:29:52 +08:00

32 lines
914 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 定义向量 A 和 B
A = [3, 4]
B = [1, 2]
# ======================
# 方法1纯Python手动计算适合理解原理
# ======================
print("==== 纯Python计算结果 ====")
# 1. 向量加法 A + B
add_result = [A[0]+B[0], A[1]+B[1]]
print("A + B =", add_result)
# 2. 数乘 2×A
mul_result = [2*A[0], 2*A[1]]
print("2 × A =", mul_result)
# 3. 向量A的长度勾股定理 √(x²+y²)
import math
norm_A = math.sqrt(A[0]**2 + A[1]**2)
print("A 的长度(模)= %.2f" % norm_A)
# ======================
# 方法2NumPy库工业界标准写法
# ======================
print("\n==== NumPy计算结果 ====")
import numpy as np
A_np = np.array([3, 4])
B_np = np.array([1, 2])
print("A + B =", A_np + B_np) # 加法
print("2 × A =", 2 * A_np) # 数乘
print("A 的长度(模)= %.2f" % np.linalg.norm(A_np)) # 模长