This introduction is based heavily on the introduction notes found here: https://github.com/scipy-lectures/scipy-lecture-notes
I've used pretty much every programming language you can think of... MatLab, IDL, Fortran, C++, C#, Java,... But Python, for me anyway, is the best of the lot. It's flexible, fast, easy to read and portable across all sorts of machines and architectures.
Let's compare a bit more specifically against some other languages we might use:
Unlike Matlab, or R, Python does not come with a pre-bundled set of modules for scientific computing. Below are the basic building blocks that can be combined to obtain a scientific computing environment:
Make sure you now type source activate python_workshop on UNIX based systems and activate python_workshop - this may not be necassary if you have nb_conda_kernels installed.
Python is an interpreted language and there is more than one interpreter we can use:
For this course we will be using the iPython notebooks - these are great for working interactively and storing the results, as well the code and any notes we might want to make. For script development you can use a basic text editor or an IDE (or anything in between), we'll be exploring PyCharm later in the course
In [ ]:
# The obligatory hello world...
print("Hello world!")
In [ ]:
an_integer = 9
a_float = 2.5
a_string = "blah"
a_complex_number = 2.0 + 2.3j
In [ ]:
# Note that variables are case-sensitive
a = 1
A = 2
print(a, A)
In [ ]:
# And can be dynamically reassigned
an_integer = 7
print(an_integer)
In [ ]:
print(an_integer * 3)
print(a_float + 3)
print(an_integer / 4)
print(a_complex_number ** 2)
print(a_string * 2)
In [ ]:
def say_hello(name):
print("Hello {}!".format(name))
In [ ]:
say_hello("world")
In [ ]:
def say_hello(name, indent=0):
padding = " "*indent
print(padding + "Hello {}!".format(name))
In [ ]:
say_hello("Duncan", indent=4)
In [ ]:
say_hello(3, 4.2)
In [ ]:
# This is a comment
pass # This is also a comment!
In [ ]:
def nicely_documented_function(oranges):
"""
This function takes a list of oranges and returns orange juice
:param list oranges: The oranges to be juiced
"""
pass
In [ ]:
help(nicely_documented_function)
In [ ]:
assorted_stuff = ["dog", 3, 2.34, "cat"]
In [ ]:
assorted_stuff[0]
In [ ]:
assorted_stuff[1:]
In [ ]:
assorted_stuff[::-1]
In [ ]:
for item in assorted_stuff:
print(item)
print("Done.")
Sometimes it's useful to also have a loop counter, the enumerate function can do this
In [ ]:
for n, item in enumerate(assorted_stuff):
print(item, n)
print("Done.")
In [ ]:
some_integers = [i for i in range(10)]
some_integers
In [ ]:
# A collection of key-value pairs
simple_lookup = {"red": 5, "green": 12, "blue": [0.5, 45.8]}
In [ ]:
print(simple_lookup["red"])
In [ ]:
print(simple_lookup["blue"][0])
In [ ]:
print(simple_lookup["yellow"])
In [ ]:
print(simple_lookup.get("yellow", 0))
In [ ]:
simple_lookup["orange"] = 15
print(simple_lookup)
In [ ]:
simple_lookup[4] = "brown"
print(simple_lookup[4])
There are other containers too, but we won't cover them here. Most useful are probably sets and named tuples.
In [ ]:
# Simple conditional statements
variable = 0.95
if variable > 0.9:
print("On")
In [ ]:
if variable > 0.9:
print("On")
elif variable < 0.1:
print("Off")
elif 0.4 < variable < 0.6:
print("Intermediate")
else:
print("Unknown")
In [ ]:
# Python will 'short-cut' logic statements
if "orange" in simple_lookup and simple_lookup["orange"] > 10:
print(simple_lookup["orange"])
In [ ]:
switch = "On" if abs(variable-1.0) < 0.1 else "Off"
print(switch)