Please ensure you have Python installed, either via Conda or the python installer.
I'd suggest using one of the following:
- Miniconda - I find the Conda ecosystem to be unnecessarily confusing, but if you already use it, you might want to continue.
- Python - This is the vanilla python. It's my suggested version.
If you use Linux or MacOS, you can install python with the package manager for your system.
When Python was created, it was convention to have all modules and libraries installed globally; i.e. every program you used, could only use the same version as every other program. To address this, several different, but similar, solutions have been created. The two most important are Python's venv and Conda's Envs.
This is built into Python's standard library and it's documentation is here.
Creating a new Virtual Environment:
python -3 -m venv <venv>
Activating the environment depends on your shell:
Platform | Shell | Command to activate virtual environment |
---|---|---|
POSIX | bash/zsh | $ source <venv>/bin/activate |
fish | $ source <venv>/bin/activate.fish |
|
csh/tcsh | $ source <venv>/bin/activate.csh |
|
PowerShell Core | $ <venv>/bin/Activate.ps1 |
|
Windows | cmd.exe | C:\> <venv>\Scripts\activate.bat |
PowerShell | PS C:\> <venv>\Scripts\Activate.ps1 |
Any installs from this point are installed in the virtual environment.
To store the versions used, run pip3 freeze > requirements.txt
.
Deactivation:
deactivate
Create:
conda create --name myenv
Activate:
conda activate myenv
Any Conda installs from this point go into the virtual environment.
Deactivate:
conda deactivate
The standard library for Python contains tools which are general and performant. A full list can be found here. What follows is a sampling I think is a good place to start.
Docs.
The re
library contains tools to work with Regular Expressions for pattern matching.
It's worth noting, not all regular expression syntax is the same.
import re
expression = re.compile(r"abc")
print(expression.match("abcdef"))
print(expression.match("defghi"))
Docs.
Itertools are useful for common enumerations to make iteration more efficient and easy to understand.
import itertools
list(itertools.permutations('ABCD', 2))
Argparse is a tool for parsing command line arguments.
import argparse
parser = argparse.ArgumentParser(description="An example for Argparse")
parser.add_argument("-n", "--n-iterations", type=int, help="number of iterations to perform")
args = parser.parse_args()
print(args)
We'll use pytest
as our test runner so let's make sure that it's installed:
$ pip3 install pytest
# OR
$ conda install pytest
Unit testing is the practice of testing each "unit" of code. For example
import unittest
def collatz_steps(n: int) -> int:
steps = 0
while n > 1:
if n & 1 == 0:
n = n // 2
else:
n = 3 * n + 1
steps += 1
return steps
class CollatzStepsTest(unittest.TestCase):
def test_base_case(self):
self.assertEqual(collatz_steps(1), 0)
def test_larger(self):
self.assertEqual(collatz_steps(27), 111)