This lesson is being piloted (Beta version)

Introduction to Programming with Python: Glossary

Key Points

Running Jupyter
  • Python programs are plain text files.

  • Use the Jupyter Notebook for editing and running Python.

  • The Notebook has Control 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.

  • In Jupyter Notebooks, the ? command will display the same result as help() in a separate panel, with formatted text.

  • The official Python documentation is a good reference to the core Python language.

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.

  • Python is case-sensitive.

  • Use meaningful variable names.

  • Variables can be used in calculations.

Data 'types' and expressions
  • 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.

Lists and indexing
  • A list stores many values in a single structure.

  • Use an item’s index to fetch it from a list.

  • Lists are mutable: list values can be replaced by assigning to them.

  • Appending items to a list lengthens it.

  • Use del to remove items from a list entirely.

  • The empty list contains no values.

  • Lists may contain values of different types.

  • Character strings can be indexed like lists.

  • Python uses 0-based indexing. The first index is zero, the second index is one, and so forth

  • Indexing beyond the end of the collection is an error.

  • Tuples are another type of collection, but unlike lists, tuples are immutable.

Making Choices with Conditionals
  • Use if condition to start a conditional statement, elif condition to provide additional tests, and else to provide a default.

  • The bodies of the branches of conditional statements must be indented.

  • Use == to test for equality.

  • X and Y is only true if both X and Y are true.

  • X or Y is true if either X or Y, or both, are true.

  • Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.

  • True and False represent truth values.

Dictionaries
  • A dictionary is a data structure similar to a list, but that uses keys instead of indexes.

  • Dictionaries are created with {}, instead of [] for lists, or () for tuples.

  • A dictionary key can be a string, a number, or any hashable object.

  • To retrieve a value for a specific key we use the method .get.

  • Check if a key exists in a given dictionary by using the in operator.

  • We can iterate over a dictionary using a for loop to get both keys and keys:values.

  • A set is an unordered collection of items. Every element is unique and immutable.

  • Sets can be used to perform mathematical set operations like union, intersection, symmetric difference.

Repeating Actions with For Loops
  • Use for variable in sequence to process the elements of a sequence one at a time.

  • The body of a for loop must be indented.

  • Use len(thing) to determine the length of something that contains other values.

  • Use accumulator structures with a for loop to update the value of an existing variable

Working with Files
  • Files are opened with the open() function.

  • The open() function returns a file object.

  • Files need to be closed when they are no longer required by your program.

  • The close() method of the file object closes a file.

  • The with statement provides an elegant and safe way to automatically close your files.

  • Data can be read from text files a line at a time by the readline() file object method.

  • If you want to process each line of a text file in order, then iteration is useful.

  • Data can be written to a file with the write() file object method.

  • Patterns are common and repeatable recipes for common problems. The text file sequential processing pattern is simple, robust and efficient.

Modules
  • Modules are simply Python files.

  • To use code from a module, it has to be imported.

  • The import command is used to import code from a module.

  • There are different ways to import from a module. Select the appropriate method based on the required effect.

Error Messages
  • Syntax errors occur because of illegal language constructs. They are detected by the Python parser.

  • Sometimes the actual error occurs slightly before the location reported by the Python exception.

  • Runtime errors occur when something goes wrong while a program is executing.

  • In Python, run-time errors raise exceptions.

  • Indentation is meaningful in Python.

Functions
  • Don’t repeat yourself. Keep your code DRY by using 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 the function call are matched to the parameters in the function definition.

  • Optional arguments have a default value in the function definition.

  • Functions return results with the return statement.

  • Functions without explicit return values return None.

  • Within a function, print communicates with humans, return communicates with the computer.

  • Use docstrings to document your functions in a standard way.

Designing Functions
  • Functions operate on input data, producing output data

  • Identifying and describing the data task is the biggest challenge in writing functions

  • A systematic design recipe will help you write elegant functions from the get-go!

Python Syntax
  • Legal Python code follows rules of syntax

  • A Python keyword is a symbol that has special meaning in a Python script

  • Python provides for single and multiple line comments

  • An identifier names a value, e.g. as in the assignment statement: x = 21

  • A Python script consists of a sequence of statements that make use of keywords, identifiers and expressions

  • Indentation introduces a code block

  • [ and ] are used to surround literal list values

  • { and } are used to surround literal dictionary values

  • ( and ) are used for function parameters, to change expression evaluation order, and to specify tuple values

  • The PEP 8 Python Style Guidelines provide more code layout and convention details

Glossary

FIXME