Post/Code

HomeAboutUsesNow

Python Basics: Cheat Sheet.

Today I began a new nanodegree from Udacity: the Full Stack Web Developer Nanodegree.

After successfully completing my React Developer Nanodegree, I decided it was time to work towards some credentials to put behind my backend development skills. While I have been developing with Node.js and MongoDB for some time, my JavaScript knowledge, both front-end and back, is mostly self-taught (along with numerous online courses). Apart from long-past project work in PHP and some basics in Java, I have not really dug into another programming language in a meaningful way. While JavaScript works throughout the stack (and I am continually working to improve on it), I felt it was time to learn a new language and to start a journey towards becoming a polyglot developer. My knowledge of SQL is also limited so this is fantastic opportunity to fill in some of the larger gaps in my working knowledge!

That being said, here are some of my notes I have kept handy for working with Python (assuming Python2 and 3 are installed - depending on your setup):

Command Line.

In the command line:

  • python --version displays the Python 2 version number.
  • python3 --version displays the Python 3 version number.
  • python3 enters the Python3 REPL (Read Evaluate Print Loop).
  • pip --version Python Package Installer. Is the equivalent of NPM for Python packages.
  • pip install --upgrade pip Upgrades pip.

Once in the Python REPL:

  • Pressing Ctrl + z will put the running code in the background. (You can enter fg to bring it back to the foreground).
  • Pressing Ctrl + c or Ctrl + d will 'kill' the REPL.

Running Python Code.

When working with Python, it is useful to set a Virtual Environment or 'venv' to keep version numbers for projects in harmony. (I think of this as keeping a .nvmrc to set the Node version or having an "engine" field in your package.json file):

  • python3.7.4 -m venv env will setup up the venv.
  • course env/bin/activate will activate (or re-activate the venv)
  • deactivate will deactivate the venv.

Flask.

Flask is a lightweight framework for Python, primarily for building APIs. (Similar to Express for Node.js):

  • pip install Flask will install Flask from pip.
  • pip freeze > requirements.txt will write all your dependencies to a requirements.txt file. (Similar to package.json)
  • pip install pylint will install 'Pylint' into the venv for development purposes. (In a way, similar to NVM, you can have different dependecies available in your particular project venv)

VS Code.

  • Installed the "autopep8" extension for automatic code formatting for .py files.
  • Installed the "pylint" extension for linting of .py files.
  • Installed the Python extension for overall VS Code improvements for .py files.

I am very excited to dig into this new chapter of development journey! I did start dipping my toe into the water a little while back with this very basic, 101 API in Flask with SQLite.

This might also be a good time to review my Postgres Cheat Sheet from a while back...

While I am familiar with SQL and can read it well enough, I have never really had to work with SQL much (I have built some basic APIs with Node.js/Express and Sequelize).

In parallel with my Full Stack Nanodegree, I am also doing a Udacity short-course called SQL for Data Analysis to brush up on my SQL.

Here is my SQL Cheat Sheet so far.