Python Intro for MATLAB Users#

Introduction#

This is a Jupyter notebook rendered as a page of a Jupyter book. Please see this page for more information on the functionality available

Python and MATLAB share many common features (and some common syntax), but have separate histories and are set up to make different problems “easy”.

MATLAB was originally developed for matrix manipulation and numerical calculation, and its syntax makes these operations simple to write. Conversely, it has relatively complicated and obscure abilities for string processing and network functionality. Python is a “general purpose” programming language, and the “out-of-the-box” interpreter is not as well suited to purely numerical operations, while handling strings, files and network protocols more gracefully. However, this deficiency can be largely fixed using a few popular and well known extensions (“packages” in Python terminology) to add back functionality which looks (deliberately, since its developers had used MATLAB) a lot like the MATLAB functions.

Some key differences#

Output is often through the print() function#

When code is run line-by-line in a Python interpretter, then as with MATLAB, the result of expressions is evaluated by default and printed to the screen. However, when run in a Jupyter cell, only the last line is evaluated, and when run as a separate script, no output is sent to screen by default.

To print the output of expressions & variables, the print() function can be used. This prints the content inside the brackets, usually to the screen.

Square brackets don’t create matrices#

In MATLAB, writing code like

x = [1, 2, 3]

will create a variable x containing a matrix. Meanwhile in Python, the same syntax creates an object of a type called a list. Unlike MATLAB matrices, Python lists don’t support scalar multiplication, nor can you add the elements of a list by just adding together the list variables, instead the + operator _concatenates (i.e. joins together)

x = [1, 2, 3]
print(x)
y = [1,2,3]

In Python indexing starts from 0#

As with the C language, in Python the first element in a sequence (e.g a list a = [100, 200, 300] is assigned as a[0], the second as a[1] and so on upwards. This is different from MATLAB, where the first element of a 1d matrix is a[1].

Because Python is built around indice starting from zero, most of the language is built to support this. For example, the function range(3) generates an iterator yielding 0, 1, and 2, which is just what we need to step over the elements of a sequence of length three. So where a MATLAB user might write

for i=1:3
    a[i] = i
end

a Python user could write

for i in range(3):
    a[i] = i+1

Such for loop constructions are more common in Python than in MATLAB, since Python users are often working with objects, such as the Python list, which don’t vectorise nicely.

Whitespace is part of the language#

In MATLAB constructions such as conditionals and loops are indicated using keyword such as for and end. While it is polite to add whitespace, it is not necessary for the code to work. In Python, there is no end style keyword. Instead, blocks of code have the same level of whitespace indentation. So

if x==y:
   a = 1
   b = 2
c = 3

will always change c but only change a & b if x is equal to y, while

if x==y:
   a = 1
b = 2
c = 3

changes b & c and only applies the condition to a.

Some key similarities#

Algorithms will generally work in both languages#

Since algorithms are usually based in mathematics, and since both languages provide similar data structures, then an algorithm in one language can usually be translated into another fairly directly.

Both languages are interpretted#

Both MATLAB and Python work in an interpretter, meaning that you can code live, see the results of changes instantly and update things as you go.

This is different from compiled languages such as C or C++ in which coding a program and running feel like (and are) separate stages of work.

Further Reading#

Pages from this book#

Other resources#