開始使用免費開始

Julia 中使用 Python 函式

既然你在前一個練習中已經用 PythonCall 從 Python 匯入了 math 套件,現在可以開始呼叫該套件裡的函式。

你把 math 以別名 pymath 匯入,所以當你要呼叫 math 的函式時,請在函式前加上 pymath 前綴。這能清楚表明你使用的是從 Python 匯入的套件版本。

本練習屬於課程

Julia 中級

檢視課程

練習說明

  • 使用 Julia 的中括號語法,定義向量 x,內容為從負三到三、步長為 1 的數值序列,包含 0。
  • 使用 math 套件中的 Python 函式 fabs,取得 x 的第二個值的絕對值。
  • 使用 math 套件中的 Python 函式 pow,將 x 的第六個值提升為 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], ____))
編輯並執行程式碼