{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Started\n", "\n", "## Overview, Objectives, and Key Terms\n", "\n", "In this lesson, we'll walk through the installation of, access to, and use \n", "of Python.\n", "\n", "### Objectives\n", "\n", "By the end of this lesson, you should be able to\n", "\n", "- *access Python on departmental machines* (or your own machine)\n", "- *define variables using basic Python types*\n", "- *explain what a variable is*\n", "\n", "### Key Terms\n", "\n", "- interpreted programming language\n", "- compiled programming language\n", "- integrated development environment\n", "- Spyder\n", "- Jupyter notebook\n", "- variable\n", "- `int`\n", "- `float`\n", "- `str`\n", "- `bool`\n", "- assignment \n", "\n", "\n", "## Overview of Python\n", "\n", "Python is a scripting language, which means \n", "it is [interpreted](https://en.wikipedia.org/wiki/Interpreted_language)\n", "and not [compiled](https://en.wikipedia.org/wiki/Compiled_language).\n", "Interpreted languages are converted from human-readable text files to binary \n", "(i.e., the 1's and 0's a computer understands) on-the-fly. That makes the \n", "cycle of changing a program, running it, correcting the program for errors, and \n", "re-running it much, much faster than with compiled languages. A compiled \n", "language, on the other hand, is converted to binary once-and-for all by an \n", "explicit compilation step. That step makes rapid changes a bit more \n", "painstaking to implement. Learning to program is so much easier without this \n", "step, and many of the problems we need to solve don't really require the added \n", "computational performance that compiled languages provide.\n", "\n", "Several versions of Python are in current use, but the most popular versions \n", "are 2.7 and 3.7. Despite the numbering, both versions are current \n", "(but [not forever](https://pythonclock.org/)). In this \n", "class, we will work exclusively with Python 3.X (the X is currently 7 and may change over time).\n", "\n", "## Python Distributions\n", "\n", "In addition to various versions of Python, there are many distributions of \n", "Python. By distribution, I mean a collection of the Python interpreter (which \n", "is what executes the code you write), editors, and/or packages for performing \n", "specific tasks. Here, we'll be using \n", "[Conda](https://conda.io/docs/),\n", "which provides a very easy way to manage Python and its packages on Windows, \n", "Mac OS/X, and Linux machines. Currently, a full version of Conda is installed \n", "on the departmental machines and represent everything you need for this course.\n", "\n", "For folks who want to install Python on their own machines, you may install\n", "the same [Anaconda](https://www.anaconda.com/distribution/) package on the MNE machines.\n", "Note, the file is pretty large. Be sure to select the latest version (3.7 as of 5/25/19) appropriate\n", "for your operating system." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Accessing Python\n", "\n", "For folks working on MNE machines, the following is a quick overview of getting access. You will need to make time to visit one of the labs (or to install Python on your own machine) to follow along with examples in this and all future readings.\n", "\n", "### Finding CMD\n", "\n", "Press `windows+r` (the Windows key and the `r` key at the same time) to open `Run`. \n", "\n", "![Finding CMD](access_python_1.png)\n", "\n", "### Interactive Python via IDLE\n", "\n", "In the CMD window, type \"python\" and press enter to open Python. \n", "\n", "![Finding CMD](access_python_2.png)\n", "\n", "You should see some information about the Python build (which, in this case, uses the version Python 3.5.2). You will also see the traditional marker `>>>` followed by a (possibly blinking) cursor. It's here that you can enter single lines of Python code. Type the following and press enter:\n", "\n", "```\n", ">>> print(\"Hello, world!\")\n", "```\n", "\n", "What happens?\n", "\n", "This particular interface to Python is called the [Integrated Development and Learning Environment (IDLE)](https://docs.python.org/3/library/idle.html). An alternative (that we'll also see in Spyder below) is IPython, which can be used by typing \"ipython\" in place of \"python\" in your original CMD window.\n", "\n", "### Spyder\n", "\n", "The primary way that I would recommend using Python for anything but the smallest tasks is by using Spyder, an [integrated development environment](https://en.wikipedia.org/wiki/Integrated_development_environment). Unlike IDLE, which is based entirely in the terminal window, Spyder provides a complete environment that incorporates a text editor (for writing complete Python programs), a variable explorer (we'll see that that means later), and a Python terminal (either IDLE or IPython), among other features. \n", "\n", "To access Spyder, type \"Spyder\" into the CMD window and press enter.\n", "\n", "![Opening Spyder](access_python_4.png)\n", "\n", "\n", "You should see something like the following:\n", "\n", "![Spyder interface](access_python_5.png)\n", "\n", "To the left is the text editor. To the upper right is a tabbed pane that defaults to a Help menu. Notice one of the other tabs is \"Variable explorer.\" Finally, the lower right pane contains the IPython console.\n", "\n", "### Jupyter Notebook\n", "\n", "Another way to interact with Python is through use of [Jupyter notebooks](http://jupyter.org/). Jupyter allows one to produce documents within a web browser that contain text, images, links, and, of course, Python code. In fact, these online materials were all produced using the notebook format and converted to the form you are now seeing! \n", "\n", "To fire up Jupyter, just type \"jupyter notebook\" in the CMD window and press enter. \n", "\n", "![Starting Jupyter](access_python_6.png)\n", "\n", "You should see something like the following:\n", "\n", "![Jupyter interface](access_python_7.png)\n", "\n", "You should recognize the listed items: they are the various folders in your home directory. Now, let's make a new notebook. First navigate to whichever directory you want to contain the file (here, that's the Documents folder). Then click the \"New\" icon in the upper right and select \"Python [default]\", as shown below:\n", "\n", "![Creating a Notebook](access_python_8.png)\n", "\n", "What you get should look like the following:\n", "\n", "![Notebook interface](access_python_9.png)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Types\n", "\n", "For the rest of this lesson, we'll take a look at the various data types one can use in \n", "the Python language. These types range from the simple `int` and\n", "`float` numerical types, to the flexible container types\n", "`list`, `tuple`, and `dict`. Certainly, we'll encounter\n", "other types as we go on (and we can even define our own types with classes), but an \n", "understanding of these basic types is essential\n", "for getting the most out of Python.\n", "\n", "Python, like every other language I've used, uses [variables](https://en.wikipedia.org/wiki/Variable_%28computer_science%29), which consist of a *name* to which a *value*\n", "can be *assigned*. For example, consider the following lines of Python code:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "y = 1.0" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "z = '1.0'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "z = float(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The names `x`, `y`, and `z` are each assigned values that\n", "look pretty similar (they all appear equivalent to unity). However, the \n", "*type* of each of these values is different. For instance, `x` in this case is an `int`, i.e., and integer value. \n", "\n", "Because the value assigned to `y` explicitly includes a decimal \n", "point, its Python type is `float`, i.e., a [floating-point](https://en.wikipedia.org/wiki/Floating-point_arithmetic) value. We'll skip the details, but the floating-point system used by Python (and many other languages) approximates the real number system (including 1.0, 1.1, 3.14159..., etc.) with a *finite* set of numbers defined between predefined minimum and maximum values.\n", "\n", "The addition of quotes in the *first* value assigned to `z` makes it a `str`, i.e., a string value. Note, strings can be defined using either single quotes `'` or double quotes `\"`, but the pair must match. In other words, `'1.0'` and `\"1.0\"` are fine (and equivalent), but `'1.0\"` and `\"1.0'` are invalid. \n", "\n", "\n", "The *second* value assigned to `z` used the built-in function `float()`, which converts whatever is passed to it (here, `z`) into a `float`. In this particular case, the final value of `z` is equal to `y`.\n", "\n", "As a final example, consider the following:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "b = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, `b` is a `bool`. [Boolean](https://en.wikipedia.org/wiki/Boolean_data_type) variables have one of two values: `True` or `False`.\n", "\n", "> **Warning**: The value of a `bool` is either `True` or `False` and the *capitalization* is *required*\n", "\n", "All five examples above use the [assignment operator](http://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html#the-assignment-operator), i.e., a single `=`\n", "(equal sign). This \"operation\" takes a name (e.g., `x`) and gives it a \n", "value (e.g., `1`). We'll see a much more complete picture of the \n", "basic operations in Python next time.\n", "\n", "These examples also highlight a key feature of Python: variables\n", "are strong- and dynamically-typed. A strong-typed language means \n", "that values have a fixed type until that type is changed explicitly.\n", "A dynamically-typed language means that variables can be assigned \n", "values of different types without restriction. Contrarily, static-typed\n", "languages require that variables be assigned a single, fixed type that \n", "is used throughout the variable's \n", "[scope](http://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html#variable-scope-and-lifetime). Don't worry: we'll explore in more detail what scope means later on in the course.\n", "\n", "\n", "## On Naming Variables\n", "\n", "Finally, variable names are subject to a few rules:\n", "\n", " 1. The first character must be a letter (`a-z` or `A-Z`) or an underscore (`_`), but letters are preferred.\n", " 2. Variable names cannot have spaces.\n", " 3. Variable names can include numbers (`0-9`) anywhere in the name except the first character (e.g., `abc123` is an acceptable name but `99redballoons` is not.)\n", " 4. Variable names cannot be *keywords* from the Python language. These include the names `for`, `if`, and some others that you'll see in the next reading.\n", "\n", "In general, one should use as descriptive a name as is possible for variables. For example, if a variable is to represent the mass of an object, it might be more illustrative to use the name `mass` than a single `m`. However, there are other techniques (e.g., comments) that can also be used to help a reader of code to understand what each variable represents. Examples of such techniques will be provided throughout the readings to follow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further Reading\n", "\n", "In addition to the various links provided above, skim the [Introduction](http://python-textbok.readthedocs.io/en/latest/Introduction.html) from [Object-Oriented Programming in Python](http://python-textbok.readthedocs.io/en/latest/index.html)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }