Python Tutorial
Disclaimer: Python is an extensive programming (or scripting) language. It is neither easy not effective to teach Python in a single webpage. The main purpose of this page is to create a shortcut to the commands that we use a lot during this course. We also provide links to nice Python tutorials if you are interested to improve your skills in Python programming.
Please look at and admire this website: https://docs.python.org
Running Python scripts
Let’s start a simple Python script:
print('Hello world')
Through Terminal
You can write it directly in your terminal:
- Open a terminal: Ctrl + Alt + T
- Type:
python3
- Paste the code above.
Or you can directly run Python scripts via terminal.
- Open a terminal: Ctrl + Alt + T
- Create a Python file:
gedit my_first_python.py
- Save and close.
- Run:
python3 my_first_python.py
Through VSCode
You need an IDE for more complicated code. We use VSCode.
- Open Visual Studio Code
- File > New File > Python file
- Paste the code above
- Run (play button rigth above).
Functions
Simple function:
def main():
print('Hello world')
if __name__ == '__main__':
main()
Call a function inside another function:
def main():
my_print_function()
def my_print_function():
print('Hello world')
if __name__ == '__main__':
main()
Rarely you define a function inside a function (nested function):
def outerFunction(text):
text = text
def innerFunction():
print(text)
innerFunction()
if __name__ == '__main__':
outerFunction('Hello world')
Imports
Imports are libraries.
TODO:
- Simple print
- import and from
- functions
- import from other file
- os, join path: https://www.geeksforgeeks.org/python-os-path-join-method/
- This is particularly important for servo.py and XX.launch.py files.
- glob: https://docs.python.org/3/library/glob.html
- https://www.geeksforgeeks.org/how-to-use-glob-function-to-find-files-recursively-in-python/
- classes
- self
- init constructor
- methods
- Just mention static and class methods
- parenting
- super()
- Simple 3 exercises.
Useful materials:
Python books Websites Youtube videos