Python Interactive Console Versus *.py Scripts
You can execute Python code by writing your scripts into text files and commonly using the
.py extension. Text files on most operating systems will be UTF-8 encoded by default. Python also reads UTF-8 encoded text files by default.
Create a new text file called
example.py and add the following text.
And then you can execute it using
python3 depending on your operating system and Python version.
PS> python ./example.py Hello World!
You can also enter Python code directly into the Python Interactive Console by typing just
python3 from the command line and then press
Enter. You then get a prompt like below.
PS> python Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Furthermore, you can now enter python commands directly.
>>> print("Hello World!") Hello World! >>>
To exit the Python Interactive Console, you can usually type
quit() or press
Ctrl-Z then press
This documentation will show examples of using both
*.py scripts and the interactive console to execute Python. Look out for the
>>> characters in the code blocks to indicate if I was using the Python Interactive Console or a
The code styling in this documentation is formatted using mostly PEP8 styling recommendations.
- UPPER_CASE : Constants will be defined using UPPER_CASE naming style.
- PascalCase : Class names will use PascalCase naming style
- snake_case : For variables names, method names and method arguments.
- Docstrings : Classes and methods contain extra documentation that is descriptive text enclosed in " or """ for multiline strings.
- _leading_underscore : Use a leading underscore to indicate variables that should be considered as private class variables.
See PEP-0008 : https://www.python.org/dev/peps/pep-0008/
I use the Pylint tool to check for code styling recommendations.
On most operating systems you would generally install Pylint by using the
pip install pylint
If using VSCode, open the Command Palette (Ctrl+Shift+P), then set the
Python: Enable Linting to
Python: Select Linter to
Common Pylint Warning and Error Messages
|C0114||Missing module docstring||Pylint requests that you add Docstrings. These are extra documentation within your code that describes its purpose and is enclosed in either " or """ in case of multiline strings. E.g., "The next line prints 'Hello World!'"|
|C0304||Final newline missing (missing-final-newline)||Pylint prefers a file to end with a new line. When copying code from a webpage into a
|R0201||Method could be a function (no-self-use)||Your method has an attribute that refers to
|R0903||Too few public methods (1/2) (too-few-public-methods)||The error assumes that your class may be used for just storing data. You could use a dictionary instead. However, the assumption is not always correct. You may be extending a class, or often in my case, I am trying to keep examples very small, readable and to the point. So you have the option to insert a Pylint declaration at the top of the file, or at a particular method declaration to ignore this Pylint error.
|E0110||Abstract class 'ClassName' with abstract methods instantiated (abstract-class-instantiated)||The Class that implements the abstract interface, or is inheriting from another abstract class, is not implementing all the abstract methods as described in the interfaces' signature; or if extending, then all the signatures of the abstract class that is being extended.|
|W0221||Parameters differ from overridden 'method' method (arguments-differ)||The arguments in your abstract class don't match the arguments in your implementing class. Check spelling of arguments.|
|W0612||Unused variable||You can remove the unused variable from your code. If you cannot remove the unused variable then use a
Command Line Interfaces
Command Line Interfaces (CLI) on different operating systems (Windows, Linux, macOS, Raspberry Pi) vary in appearance quite a lot.
You can use CMD, PowerShell or Git BASH on Windows, Bash on Linux or Terminal on macOS.
-- Windows PowerShell -- PS> python example.py PS E:\python_design_patterns> python example.py -- Windows CMD -- C:\> python example.py -- Git BASH Username@hostname MINGW64 /e/python_design_patterns $ python example.py -- Linux -- user@domain:~# python3 example.py user@domain:$/ python3 example.py user@domain:/python-design-patterns# python3 example.py $ python3 example.py # python3 example.py -- MacOSX-- hostname:~ username$ python3 example.py
Wikipedia - Command-line interface : https://en.wikipedia.org/wiki/Command-line_interface