Python Tutorials
Hey! So you want to learn Python? Good 👍 you are at the right place! 😄
Here is what you have to know:
-
There are mainly 2 versions of Python: Python 2 and Python 3. The most known version of the former is Python 2.7, and is currently installed by default on Linux and MacOSX systems. You can try it by typing
pythonin the terminal. To exit, pushCtrl+Dor typeexit(). Note that Python 2.7 will stop to be maintained in 2020. Now, I am pretty sure that you are asking yourself "what is the difference between Python 2.7 and Python 3?". Well, to be short "Python 2.x is legacy, Python 3.x is the present and future of the language" as mentioned here. However, if you are using ROS/Gazebo, I would still recommend to use Python 2.7 for now. Pratically, these 2 versions are similar except for few details, theprintstatements where you don't have to put parenthesis in Python 2.7, and for few specific libraries. Personally, most of my code works in Python 3 and Python 2.7. -
Check first this "Tutorial: Learn Python in 10 Minutes" to get a general understanding of the language. It assumes that you already know at least one programming language such as C++, Java or Matlab. Otherwise, it will take you more than 10minutes 😜
-
Then check this one for Python 2.7 or this one for Python 3.
-
Well now, you just have to practice! 😁
About Libraries/Modules
The libraries (aka modules) that are useful for roboticists and/or machine learning engineers are:
numpyfor vectors, matrices, and tensors. Here is the tutorial.matplotlibfor plotting.- check here to see the gallery (basically it shows you what you can do with it).
- check this tutorial to learn on how to use it. The first link
Pyplot tutorialshould be enough to have a basic understanding.- if you are interested by 3D plots, check this one
- and if you want animations, look at this blog (and the webpages it links to)
scipyfor scientific and mathematical tools such as optimization, interpolation, signal processing, etc. You can find the tutorials at the following link.- If you are not satisfied with
scipy.optimize.minimize, you can have a look atcvxpy(which uses different solvers such ascvxopt) for convex optimization. For QP problems with cvxopt, you can have a look at this small tutorial. For general nonlinear optimization, you can checknlopt, oripoptwith the corresponding documentation. Most of these softwares are written in C/C++/Fortran but provides python wrappers to call the various functions.
- If you are not satisfied with
sklearnfor machine learning algorithms. You can have a look at this library with the tutorials and documentations at the following link.pandaswhen working with tables and data structures. It also provides you data analysis tools. Check here for a "10min" tutorial.
More specific libraries:
- Deep Learning (or if you want to work with Tensors, or use automatic derivation tools)
TensorFlow(akaTF) which can be found here.PyTorchat the following webpage.- Difference between
PyTorchandTF? You can have a look at this blog and this medium post. Quickly, TF is being developed by Google, is more stable, has a bigger community, deals very nicely with the hardware part, has a syntax close to numpy, and is the current tool to use if you want to develop a software product. Meanwhile, PyTorch has a better integration with Python, allows you to use dynamic graphs (instead of static ones as in TF) and experiment new ideas faster. Hovewer, it is still in its early phase and the syntax used is currently different from numpy.
- Difference between
Keras: this library is in the process of being integrated into TensorFlow. Quick description: it provides higher functionalities and is used on top of Theano or TensorFlow.Theano: this library is no longer maintained.
- Gaussian Processes
- Reinforcement Learning
- Dynamic Movement Primitives
rospy: this library allows you to use ROS (the Robot Operating System). Check here and here for the tutorials.
C/C++ libraries with Python support:
OpenCV: library for computer vision.KDL: kinematics and dynamics library.RBDL: rigid body dynamics library.
Nice tools
Here are nice tools that you should know:
Jupyter Notebook. As mentioned on their webpage, "The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text".pipwhich is tool that allows you to install Python packages/modules/libraries. For instance, to installnumpyyou would type in the terminal:pip install numpy.Anacondawhich allows you to create virtual environments and install packages likepip.Cython: this one is not really a tool, but allows you to wrap C/C++ classes and functions, and use them in Python. You could also use it to writeCythoncode which would be faster than pure Python code. Having said that, it takes quite a bit of time to learn this language, and effectively use it.
For Matlab Users
So you want to move from Matlab? Good, you are definitely at the right place 😉 Here are few webpages that could be useful for you:
- The following link explains the difference between Matlab and Python. I would also add that Python has a bigger community (because it is free), and is becoming the programming language to use for machine learning.
- Numpy for Matlab Users: Link1 and Link2.
That's all folks! 👏