Marcos Duarte, Renato Naville Watanabe
Laboratory of Biomechanics and Motor Control
Federal University of ABC, Brazil
from IPython.display import Image
Image(data='http://imgs.xkcd.com/comics/python.png')
The Python programming language with its ecosystem for scientific programming has features, maturity, and a community of developers and users that makes it the ideal environment for the scientific community.
This talk will show some of these features and usage examples.
Traditionally, science has been divided into experimental and theoretical disciplines, but nowadays computing plays an important role in science. Scientific computation is sometimes related to theory, and at other times to experimental work. Hence, it is often seen as a new third branch of science.
Figure from J.R. Johansson.
Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs [python.org].
import math
, we will have all the functions and statements defined in this module in the namespace 'math.
', for example, math.pi
is the $\pi$ constant and math.cos()
, the cosine function.Python is also the name of the software with the most-widely used implementation of the language (maintained by the Python Software Foundation).
This implementation is written mostly in the C programming language and it is nicknamed CPython.
So, the following phrase is correct: download Python (the software) to program in Python (the language) because Python (both) is great!
The origin of the name for the Python language in fact is not because of the big snake, the author of the Python language, Guido van Rossum, named the language after Monty Python, a famous British comedy group in the 70's.
By coincidence, the Monty Python group was also interested in human movement science:
from IPython.display import YouTubeVideo
YouTubeVideo('eCLp7zodUiI', width=480, height=360, rel=0)
Python is not the best programming language for all needs and for all people. There is no such language.
Now, if you are doing scientific computing, chances are that Python is perfect for you because (and might also be perfect for lots of other needs):
from IPython.display import IFrame
IFrame('https://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-us-universities/fulltext',
width=800, height=600)
The Jupyter Notebook App is a server-client application that allows editing and running notebook documents via a web browser. The Jupyter Notebook App can be executed on a local desktop requiring no Internet access (as described in this document) or installed on a remote server and accessed through the Internet.
Notebook documents (or “notebooks”, all lower case) are documents produced by the Jupyter Notebook App which contain both computer code (e.g. python) and rich text elements (paragraph, equations, figures, links, etc...). Notebook documents are both human-readable documents containing the analysis description and the results (figures, tables, etc..) as well as executable documents which can be run to perform data analysis.
The easy way
The easiest way to get Python and the most popular packages for scientific programming is to install them with a Python distribution such as Anaconda or Miniconda.
In fact, you don't even need to install Python in your computer, you can run Python for scientific programming in the cloud using python.org, Google Colaboratory, or repl.it.
The hard way
You can download Python and all individual packages you need and install them one by one. In general, it's not that difficult, but it can become challenging and painful for certain big packages heavily dependent on math, image visualization, and your operating system (i.e., Microsoft Windows).
Go to the Anaconda website and download the appropriate version for your computer (but download Anaconda3! for Python 3.x). The file is big (about 500 MB).
Follow the installation steps described in the Anaconda documentatione for your operational system.
import sys
sys.version
'3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]'
More information can be obtained using the watermark extension:
try:
from watermark import watermark
except ImportError:
%pip install -q watermark
%load_ext watermark
%watermark -u -t -d -m -v --iversions
Last updated: 2023-09-19 16:06:59 Python implementation: CPython Python version : 3.10.12 IPython version : 7.34.0 Compiler : GCC 11.4.0 OS : Linux Release : 5.15.109+ Machine : x86_64 Processor : x86_64 CPU cores : 2 Architecture: 64bit sys: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
You might want an Integrated Development Environment (IDE) for programming in Python.
See 10 Best Python IDE & Code Editors for possible IDEs.
There is a lot of good material in the Internet about Python for scientific computing, some of them are:
Let's run stuff from:
import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!