Files
task-3-2-1-Text-Processing-…/260421-2509165039.py
2026-04-21 11:30:18 +08:00

43 lines
1.0 KiB
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.

# 题目1 在Python中用两种方式表示"Hello"
# 1.用 ord() 函数打印每个字符的ASCII码
text = "Hello"
for char in text:
print(f"'{char}'的ASCII码是:{ord(char)}")
# 2.用 chr() 函数验证字符65对应的是大写字母A
char_65 = chr(65)
print(f"ASCII码65对应的字符是:{cahr_65}")
print(f"验证结果:{char_65 == 'A'}")
# 题目3
# 1.计算 A + B 的结果:
A = [3,4]
B = [1,2]
A_plus_B = [a + b for a,b in zip(A,B)]
print(f"A + B = {A_plus_B}")
# 2.计算 2 × A 的结果:
scalar = 2
two_A = [scalar * a for a in A]
print(f"2 * A = {two_A}")
# 3.计算 A 的长度(模):
import math
A_magnitutude = math.sqrt(sum(a**2 for a in A))
print(f"A的长度(模)是:{A_magnitutude}")
# 题目4
# 1.计算它们的点积 A · B:
A = [1,2,3]
B = [4,5,6]
dot_product = sum(a * b for a,b in zip(A,B))
print(f"A * B ={dot_product}")
# 2.计算它们的余弦相似度:
import math
A = [1,2,3]
B = [4,5,6]
dot_product = sum(a * b for a,b in )