Skip to content

Instantly share code, notes, and snippets.

@estysdesu
Last active May 5, 2020 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estysdesu/a8200ac1bcbfd9b1c50dd4f22dd19e7c to your computer and use it in GitHub Desktop.
Save estysdesu/a8200ac1bcbfd9b1c50dd4f22dd19e7c to your computer and use it in GitHub Desktop.
[Python: Learning Materials] #python #study #learn

Python 3 Learning Materials

Installing

  1. I would recommend skipping installing Python on your machine for now. Instead, I would start with something like Google Colab. It's linked to your Google account and Google Drive and they have a generous free tier. This is an excellent environment for first learning Python in and similar to a Jupyter Lab/Notebook environment that you will likely use in the future at some point.
  2. After using Google Colab for some time, Python can be installed multiple ways. My recommendations:
    • Windows
      1. Scoop or Chocolatey package manager (scoop install python)
      2. Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)
      3. Using Ubuntu under Window's Subsystem for Linux (then see Linux instructions).
    • macOS
      1. Homebrew package manager (brew install python)
      2. Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)
    • Linux
      1. Distrobution's package manager (Debina/Ubuntu: apt install python3 && apt install python3-pip)
      2. Anaconda/Spyder provides a GUI similar to Matlab's GUI (great for data science, data exploration, or other STEM tasks)

Environment (and Project) Setup

  • Note: I'm using a *unix system containing Bash syntax as the example for these commands.
  1. Please, please, please use a virtual environment (abbreviated venv or virtualenv) for each of your projects! I can't stress this enough. Virtual environments are a way to isolate the dependencies of projects with respect to your system's environment. Think of it like global and local scope. Playing with global scope can be dangerous unless you know what you're doing. The virtual environment may look different depending on your installation method.
    1. System package manager, Python.org, or any other installation method: Python comes bundled with a module that helps you minimally manage your virtual environments. I'd recommend starting with this tool to learn the flow of creating environments, installing packages inside them, and removing them before before trying other tools that can manage some of those steps for you.
      1. A new Python virtual environment can be created with python3 -m venv <path-to-environment-directory>. This will create a Python virutual environment at the directory specified (the venv module will create the directory as long as the parent directory exists).
      2. To activate the virtual environment, run source <path-to-environment-directory>/bin/activate. Running which python3 should now give the location of the Python in the virtual environment (Note: the virtual environment will carry the version of whatever Python it was created with; therefore, running python is synonymous with python3 as is pip and pip3). The package installer, pip, is also a part of the environment (pip is actually a Python module just like venv) and any packages install with it will be isolated inside the virtual environment. To say differently, any package installed with pip in the virtual environment will not be located by Python when the virtual environment is not active.
      3. To deactivate the virtual environment, run deactivate. To delete the virtual environment, delete the directory specified when creating it.
      4. After playing with the venv module, I'd recommend looking into the virtualenv package instead of using the native venv module shipped with Python. It provides many niceties over the venv module and is one of the most popular. For me, the main difference is integration with pyenv and the location of the environments it creates is automatically managed.
    2. Anaconda/Miniconda: Anaconda and Miniconda come with a built in environment manager and Python package registry, conda. By default, an Anaconda install includes many data science packages (numpy, pandas, matplotlib, jupyter, etc.). Many Anaconda users don't need to mess with packages or environments for a while. Anaconda has good documentation for these topics and there is an excellent post by Gergely Szerovay.
    3. Any installation method: Poetry is a Python dependency management tool that manages project environments and packages. It bundles environment management, package installation (with separation between development and package dependencies), and package publishing. Their website is the source of documentation as the project is continuously evolving.

References

  • Don't understand something about the language?
    1. Start here: Python Docs Search
    2. Still don't understand? That's okay, try here: W3 Schools
  • Want to ask a question? (make sure to always search online for an answer first!)
    1. Connect with hundreds of thousands of experienced and young Pythonistas like you: r/LearnPython
    2. Optimizing or solving a complex problem: StackOverflow Python
  • What does idiomatic Python look like?
    1. Idiomatic Python code is referred to as "pythonic". The most popular python code syntax standard is released as a PEP (Python Enhancement Proposal). It is called PEP 8. It is generally referred to as the standard of how Python code should be written.
    2. There are many code syntax and format analyzers/checkers. Recently, most popular is black, autopep8, isort, and mypy.
      • black is a code formatter. It is very opinionated, but generally accepted. Another alternative is autopep8.
      • isort sorts your imports. This is not mentioned in PEP 8, but is generally preferred over random import lists.
      • mypy is a static type checker. As of Python 3.5, Python gained optional static typing features. Using a linter like mypy can catch type errors that duck typing/dynamic typing languages often allow us to make.

Practicing (Basics)

  • Exercism.io
    • I'm actually a Python mentor on this site, its really easy to join and practice/learn good Python styling and code structure. Make sure to try mentored mode if you can. The feedback mentors give you can be invaluable.
  • W3 Schools
    • Provides quizzes with immediate feedback.
  • Python Practices
    • Provides a solution and difficulty levels.

Data Science/STEM Tools and Libraries Intro

Appendix

A: Troubleshooting

  • Python isn't found as a command?
    • This means Python isn't in the PATH. PATH is a list of the directories that your shell searches for when you run a command. Editing the PATH differs by OS. Please check with a guide specific to your OS.

B: FAQ

  • Why don't you recommend installing Python from python.org?
    • The Python Organization's installation of Python doesn't give you the added benefits that installing by other means does. System package managers can help you keep your Python installation up to date and generally place python in your system's PATH. Anaconda comes with it's own environment and Python package manager (conda vs. pip). These save you the time of having to manually set up the PATH and keep your Python install updated (and save you the possibility of making a mistake while installing Python manually and having to venture into Google Hell to fix it). To be clear, there is nothing wrong with installing from python.org and sometimes there are reasons for it. But, for the average user, there are better methods in my opinion.
  • What is a requirements.txt and why do I need one?
    • Many projects have a requirements.txt file that list the packages used in the Python code for that project. It's simply a list in the format of <package>==<version>. You can install those packages using pip really easily! Try pip3 install -r requirements.txt (the -r flag is short for --requirement). Make sure you're in a virtual environment first! It's helpful so that 3rd parties that use your code can understand and install packages that satisfy the requirements needed to run your code successfully. You yourself can create one from your Python environment by running pip3 freeze > requirements.txt.
  • I see online that Python 2 has reached its end of life. What does that mean?
    • Python, like all software, has versions. Python 2 has been around since the early 2000's and the Python Organization decided to bump the major version (2 -> 3) as there were many breaking changes between the versions (the versions are not closely compatible). Recently, Python 2 has been in maintenance mode and only security patches were released as new development on Python shifted to focus only on Python 3. As of 2020, Python 2 is no longer maintained and will not receive any patches or updates including for security. It is recommended that all new Python code be written so that it is compatible with Python 3.
  • What are optional types in Python?
    • Python 3.5 introduced the concept of [and 3.6 changed the syntax of] optional types. Optional types are type annotations in Python code to improve the readability, maintenance, and testing of Python code. I generally see this as a boon. Static typing gives users of a codebase more information (such as better hints on what types a function expects, etc.) on how to satisfy APIs and maintain existing APIs. mypy is one of the more popular static type checkers in Python. More info can be found in PEP 484.

TODO:

  • talk about [Ana/mini]conda
  • talk about poetry
  • separate out into repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment