Skip to article frontmatterSkip to article content
%%capture
'''
(C) Copyright 2020-2025 Murilo Marques Marinho (murilomarinho@ieee.org)

     This file is licensed in the terms of the
     Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
     license.

 Derivative work of:
 https://github.com/dqrobotics/learning-dqrobotics-in-matlab/tree/master/robotic_manipulators
 Contributors to this file:
     Murilo Marques Marinho (murilomarinho@ieee.org)
'''

DQ1 Python Basics

I found an issue

Thank you! Please report it at https://github.com/MarinhoLab/OpenExecutableBooksRobotics/issues

Introduction

Before we begin learning DQ Robotics, it is important to understand some basics of Python programming.

Python scripts

This is a Jupyter notebook with extension .ipynb. In general, Python scripts will have the extension .py.

A script, such as the hello_world.py, is the usual programming method of Python.

A Jupyter notebook, such as this one lesson_dq1_python_basics.ipynb, is a file in which we can write explanations together with code.

The Python kernel will execute each line of your script, in order. Python will ignore any line that stards with #. That can be used to add comments to your scripts, as follows

# This is a comment. Python will ignore this line.

Defining real numbers

It is easy to create a real number aa\in R\mathbb{R} in Python.

For instance, to create a1=5a_1 =5 and a2=67a_2 =67 , we can use the following script

# Instantiate a1 and a2
a1 = 5.0
a2 = 67.0

Note that the output of the script is not displayed.

If you want to show the values stored in each variable, you have to explicitly call print. Ideally, with f-strings.

# Showing the value of a1 but NOT a2
a1 = 5.0
a2 = 67.0

print(f"The output of a1 is {a1}")
The output of a1 is 5.0

If you want to show the ouput of the second line, you write

# Showing the value of a2 but NOT a1
a1 = 5.0
a2 = 67.0

print(f"The value of a2 is {a2}")
The value of a2 is 67.0

And if you want to show the values of both variables

# Showing the values of of a1 and a2
a1 = 5.0
a2 = 67.0

print(f"The value of a1 is {a1} and a2 is {a2}")
The value of a1 is 5.0 and a2 is 67.0

When displaying small values, Python will truncate to up to 16 digits. For instance

# Displaying small values
a1 = 0.12312312312311231231231231231213231233

print(f"a1 = {a1}")
a1 = 0.12312312312311231

Python will, by default, show only up to 16 significant digits when displaying a number. This is not a problem for the calculations, Python just truncates what is displayed, the internal representation stays as close to machine precision as possible.

Basic operations with real numbers

In your Python scripts, you can make basic operations between real numbers very easily. For example, for a1=5,a2=67,a3a_1 =5,a_2 =67,a_3 \in R\mathbb{R} , see the following operations:

Sum

a3=a1+a2a_3 =a_1 +a_2
# Get the sum of a1 and a2
a1 = 5
a2 = 67
a3 = a1 + a2
print(f"a3 = {a3}")
a3 = 72

Subtraction

a3=a1a2a_3 =a_1 -a_2
# Subtract a1 by a2
a1 = 5
a2 = 67
a3 = a1 - a2
print(f"a3 = {a3}")
a3 = -62

Multiplication

a3=a1a2a_3 =a_{1}a_{2}
# Multiply a1 by a2
a1 = 5
a2 = 67
a3 = a1 * a2
print(f"a3 = {a3}")
a3 = 335

Division

a3=a1a2a_3 =\frac{a_1 }{a_2 }
# Divide a1 by a2
a1 = 5
a2 = 67
a3 = a1 / a2
print(f"a3 = {a3}")
a3 = 0.07462686567164178

Exponentiation

a3=a12a_3 =a_1^2
# a1 squared
a1 = 5
a3 = a1**2
print(f"a3 = {a3}")
a3 = 25
a3=a13a_3 =a_1^3
# a1 cubed
a1 = 5
a3 = a1**3

Trigonometric operations with real numbers

In Python, we can easily use π and some important trigonometric functions from the module math. For example, π is defined as “pi”. We can show its first 16 significant digits as

from math import pi
# Display the value of pi
print(f"pi = {pi}")
pi = 3.141592653589793

Angles in Python are defined in radians.

Using the math module, we can use trigonometric functions as shown below. Consider the angles θ  1=π4  \theta {\;}_1 =\frac{\pi }{4}\; and θ  2=π2  \theta {\;}_2 =\frac{\pi }{2}\; .

Cosine

a1=cos(θ  1)a_1 =\cos \left(\theta {\;}_1 \right)
from math import cos
# Get the cosine of theta1
theta1 = pi/4
a1 = cos(theta1)
print(f"a1 = {a1}")
a1 = 0.7071067811865476

Sine

a2=sin(θ  2)a_2 =\sin \left(\theta {\;}_2 \right)
from math import sin
# Get the sine of theta1
theta2 = pi/2
a2 = sin(theta2)
print(f"a2 = {a2}")
a2 = 1.0

Tangent

a3=tan(θ  1+θ  2)a_3 =\tan \left(\theta {\;}_1 +\theta {\;}_2 \right)
from math import tan
# Get the tangent of theta1 + theta2
theta1 = pi/4
theta2 = pi/2
a3 = tan(theta1 + theta2)
print(f"a3 = {a3}")
a3 = -1.0000000000000002

Exponential and logarithm

In Python, the exponential and logarithm functions are also easily used from math. For instance, for a1=5,a2=67,a3a_1 =5,a_2 =67,a_3 \in R\mathbb{R} , consider the following operations.

Exponential

a3=ea1a_3 =e^{a_1 }
from math import exp
# Get the exponential
a1 = 5
a3 = exp(a1)
print(f"a3 = {a3}")
a3 = 148.4131591025766

Natural logarithm

a3=loge(a2)a_3 =\log_e \left(a_2 \right)
from math import log
# Get the logarithm
a2 = 67
a3 = log(a2)
print(f"a3 = {a3}")
a3 = 4.204692619390966

Homework

  1. Create a Python script called [python_basics_homework.py] and, in the same script, do 2-10.
  2. Define the real numbers a1=123a_1 =123 and a2=321a_2 =321
  3. Calculate and display a3=a1+a2a_3 =a_1 +a_2
  4. Calculate and display a3=a1a2a_3 =a_1 *a_2
  5. Calculate and display a3=a1a2a_3 =\frac{a_1 }{a_2 }
  6. Define the real numbers θ  1=π    12\theta {\;}_1 =\frac{\pi \;\;}{12} and θ  2=3π  \theta {\;}_2 =3\pi \;
  7. Calculate and display a3=cos(θ  1+θ  2)a_3 =\cos \left(\theta {\;}_1 +\theta {\;}_2 \right)
  8. Calculate and display a3=cos2(θ  1+θ  2)+sin2(θ  1+θ  2)a_3 =\cos^2 \left(\theta {\;}_1 +\theta {\;}_2 \right)+\sin^2 \left(\theta {\;}_1 +\theta {\;}_2 \right)
  9. Calculate and display a3=exp(a1)+loge(a2)a_3 =\exp \left(a_1 \right)+\log_e \left(a_2 \right)