nuwc_nps_banner.png

Python 2 vs Python 3?

Time for Python 2 is running out... https://pythonclock.org/

Programming Languages: Compiled vs. Interpreted

What Matters: Execution Time or Development Time?

Why Python?

Python is an Interpreted Language. What does that mean?

A Brief Overview of Methods for Interacting with the Python Interpreter

There are several "modes" for working with the Python interpeter:

Interacting with basic Python from the Terminal

The first mode of interacting with Python we will explore is interactive mode in the shell.

(1) Open the terminal/shell (if you forgot how, see the section on Unix commands above).

(2) At the command line, type: python

You should now see something similar to the following report in your terminal (PC and Mac reports vary slightly):

If Python has loaded correctly, the terminal command line will have changed from a $ symbol to >>>. The >>> is the Python prompt.

(3) Use the Python prompt as a calculator. Add a few numbers together (and hit enter). Do some more advanced math.

(4) To close out of Python (but remain in the terminal/shell), hit control-d on your keyboard or type exit(). The $ prompt should return. Note that when you exit, all Python variables/values are erased from memory.

The terminal is the most primitive environment, but also universally available.

Lesson 1.2: Overview

During this lesson, you will learn the following:

Interacting with enhanced IPython (interactive python) from the Terminal

The first mode of interacting with Python we will explore is interactive mode in the shell.

(1) Open the terminal/shell (if you forgot how, see the section on Unix commands above).

(2) At the command line, type: ipython

You should now see something similar to the following report in your terminal (PC and Mac reports vary slightly):

If Python has loaded correctly, the terminal command line will have changed from a $ symbol to In [1]:. The In [1]: is the Python prompt.

(3) Use the Python prompt as a calculator. Add a few numbers together (and hit enter). Do some more advanced math.

(4) To close out of Python (but remain in the terminal/shell), type exit on your keyboard. The $ prompt should return. Note that when you exit, all Python variables/values are erased from memory.

The IPython terminal has a number of additional features that make it more friendly (and more powerful), including a number of so-called "magic" commands (documentation here)

Interacting with Python Using Jupyter Notebooks

Jupyter notebooks provide a comprehensive environment for interactive and exploratory computing. They also allow you to save your work for your own viewing or viewing by others.

You launch a Jupyter Notebook from the Anaconda Navigator (the main app) or "manually" from a terminal window as follows:

  1. First, make sure you are in your "target" working directory by using the appropriate Unix commands to set your working directory to the Lab_Practicum folder on your desktop.

  2. Launch Jupyter Notebooks by entering the following command into the command prompt:

jupyter notebook

This should open up a window that looks like the one below in your default web browser (I recommend you set your default web browser to something other the Internet Explorer if you are using a PC):

Note that your "notebook list is empty." The environment you are looking at is the Jupyter Notebook Dashboard which is the front-facing entity for the Jupyter Notebook App. The Jupyter Notebook App is a server-client application that allows editing and running notebook documents via a web browser. This environment will also provide a Python "kernel" for interpreting commands you give it from a notebook. The dashboard environment functions very much like the traditional file system you are used to interacting with on a Mac or PC operating system. Let's start creating some content; let's build your first notebook.

  1. Click on "New" in the top-right hand of the Jupyter Notebook App and select "Python 3" (see image below):

This will launch a new browser window containing a new (blank) Jupyter Notebook.

  1. To save this notebook under a new name, go to the very top of the file and click on the the word "Untitled" which will be right next to the jupyter "headline" at the top. Change the name of the file to something meaningful (like "YourlastnamePythonIntro").

  2. Click on the browser tab that contains the Jupyter Notebook App (the file system view). You should now see your new file represented there as "YourlastnamePythonIntro.ipynb". This is an Jupyter Notebook file.

  3. Return back to your notebook browser tab. You should be looking at something that looks like this:

This is a blank notebook. You have been provided a cell to start working in. You can designate cells to perform a couple of different functions. The cell you are looking at is currently designated as a code block, indicated by the "In" marker on the left side. The "In" indicator functions much the same as the command prompt.

  1. A Jupyter notebook is divided into individual "cells". You can execute a given cell, either by clicking the "Run" button on the top menu, or by hitting "shift-enter" on your keyboard tells the Python kernel (interpreter) to execute the code block. Executing on a blank block generates a new block (a nice shortcut for creating new blocks to work in). Hit "shift-enter" a few times to create some empty cells. You can also use the "Insert" item on the banner to add more code blocks.
  1. There are different types of cells. The two main ones (that we will use) are
    • Code cells, live code connected to a Python interpreter
    • Markdown cells, used for text. These support markdown, html, and LaTeX!

Activity: Open a Jupyter Notebook and play around with creating new text cells and coding cells.

Executing Code in the Jupyter Notebook

Anything typed in a Jupyter notebook Code cell is "live code". When you click "Run" (or type Shirt-Return), the contents of that cell are sent to the Python interpreter and executed, as though you had typed it in an interactive IPython terminal session.

The simplest type of code to execute simply evaluates an expression, which is kind of like asking Python, "tell me the value of this expression"...

The numbers in [ ] provide the "numbered input/output prompts with command history" provides a record of what commands the Python interpreter has executed (and the order that it executed them in).

Activity: enter a cell below this one and play around entering an evaluating differnt types of input.

Data Types in Python

Literal values in Python have a type associated with them. We can use the built-in type() function to determine the type of data.

Python has a special value None (like Null in other languages)

Operators and Expressions

The same operator will behave differently on a different type of data.

Arithmetic Operators

The first type of operators we will learn are the arithmetic operators:

Python supports C-style shifting and masking

Integer Arithmetic

Arithmetic Involving Different Types

Order of Operations

For expressions involving multiple operators, Python follows an explicit ordering for evaluating each part:

  1. (): Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want, starting from the inner most parentheses and working outward.

  2. Unary operators (e.g., designating a negative number) have the next highest precedence.

  3. **: Exponentiation has the next highest precedence.

  4. *, /, //: Multiplication and Division have the same precedence.

  5. + and -: Addition and Subtraction have the same precedence.

  6. Operators with the same precedence are evaluated from left to right (except exponentiation).

Examples (evaluate these expressions by hand and check your work in Python):

"Shortcut" Operators

a +=5
b -= 7.1    # same as: b = b – 7.1;
c *= 2  # same as: c = c * 2;
d /= 2.0    # same as: d = d/2.0;
e %= 3  # same as: e = e % 3;

Variables

Identifiers

Examples:

Reserved Words

Relational and Logical Operators

Today's lesson will focus on using relational and logical operators to develop boolean expressions.

Control Structures

Conditional Execution

NOTE: Spacing Matters! Block structure dictated by indentation (white space)*

Activity: write a simple ```if``` statement that:

Activity: write a nested ```if``` statement that:

Activity: write a nested ```if``` statement that:

Activity: write an ```if``` statement that does the following:

Do not write two seperate if statements, write nested if statements.

The while loop

while boolean_expression:
    statement1
    statement2
    statement3

    update_statement     # prevent an infinite loop

The break statement

The continue statement

The for loop

for var in sequence:
    statements

Next Lesson: 1.3 Strings...