在这个教程中,我们将学习如何使用 Python 创建一个高级计算器。这个计算器将支持基本的算术运算,如加、减、乘、除,以及更复杂的数学函数,如三角函数、指数和对数。

功能列表

  • 基本算术运算
  • 三角函数(正弦、余弦、正切)
  • 指数运算
  • 对数运算

创建计算器

首先,我们需要导入 Python 的 math 模块,它提供了许多数学函数。

import math

接下来,我们可以定义一个函数来处理用户的输入,并返回计算结果。

def advanced_calculator(operation, *args):
    if operation == 'add':
        return sum(args)
    elif operation == 'sin':
        return math.sin(args[0])
    elif operation == 'cos':
        return math.cos(args[0])
    elif operation == 'tan':
        return math.tan(args[0])
    elif operation == 'exp':
        return math.exp(args[0])
    elif operation == 'log':
        return math.log(args[0])
    else:
        return "未知操作"

使用示例

以下是如何使用这个高级计算器的示例:

print(advanced_calculator('add', 5, 3))  # 输出 8
print(advanced_calculator('sin', math.pi / 2))  # 输出 1.0
print(advanced_calculator('exp', 1))  # 输出 2.718281828459045

扩展阅读

想要了解更多关于 Python 的知识,可以访问我们的 Python 教程 页面。

[center][Golden_Retriever]