def pressure(v, t, n):
"""Compute the pressure in pascals of an ideal gas.
Applies the ideal gas law: http://en.wikipedia.org/wiki/Ideal_gas_law
v -- volume of gas, in cubic meters
t -- absolute temperature in degrees kelvin
n -- particles of gas
"""
k = 1.38e-23 # Boltzmann's constant
return n * k * t / v
help(pressure) # 查看文档字符串
参数默认值
k_b=1.38e-23 # Boltzmann's constant
def pressure(v, t, n=6.022e23):
return n * k_b * t / v
函数体的大多数数据
应该表示为具名参数的默认值
永远不会改变的值
就像基本常数k_b,应该定义在全局帧中。
python 可以有多个 return 值
def divide_exact(n,d):
return n // d, n % d
quotient, remainder = divide_exact(2013, 10)
>>> quotient
201
>>> remainder
3