Python Fundamentals

python logo

✅ Objectives

  • Review a brief history of Python
  • Use ‘pip’ to manage Python packages
  • Debug Python code with shell, print, and ipdb
  • Review common Python data types
  • Understand Python conditionals and control flow
  • Write Python functions
  • Review Python variable scope and the global keyword
  • Read Python error messages and exceptions
  • Handle errors with try: and except:

What is Python ❓

  • interpreted scripting language
  • object-oriented
  • dynamically typed
  • supports modules and packages
  • extensive standard library
  • emphasis on readability

A little history... 📚

  • Who: Guido van Rossum

  • When: February 20, 1991

  • Eponym: Monty Python’s Flying Circus

    Began as a personal project based on another language he wrote called ABC with the intention of making it easier to read and program in C.

    Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another’s code; too little and expressiveness is endangered.

    -Guido van Rossum

PyPi and Pip

What do PyPi and Pip stand for?

Let's install a popular package for sending HTTP requests

pip install requests

PyPi

To install a specific version:

pip install requests==2.22.0

To uninstall a package:

pip uninstall requests

When deploying, you will use pip to create a requirements file:

pip install -r requirements.txt

The Python Shell

  • Python comes with a built in REPL.
  • Start a REPL by typing python at the command line.
  • Exit with exit() or ctrl + d
Let’s try it!

Python’s Common Data Types

Example Data Type Name
"Hello Python" str string
20 int integer
20.5 float float
[1, 2, 3] list list
(1, 2, 3) tuple tuple
{"name": "Mimi", "age": 10} dict dictionary
{2, 4, 6} set set
True bool boolean
None NoneType NoneType

The Virtual Environment 🌲

Pyenv

Pyenv is the tool we’ll use to install and manage different versions of Python.

  • pyenv versions will show you what Python versions are currently installed on your machine
  • pyenv install -l will show a list of versions available to install
  • pyenv install 3.9.2 will install that version on your machine
  • pyenv global 3.9.2 will set that as the global version on your machine

Pipenv

Pipenv is a tool built upon pip which can create virtual environments and install packages in them.

  • Pipfile: look for this first, it is analogous to npm’s package.json file and lists the dependencies and Python version for the current project
  • Pipfile.lock: similar to package-lock.json, this file describes both the Pipfile dependencies AND their dependencies with exact versions

If the project directory doesn’t have these, you need to create a virtual environment

pipenv --python 3.8.13

Now you can start adding dependencies similarly to how you would install packages with pip:

pipenv install requests
Command Description
pipenv install creates the virtual environment and installs dependencies
pipenv shell activates the virtual environment
ctrl + d deactivates the virtual environment
pipenv --rm removes the virtual environment

Let’s dive into the code! 🤿