Scientific Python therefore requires having a firm grasp of Python itself. We suggest reading through the official tutorial, doing an online tutorial on exercism, or using any of the countless resources that exist online or in print.
Learning a new language can be challenging, but Python is fun—so keep trying and hang in there! The community is there to help you along the way.
So let’s cover some basics.
Python is an interpreted language: that means that it reads a text file with instructions and executes those one by one.
The easiest way to create a text file is in a text editor, like Spyder or VSCode.
We can do that right now. Let’s create a file called hello.py
:
print("Hello world")
And then run it:
python hello.py
hello
That’s it, your first Python program!
You can also play around with Python code interactively in IPython:
[launch IPython and run:]
In [1]: def fibonacci(n):
...: a, b = 0, 1
...: for i in range(0, n):
...: a, b = b, a + b
...: return a
...:
In [2]: fibonacci(10)
Out[2]: 55
Another ways to play with Python code is in Jupyter Lab. This is an interactive web application for typing in and executing Python code. Let me show you how to do a simple plot in Jupyter:
[Open Jupyter Lab; create notebook; import matplotlib as plt; plt.plot([1, 2, 3])]
You can head over to https://try.jupyter.org to test it out.
What distinguishes most scientific codes from general ones is that they operate on collections of numbers. These are often represented as NumPy arrays—they are fast, and they have convenient syntax.
Let’s generate 1000 random numbers and square them:
[In IPython]
import numpy as np
import matplotlib.pyplot as plt
# Generate 1000 random numbers, store in x
x = np.random.random(size=1000)
# Square them and store in y
y = x**2
# Plot the results!
plt.plot(x, y)
plt.show()
We’ll post a list of links below the video where you can learn more:
By far the best way to learn, however, is to start coding!
The first thing to do when stuck is to read the documentation. Note that almost all libraries ship with documentation right at your fingertips!
[illustrate how to look up the docstring for np.linspace
]
If you are still stuck, join the community forum at https://discuss.scientific-python.org or reach out to the relevant package on its mailing list.
Good luck!