Python Basics
Python is used as the primary programming language for the DSP section of ringbuffer.org. There are countless nigh-quality online resources for learning Python and this chapter does not intend to give a full introduction. Rather, it is a primer that covers some of the relevant aspects to make it easier to get started.
Additional resources might be necessary. The official Python documentation is recommended in this case:
Another resource for detailled examples can be found here:
Syntax
No Termination
Unlike many other languages, python does not require semicolons to terminate statements:
Termination can be necessary in some cases to suppress output from functions.
Indents
In Python, code indents are a part of the syntax. This is a very important and unique feature of python. This will become more important when using loops and conditions, as indatation can have an effect on the outcome of the code.
Code blocks always need to have the same indatation level. When using wrong indents:
the interpreter will give the following error message:
IndentationError: unexpected indent
Importing Modules
In Python, modules can be imported to extend the functionality. This is the equivalent to including libraries in other programming languages. Modules can be imported at any point in a script, before they are needed, but it is commom to import all modules at the beginning. After import, functions from a module can be called with the dot syntax:
It is common to import modules with shorter aliases for shorter code. Standard modules have typical aliases, that are used throughout the community:
If only a specific part of a module is needed, it can be imported exclusively with an alias to make it lightweight and fast:
Data Types
Strings
Lists
The list is Python's default, builtin data type to store collections of data. Lists are ordered, mutable and can combine different data types. Elements of lists can be accessed with brackets.
Arrays
Numpy arrays allow only the same datatype. They offer more operations than simple lists and can be created with different methods:
Control Structures
While Loop
For Loop
For loops in Python iterate over a list or other sequences:
Note that the range function creates a list of integers, counting from 0 to 9. This example is a more explicit loop over a list:
Conditional Statements
elif is an additional conditional statement that is evaluated if the previous if is false:
Writing Functions
Functions are defined using the def keyword. Arguments are declared in the parenthesis after the function's name. Return values are specified by the return keyword. Multiple values can be returned if comma separated. Note the indent to mark the function's body.
Since python code is not compiled but interpreted, functions need to be defined before they can be called:
Namespaces
All variables defined at the main level of the program (not in a function or class) are stored in the Global Namespace as global variables. All gobal variable can be inspected in an IDE like Spyder and `listed as follows:
Every function creates a new namespace while being executed - see the LEGB rule (Local, Enclosing, Global, and Built-in).