Learn CS

Python

Overview

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

—The Python Tutorial

Official Resource

Hello World

print('Hello World')

Insight

Package/Environment Management

pip

pip is the package installer for Python. Sometimes pip refers to either pip or pip3. As Python 2 gets deprecated, hopefully it would not be ambiguous in the future.

Generally you don't need to worry about installation of pip itself. And here are the common commands with pip (or pip3).

# install package
pip install packageName         # latest version
pip install packageName==1.0.0  # specific version
pip install packageName>=2.0.0  # minimum version

# install a list of requirements specified in a file
pip install -r requirements.txt

# update package
pip install --upgrade [packageName]

# remove package
pip uninstall [packageName]

For more information, visit User Guide and Reference Guide on pip documentation.

Virtual Environment

When working with multiple projects, you may encounter version conflict problem: some project requires a specific version of a package, while another project requires another version. Virtual environment helps to solve that issue: you can create different virtual environments for different projects.

Newer versions of Python comes with venv, so it's handy to create virtual environment directly.

# create a virtual environment
python3 -m venv tutorial-env

# activate a virtual environment after creating one
tutorial-env\Scripts\activate.bat   # on Windows
source tutorial-env/bin/activate    # on Unix or MacOS

# deactivate the virtual environment after activating
deactivate

Here is the documentation of venv.

Besides, there're also virtualenv which is more powerful than venv, and Pipenv which combines pip and virtualenv.

Anaconda

Anaconda makes it easier to manage packages and environments. Anaconda® is a package manager, an environment manager, a Python/R data science distribution, and a collection of over 7,500+ open-source packages. Anaconda is free and easy to install, and it offers free community support. You can download it free.

Personally, I'd recommend using Anaconda because:

  • it includes almost all packages you need, you don't have to install them manually
  • it protects your system environment, especially if you are using macOS

Data science

Python plays an important role in data science. It is worth of another page to discuss data science. Here I'll just list some of the packages related to data science.

Data and computing

  • NumPy
  • pandas
  • SciPy

Data Visualization

  • Matplotlib
  • Seaborn

Machine Learning

  • Scikit-Learn
  • NLTK

Deep Learning

  • TensorFlow
  • PyTorch

Courses