Python is by far my favorite programming language. I have been able to do so much with it: automation, Web Programming, Data Analysis, Data wrangling, etc. I was the kind of person that would first try to do a programming task in Python and ignore the rest. Just in the last few years, specially after the release of Chat GPT, I have changed my mind. I know am less concerned on what programming language I use, and simply code in the language that is needed for the task. Never the less, I am still a big fan of the entire Python ecosystem.
Basic Notepad++ Settings
In general I have used very basic methods to writing code and Notepad++ has been my favorite. I also have heavily made use of the Jupyter Notebook.
Inconsistent Indentation
If you are having issues running Python scripts and the error mentions issues related to “inconsistent indentation”, this usually means your notepad++ file is mixing tabs and spaces.
To see if your file has this issue, go to View > Show Symbol > Show White Space and TAB. If you see both tabs and spaces then that is your issue. Make sure you only use spaces.
Python Tab Settings
- Go to Settings > Preferences > Language
- Tab Settings, select python
- Change “Use default value” to “Replace by space”
Running a Basic Python Script
In certain job environments, double-clicking a .py file will not work for you. I have found that using Windows Powershell to run the Python file has provided me a much more consistent result. Create the two files below and place them in the same directory.
If you double-click the PowerShell script, this will run your Python file and you should see the “hello world” message.
example.py
RUN_script.cmd
Imports
Here we focus on how to import Python libraries. In most of the scripts you write, you will need to import certain libraries at the top of your code in order to have access to them. Don’t worry if you do not know what these libraries do, the important thing is to notice how these libraries are imported.
Variables
Here we will talk about declaring variables in Python. Note that no data types are need to be associated with variables and that all variables are objects in Python.
Data Structures
Python comes with several data structures or ways to store and work with data.
There are also dictionaries and tuples. Feel free to use Chat GPT to learn more about them. Out of these two, tuples are probably used less frequently.
Conditional Statements
Here are some examples of if/else statements
Looping
Functions
Coding Standards
I have always tried to push for the idea that everyone in the team should be following the same coding standards.
I try to follow the PEP 8 style guide: https://peps.python.org/pep-0008/
Indentation
- Use 4 spaces per indentation level
- Use spaces and not tabs
- Python disallows mixing tabs and spaces for indentation
- Make sure Notepad++ is set up to use spaces
Imports
- Imports are always put at the top of the file
- One library per line
String Quotes
- In Python, single-quoted strings and double-quoted string are the same
- Try to be consistent
Comments
- Use block commenting (""" or ''') for documenting functions/classes
- Use single # comments for everything else
Functions/Variables
- All lowercase names
- Use underscores to improve readability
- Use all caps for CONSTANTS
- Use CamelCase for classes
Folder Structure
Here is a basic folder structure if you are writing an application. There are many variations in the wild, use what works for you.
Project/ |—app.cmd |—tests.cmd |—config/ |—docs/ |—lib/ |—scripts/ |—spool/ |—tests/
- Project - Main folder, you can use any name you want here
- app.cmd - PowerShell command to run application
- tests.cmd - PowerShell command to run all tests
- config - Folder that contains all configuration files
- docs - Folder that contains all documentation files
- lib - Folder that contains all Python files
- scripts - Folder that contains all SQL files
- spool - Folder that contains all SQL spooled files
- tests - Folder that contains all test files
Libraries I have used in the past
- Pulp
- Pandas
- sqlite3
- matplotlib
- numpy
- Basic Tornado Application
- talib
- scipy
- redis
- reportlab
- sqlalchemy
- psycopg2
- smtplib
- datetime
- statsmodels
- json
- time
- logging
- sys
- urllib2
- urllib
- itertools
- sklearn
- BeautifulSoup
- jinja2
- glob
- requests
- subprocess
- os
- pyodbc
- bs4
- re
- io
- tkinter
- csv
- Music21
- SimPy - Introduction
- pil
- mpl_toolkits
- bokeh
- collections
- math
- argparse
- folium
- StringIO
- mpld3
- networkx
- seaborn
- sas7bdat
- random