Python Scripts and Modules Explained - Programming Example
The interactive Python interpreter lets programmers run code a line at a time, which is typically used for short programs or just practicing python syntax. More commonly, programmers write Python program code in a file called a script, and execute the code using a Python interpreter. Programmers may write lots of separate script files, and even import code into other scripts. A module is a file that contains Python code that may be used by other modules or scripts. An import statement makes a module available for use via dot notation. Ex: math.pi When writing large programs, putting code into smaller modules makes managing it easier. There are many useful pre-installed modules that are part of the Python standard library that you can use in your programs. They typically contain useful functions, which we will learn in a future video. When you import a module, all code in the module is executed immediately. Python programs typically use the built-in special name __name__ to determine if the file was executed as a script by the programmer. Files may also be imported by another module. The code belonging to the following if statement executes only if file run as a script (i.e. running the file: python user.py) name = 'Bob'age = 20 if __name__ == '__main__': print(name, 'is', age, 'years old’) If you wanted to import the objects found in demo.py, then in another file, write the statement to the top of the program: import demo print(demo.name, 'is', demo.age, 'years old') Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.