Running and Quitting
|
Python programs are plain text files.
Use the Jupyter Notebook for editing and running Python.
The Notebook has Command and Edit modes.
Use the keyboard and mouse to select and edit cells.
The Notebook will turn Markdown into pretty-printed documentation.
Markdown does most of what HTML does.
|
Variables and Assignment
|
Use variables to store values.
Use print to display values.
Variables persist between cells.
Variables must be created before they are used.
Variables can be used in calculations.
Use an index to get a single character from a string.
Use a slice to get a substring.
Use the built-in function len to find the length of a string.
Python is case-sensitive.
Use meaningful variable names.
|
Numeric Data Types
|
Every value has a type.
Use the built-in function type to find the type of a value.
Types control what operations can be done on values.
Strings can be added and multiplied.
Strings have a length (but numbers don’t).
Must convert numbers to strings or vice versa when operating on them.
Can mix integers and floats freely in operations.
Variables only change value when something is assigned to them.
|
Sequence Types: Strings, Tuples and Lists
|
Strings, tuples, and lists are ordered collections of objects.
Strings and tuples are immutable.
Lists are mutable.
Strings are sequences of characters.
Tuples and lists can be of arbitrary (mixed) data types.
Unordered data types return data in a random order.
Access a specific item by its index with sequence[index] .
Access a range of items using slices: sequence[start:stop:skip] .
|
Sets and Dictionaries
|
A set is an unordered, unique collection of immutable objects.
A dictionary is a mapping between objects, from keys to values.
Dictionary keys must be immutable.
Get a value from a dictionary with my_dict[some_key] . Add an item with my_dict[some_key] = some_value .
Dictionaries and sets are mutable.
|
More on Iterable Data Types
|
Iterable data types are collections of objects.
Ordered data types perserve the order of creation.
Unordered data types return data in a random order.
Mutable data types can be changed in place.
Immutable data types cannot be changed in place. To modify them, you must create a new object.
All iterable types have a length, the number of items they hold
Choose an iterable type that makes your code safer and easier to understand.
|
For Loops
|
A for loop executes commands once for each value in a collection.
The first line of the for loop must end with a colon, and the body must be indented.
Indentation is always meaningful in Python.
A for loop is made up of a collection, a loop variable, and a body.
Loop variables can be called anything (but it is strongly advised to have a meaningful name to the looping variable).
The body of a loop can contain many statements.
Use range to iterate over a sequence of numbers.
The Accumulator pattern turns many values into one.
|
Comparisons and Conditionals
|
Use if statements to control whether or not a block of code is executed.
Conditionals are often used inside loops.
Use else to execute a block of code when an if condition is not true.
Use elif to specify additional tests.
Conditions are tested once, in order.
Create a table showing variables’ values to trace a program’s execution.
|
Built-in Functions and Help
|
Use comments to add documentation to programs.
A function may take zero or more arguments.
Commonly-used built-in functions include max , min , and round .
Functions may only work for certain (combinations of) arguments.
Functions may have default values for some arguments.
Use the built-in function help to get help for a function.
The Jupyter Notebook has two ways to get help.
Every function returns something.
Python reports a syntax error when it can’t understand the source of a program.
Python reports a runtime error when something goes wrong while a program is executing.
Fix syntax errors by reading the source code, and runtime errors by tracing the program’s execution.
|
Libraries
|
Most of the power of a programming language is in its libraries.
A program must import a library module in order to use it.
Use help to learn about the contents of a library module.
Import specific items from a library to shorten programs.
Create an alias for a library when importing it to shorten programs.
|
Reading Tabular Data into Pandas DataFrames
|
Use the Pandas library to do statistics on tabular data.
Use index_col to specify that a column’s values should be used as row headings.
Use DataFrame.info to find out more about a dataframe.
The DataFrame.columns variable stores information about the dataframe’s columns.
Use DataFrame.T to transpose a dataframe.
Use DataFrame.describe to get summary statistics about data.
|
More with Pandas DataFrames
|
Use DataFrame.iloc[..., ...] to select values by integer location.
Use : on its own to mean all columns or all rows.
Select multiple columns or rows using DataFrame.loc and a named slice.
Result of slicing can be used in further operations.
Use comparisons to select data based on value.
Select values or NaN using a Boolean mask.
|
Plotting
|
matplotlib is the most widely used scientific plotting library in Python.
Plot data directly from a Pandas dataframe.
Select and transform data, then plot it.
Many styles of plot are available.
Can plot many sets of data together.
|
Writing Functions
|
Break programs down into functions to make them easier to understand.
Define a function using def with a name, parameters, and a block of code.
Defining a function does not run it.
Arguments in call are matched to parameters in definition.
Functions may return a result to their caller using return .
|
Review Exercise
|
|
Command-Line Programs
|
|
Trying Different Methods
|
|
Program Flags
|
|
Defensive Programming
|
|
Refactoring
|
Refactoring makes code more modular, easier to read, and easier to understand.
Refactoring requires one to consdier future implications and generally enables others to use your code more easily.
|
Running Scripts and Importing
|
|
Programming Style
|
|
Wrap-Up
|
|
FIXME: more reference material.
FIXME: glossary.