开始使用免费开始使用

在 Julia 中调用 Python 函数

既然您在上一个练习中通过 PythonCall 已经从 Python 导入了 math 包,现在可以开始调用该包中的函数了。

您使用别名 pymath 导入了 math,因此在调用 math 中的函数时,请在函数名前加上 pymath 作为前缀。这样可以清楚地表明您使用的是从 Python 导入的该包。

本练习是课程的一部分

Julia 中级

查看课程

练习说明

  • 使用 Julia 的方括号语法,定义一个向量 x,其取值从负三到三,步长为 1,并包含 0。
  • 使用 math 包中的 Python 函数 fabs,获取 x 的第二个值的绝对值。
  • 使用 math 包中的 Python 函数 pow,将 x 的第六个值提升到第七个值的幂。

交互式实操练习

通过完成这段示例代码来试试这个练习。

using PythonCall
pymath = pyimport("math")

# Define a vector x from -3 to 3
x = ____

# Print the absolute value of the second value in x using pymath
println(____.____(____))

# Print the sixth value of x raised to the power of the 7th value of x
println(____.____(x[6], ____))
编辑并运行代码