This lesson is being piloted (Beta version)

Intermediate Programming with Python: Glossary

Key Points

Creating and Running Python Scripts
  • Python scripts are plain text files, usually with the .py extension.

  • You execute Python scripts at the command-line with the Python interpreter.

  • Values are displayed using the print function.

  • Comments are indicated with #. Anything between the # and the end of the line is ignored.

While loops
  • Most programs will require ‘Loops’ and ‘Branching’ constructs.

  • while statements, in addition to for, allow for looping through sections of code

  • The programmer must provide a condition to end a while loop.

Getting Help When You Need It: Working with Documentation
  • The official Python documentation is a good reference to the core Python language.

  • You should ensure that the correct Python version is selected in the online documentation, as language features have changed over time.

  • There are several ways to check the Python version you are using.

  • The help() function displays the documentation for a given Python object.

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

  • The type() function returns the type of a Python object.

Errors and Exceptions
  • Illegal language constructs are called syntax errors. They are detected by the Python parser.

  • Legal code can also produce errors during execution, known as run-time errors.

  • In Python, run-time errors raise exceptions.

  • Exceptions are handled with the try-catch language feature.

  • You can raise your own exceptions with the raise keyword.

  • Look Before You Leap (LBYL) is a defensive programming style where you check for problems before executing important code.

  • It’s Easier to Ask Forgiveness than Permission (EAFP) is a programming style where you wait for errors to occur and then handle them later.

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.

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.

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

Getting arguments from the command-line
  • Using command-line arguments can make your programs easier to use and reuse.

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.

What's in a __name__?
  • __name__ is a string containing the module name.

  • For files directly executed by Python, __name__ gets the special value “main”, indicating that this is the main file.

  • The if __name__ == "__main__" expression prevents code from running when the module is being imported.

Testing
  • Verification vs validation, different levels.

  • There are numerous frameworks and tools, but we use pytest here.

  • Test Driven Development places the focus on writing just enough code at each point in time to make a failing test pass.

  • A doctest consists of a multi-line string containing a mixture of text, code and expected results (implicit assertions).

Final practical: How Many Articles Reference that they use R and/or Python?
  • XXXXXXX

  • XXXXXXX

  • XXXXXXX

  • XXXXXXX

Glossary

FIXME