python 计算化学分子质量
时间: 2023-11-01 08:56:58 浏览: 151
在 Python 中计算分子质量可以使用化学元素周期表中的原子质量数据。下面是一个简单的代码实现:
```python
# 原子质量表
atomic_mass = {
"H": 1.00794,
"He": 4.002602,
"Li": 6.941,
"Be": 9.012182,
"B": 10.81,
"C": 12.011,
"N": 14.007,
"O": 15.999,
"F": 18.9984032,
"Ne": 20.1797,
"Na": 22.98976928,
"Mg": 24.305,
"Al": 26.9815385,
"Si": 28.085,
"P": 30.973,
"S": 32.06,
"Cl": 35.45,
"K": 39.0983,
"Ar": 39.948,
"Ca": 40.078,
"Sc": 44.955912,
"Ti": 47.867,
"V": 50.9415,
"Cr": 52.00,
"Mn": 54.938045,
"Fe": 55.845,
"Ni": 58.6934,
"Cu": 63.546,
"Zn": 65.38,
"Ga": 69.723,
"Ge": 72.63,
"As": 74.92160,
"Se": 78.96,
"Br": 79.904,
"Kr": 83.798,
"Rb": 85.4678,
"Sr": 87.62,
"Y": 88.90585,
"Zr": 91.224,
"Nb": 92.906,
"Mo": 95.94,
"Tc": 98.00,
"Ru": 101.07,
"Rh": 102.90550,
"Pd": 106.42,
"Ag": 107.8682,
"Cd": 112.411,
"In": 114.818,
"Sn": 118.710,
"Sb": 121.760,
"Te": 127.60,
"I": 126.90447,
"Xe": 131.293,
"Cs": 132.9054519,
"Ba": 137.327,
"La": 138.90547,
"Ce": 140.116,
"Pr": 140.90765,
"Nd": 144.242,
"Pm": 145.00,
"Sm": 150.36,
"Eu": 151.964,
"Gd": 157.25,
"Tb": 158.92535,
"Dy": 162.500,
"Ho": 164.93032,
"Er": 167.259,
"Tm": 168.93421,
"Yb": 173.054,
"Lu": 174.9668,
"Hf": 178.49,
"Ta": 180.94788,
"W": 183.84,
"Re": 186.207,
"Os": 190.23,
"Ir": 192.217,
"Pt": 195.084,
"Au": 196.966569,
"Hg": 200.592,
"Tl": 204.3833,
"Pb": 207.2,
"Bi": 208.98040,
"Th": 232.03806,
"Pa": 231.03588,
"U": 238.02891,
"Np": 237.00,
"Pu": 244.00,
"Am": 243.00,
"Cm": 247.00,
"Bk": 247.00,
"Cf": 251.00,
"Es": 252.00,
"Fm": 257.00,
"Md": 258.00,
"No": 259.00,
"Lr": 262.00,
"Rf": 267.00,
"Db": 270.00,
"Sg": 271.00,
"Bh": 270.00,
"Hs": 269.00,
"Mt": 278.00
}
# 计算分子质量
def molecular_weight(formula):
weight = 0
for atom in formula:
if atom in atomic_mass:
weight += atomic_mass[atom]
else:
print("Error: Unknown element '" + atom + "'")
return None
return weight
# 使用示例
print(molecular_weight("H2O")) # 输出 18.01528
print(molecular_weight("C6H12O6")) # 输出 180.15588
```
在上面的代码中,我们首先定义了一个字典 `atomic_mass`,其中包含了所有常见元素的原子质量。然后定义了一个函数 `molecular_weight`,该函数接受一个化学式作为参数,并计算出分子质量。最后,我们使用 `print` 函数输出了两个示例的分子质量。
阅读全文