%%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://
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 in Python.
For instance, to create and , 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 , see the following operations:
Sum¶
# Get the sum of a1 and a2
a1 = 5
a2 = 67
a3 = a1 + a2
print(f"a3 = {a3}")
a3 = 72
# Subtract a1 by a2
a1 = 5
a2 = 67
a3 = a1 - a2
print(f"a3 = {a3}")
a3 = -62
# Multiply a1 by a2
a1 = 5
a2 = 67
a3 = a1 * a2
print(f"a3 = {a3}")
a3 = 335
# Divide a1 by a2
a1 = 5
a2 = 67
a3 = a1 / a2
print(f"a3 = {a3}")
a3 = 0.07462686567164178
# a1 squared
a1 = 5
a3 = a1**2
print(f"a3 = {a3}")
a3 = 25
# 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 and .
Cosine¶
from math import cos
# Get the cosine of theta1
theta1 = pi/4
a1 = cos(theta1)
print(f"a1 = {a1}")
a1 = 0.7071067811865476
from math import sin
# Get the sine of theta1
theta2 = pi/2
a2 = sin(theta2)
print(f"a2 = {a2}")
a2 = 1.0
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 , consider the following operations.
Exponential¶
from math import exp
# Get the exponential
a1 = 5
a3 = exp(a1)
print(f"a3 = {a3}")
a3 = 148.4131591025766
from math import log
# Get the logarithm
a2 = 67
a3 = log(a2)
print(f"a3 = {a3}")
a3 = 4.204692619390966
Homework¶
- Create a Python script called [python_basics_homework.py] and, in the same script, do 2-10.
- Define the real numbers and
- Calculate and display
- Calculate and display
- Calculate and display
- Define the real numbers and
- Calculate and display
- Calculate and display
- Calculate and display