Import statement allows you to use code that in different files or written by others.
Put all your code in one file is bad for maintenance and hard to navigate between code. Put your code in to multiple files can make your work easier.
Just for demonstration, let’s create two files, one called
main.py and another called
functions.py. Then put the following code to those files:
main.py, you will see
Hello, Pegasis! been printed out.
If you think use
fileName.functionName() to invoke functions in other files is annoying, use another way to import it:
But this way is not encouraged because you may have conflicts. (for example a function you import have the same name with a function in your code)
You can also import from others code using same syntax. For example you can import numpy for scientific math calculation, import pygame for making games and import tkinter for creating GUI (which we will learn later).