Hello Class!
False
Introduction to Python
March 31, 2024
.
operator has a multiple meanings
Python comes with many operating systems, but to get the latest version…
brew install python
You want to always have the latest version, but this leads to many installations of Python on the same computer.
Each of these installations are referred to as “interpreters”.
VS Code allows you to choose a Python Interpreter when editing a “.py” file.
You can run a whole file of Python code by clicking the play arrow.
Or you can run a single line or selection…
Shift+Enter
Both of these will execute the Python code in a terminal.
There are two main options for setting up Python environments:
venv
Comes with the latest Python installations, similar to renv
for R, creates a folder with symlinks to the packages.
conda
Part of the Anaconda/miniconda world.
Can be used both as a package manager and for environments.
Conda manages both Python installations, packages, and environments from outside Python.
Venv manages Python environments from within Python.
Anaconda is a distribution of (1) a Python installation, (2) Conda environments, (3) a bunch of default packages.
Miniconda is a distribution of (1) a Python installation and (2) Conda environments.
You should be using environments for any language, but especially for Python.
For venv
the command to create a virtual environment is…
Luckily VS Code’s Python Extension makes handling these environments easy.
Open the Command Palette Shift+Cmd+P
Search for “Python: Create Environment…”
Select either “venv” or “conda”
Select the Python interpreter (version) to use
Now whenever you launch a terminal for this workspace, it wil use the environment you created.
Packages are how you can import functions.
You will sometimes see “Modules”. A package could have one or many modules within it.
Packages can be installed using
pip
built in to Pythonconda
pip
stands for “pip installs packages”.
It installs Python packages hosted on the Python Package Index (PyPI).
pip install numpy
is executed in a terminal, not in Python code itself, unlike R or Julia.
Installs packages hosted on the Anaconda repository.
Can also install other software, like R.
You can also use pip install
to install packages in a conda
environment, but this can cause conflicts, so you should use conda install
by default in this situation.
Once a package is installed (to your environment), you can…
numpy
is a package for numerical computation (ex. better arrays and linear algebra).
The first option is what is recommended and used most often.
Python uses a single =
for assignment
Math is not that different:
Logic operators are written out instead of symbols.
Lists are constructed as comma-separated elements in square brackets
Tuples are constructed as comma-separated elements in parentheses.
Sets are constructed as selements in curly braces.
Dictionaries are constructed as key:value
pairs in curly braces.
{'RI': 'Rhode Island', 'MA': 'Massachusetts', 'VT': 'Vermont'}
Python starts indexing from 0.
For some people, this is the mark of a true programming language.
Python is space sensitive.
For example, a for loop requires the looped lines to be offset by at least one space (customary to use a tab—4 spaces).
Python is an object-oriented programming language.
Example: a list is an object.
Objects have
Classes define objects; objects are the actual instance of the class.
Methods are a key feature of Python.
Methods are functions attached to an object that operate on the object.
Remember: methods are a type of function.
You can think of methods as functions that always take as an input the object they are defined for.
They may take other inputs as well.
If you wanted to append an element to a list…
append()
clear()
copy()
count()
extend()
index()
insert()
pop()
remove()
reverse()
sort()
Some functions that you would expect to be methods are not.
Example, len()
returns the length of an object.
Python functions are defined with the keyword def
and spacing:
Pandas
is a package that implements DataFrames in Python.
First, you need to install pandas
, then import it.
The construction of a DataFrame is based off of the dictionary objects.
Each column of a DataFrame is a Series.
Dataframes have a lot of their own methods.
describe()
will summarize all numerical columns.
A crucial step is reading and writing data.
Writing it out is a method:
All of the data science operations we saw with dplyr
and data.table
are possible with Python and pandas
.
numpy
vectors, arrays, numerical analysispandas
DataFramesmatplotlib
plottingSeaborn
plotting with pandas DataFramesplotly
interactive plotsScikit-Learn
machine learningTensorFlow
Neural NetsPyTorch
Neural Nets, but using GPUsBeautifulSoup
web scraping.
operator has a multiple meanings