List
- You can access values using negative indexes, which will go from right to left. The last element would be at index
-1
. - You can get multiple values with slicing, i.e.
l[start:end]
.start
is inclusive, andend
is exclusive.- If you are just slicing from the beginning, you can leave it out, e.g.
l[:3]
will slice from0
to3
(exclusive). - Similarly, if you are just slicing up to the end of the list, you can leave it out, e.g.
l[2:]
will slice from2
(inclusive) to the end.
- If you are just slicing from the beginning, you can leave it out, e.g.
- One correct way to copy a list is
b = a[:]
, since slicing gives you a new list. - You can combine two lists using
+
.
Build System
-
Key points
Due to Python being an interpreted language, its dependency management is similar to Node.js.
-
pyproject.toml
-
PEP 518 -- Specifying Minimum Build System Requirements for Python Projects (opens in a new tab)
Similar to
pom.xml
in Maven
-
Virtual Environments
-
Key points
- Virtual environments should be considered as similar to Maven POM to project dependencies, therefore one project per environment.
- One
virtual environment
should be dedicated to one project for dependency management purposes.
-
Workflow
-
Prepare pyenv-installed Python versions
pyenv install $python_version
-
Switch to the desired Python version for the current directory
pyenv local $python_version
This will create a
.python-version
file in the current directory (can be version controlled withgit
). -
Prepare Poetry project config (
pyproject.toml
)Make sure to specify the
project name
andPython version
, which will be used in the name of the virtual environment.# Create a new Poetry project file poetry init
# Example: pyproject.toml [tool.poetry] name = "03-visualizing-financial-time-series" [tool.poetry.dependencies] python = ">=3.9,<3.10"
-
Switch to the virtual environment using the specified Python version, must meet version constraint in
pyproject.toml
.poetry env use $python_version
This will create a virtual environment if it does not exist.
-
Resolve project dependencies
poetry lock
This will create a
poetry.lock
file in the current directory (can be version controlled withgit
). -
Install project dependencies
poetry install
-
Use the virtual environment in IDE if needed
# Get the path of the activated virtual environment poetry env list --full-path | grep Activated | awk '{print $1}'
-
Virtual Environments - Cheatsheet
venv - version control
- venv is bound to local Python installation, so it is not recommended to version control it.
- Build system declares the Python version to use, and venv is responsible for sourcing the correct version of Python installation.
Installing Packages from requirements.txt
pip install -r requirements.txt
Create a requirements.txt file with all the packages installed in the current environment
pip freeze > requirements.txt
pyenv
-
Key points
- Similar to
RVM
andNVM
pyenv
installs and manages Python versions itself.pyenv
addshims
with the same name toPATH
to overridepython
executables already inPATH
.Shims
will switch between Python versions.
- Similar to
pyenv - Cheatsheet
pyenv set up
-
Homebrew
brew install pyenv
-
zsh
# env export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH"
# init eval "$(pyenv init -)"
List all available Python versions
pyenv install -l
List all installed Python versions
pyenv versions
Determine which Pyenv-installed Python executable will actually be used
pyenv which $python_executable
# e.g. pyenv which python3
Select a Pyenv-installed Python for the current shell
pyenv shell $python_version
Select a Pyenv-installed Python for the current directory
pyenv local $python_version
Poetry
-
Key points
- All virtual environments created by Poetry are stored in
virtualenvs.path
in Poetry config.
- All virtual environments created by Poetry are stored in
Poetry - Cheatsheet
Poetry set up
Installation
-
Homebrew
brew install poetry
Create a project file template in the current directory
poetry init
Determine Python version of the current environment
poetry env info
List all environments of the current project
poetry env list --full-path
Switch to a different Python version
Will create a new environment with the specified Python version if it doesn't already exist.
poetry env use $python_version
Delete an environment
# Get the name of the environment
env_name=$(poetry env list | grep $python_version | awk '{print $1}')
# Delete the environment
poetry env remove $env_name
Note: you can only delete an environment of the current project.
Display the current config
poetry config --list
uv (opens in a new tab)
uv - Cheatsheet
Convert an existing project to uv
# pyproject.toml
[project]
name = "my_project"
version = "0.1.0"
requires-python = ">=3.9,<3.10"
dependencies = [
"alpha-vantage>=3.0.0",
"pandas>=2.0.0"
]
# Test the build
uv build
Add a new dependency
uv add alpha-vantage