12. Modules
To import a standard module and use its functions.
Explanation
Section titled “Explanation”- A module is a
.مfile you import into your program. The standard library is the set of modules that come with the compiler. - Write
استورد رياضيات(import math) at the top of the file, then call its functions like this:رياضيات.جذر(81.0), that is, the module name, a dot, then the function name.
Example
Section titled “Example”استورد رياضياتاطبع(رياضيات.جذر(81.0), " ", رياضيات.نسق(رياضيات.باي, 2))Common mistakes
Section titled “Common mistakes”Importing a module that does not exist:
استورد وحدة_لا_وجود_لهاExit code: 2
In the error catalog: E0167
Exercise
Section titled “Exercise”Write a function مسافة (distance) between two points using رياضيات.جذر (square root), and print the distance from (0, 0) to (3, 4), and from (1, 1) to (2, 2) to three decimal places with رياضيات.نسق.
Show the solution
استورد رياضيات
داله مسافة(س1 كسر64, ص1 كسر64, س2 كسر64, ص2 كسر64) كسر64: ارجع رياضيات.جذر((س2 - س1) * (س2 - س1) + (ص2 - ص1) * (ص2 - ص1))
اطبع(مسافة(0.0, 0.0, 3.0, 4.0))اطبع(رياضيات.نسق(مسافة(1.0, 1.0, 2.0, 2.0), 3))