This way we no longer have to use the dot notation when calling the square root function. And we’ll use the dot notation to call it: > math.sqrt(64)Īlternatively, you can use the keyword “ from” followed by the module name and “ import” followed by the attribute or function. The function to calculate square root is called ‘ sqrt’. To see the available functions and attributes for a module use the built-in function dir(): > dir(math) If you’ve ever imported something like random, NumPy, or math then it is really as simple as that! If you haven’t, then here’s a quick look at how it’s done.Īs an example, let’s use the math module to find the square root of a number. But what if you’ve defined your own function, saved it in a file, and would like to call it in another file? Import it! You have most likely used some of Python’s built-in functions already like print() and len(). If you’re new to Python and have stumbled upon this question, then I invite you to read on as I discuss how to call a function from another file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |