TA: Li Ang ([email protected])
- This tutorial is consists of basic python guideline, python tutorial for robot stuffs, and Advanced programming skills
Recommended Reading:
Liao's Python Tutorial (Example codes)
If you are familar with python, just jump to part 2
# Basically, you have three way to get a python environment.
# 1.Using IDE, such as Pycharm(get community version via your CUHK e-mail address).
# 2.Using interpreter with an (powerful) text editor such as VS code.
# 3.Using Anaconda (Not Deprecated in our course for confilcts may caused when you using ROS in the Lab computer).
pip install numpy
# Install a code package via pip.
# In this case, you installed numpy for your environment.
# Similarily, you can use pip uninstall xxx to remove a package.
c = "#"+"".join([random.choice('0123456789ABCDEF') for x in range(6)])
print(c)
You should understand codes below
a = 1
b = '1'
print(a == b, type(a), type(b), type(a == b))
# the result is"False, <class 'int'>, <class 'str'>, <class 'bool'>"
# if you have problem with this, please read note_1
def fun1():
print("the first funcntion")
for i in range(3):
print('iteration', i)
if i == 1:
print("print extra stuffs")
if i == 3:
print("it will never happens because range stops in 3")
def fun2(sth):
print('the second funcion, input args is', sth)
def fun3(*args, **kargs):
# you don't need to write all args
# you can use *args and **kargs to hide them
print(args)
if 'kw' in kargs:
print("has key arg kw:",kargs['kw'])
else:
print("other kw:", kargs)
def fun4():
return 4
def fun5(functor):
print(functor())
def fun():
fun1()
fun2('abcd')
fun3(1, 2, 3, 4, 5, kw=6)
#in this case, the input is a number, becase the fun4 is called before being a argument
fun3(fun4())
# in this case, the input is a function
fun5(fun4)
fun() # call function here
# the result is following:
'''
the first funcntion
iteration 0
iteration 1
print extra stuffs
iteration 2
the second funcion, input args is abcd
(1, 2, 3, 4, 5)
has key arg kw: 6
(4,)
other kw: {}
4
'''
class BaseClass():
def __init__(self):
self.basic_sth = 0
def print_self(self):
print(type(self) ,'Functions will be inherited by default')
def __lalala(self):
print(type(self), 'it could not be inherited because you write __ in the functin name')
@staticmethod
def sfunc():
print('this function do not needs self, because it belongs to Class, not object')
print('call it by BasicClass.sfunc()')
class A(BaseClass):
def __init__(self):
super().__init__() # called __init__ of super class
self.a_sth = 1
class B(BaseClass):
def __init__(self):
# if you do not write super().__init__() here, you can not use basic_sth from the baseclass
self.b_sth = 2
c = BaseClass()
a = A()
b = B()
c.print_self()
a.print_self()
b.print_self()
BaseClass.sfunc()
# or you can use
a.sfunc()
b.sfunc()
c.sfunc()
print(c.basic_sth)
print(a.basic_sth, a.a_sth)
print( b.b_sth)
# try to imagine the output
# and run toy_script/try_q3.py
I collected some example script of following works, wish it can recall your interests.
---