What is Python
Python is an east-use program language, It is useful now, and has been implied to many open-source project, also is Go, which has been used in my favorite Docker.
Basic
Small examples
- 斐波那契数序列
a,b=0,1
while b<10:
print(b, [end=' '])
a,b=b,a+b
Attention: end=’ ’ can replace \n with ’ ‘
Key Word
if - elif - else
if x<0:
x=0
print("Negative changed to zero")
elif x==0:
print("Zero")
elif x==1:
print("Single")
else:
print("More")
for
for x in array
print(x)
break, continue,loop-else
for x in array
if n%x==0: break;
else continue;
pass
一般用于提醒,类似于空语句
del
使用索引而不是值从列表中删除一个元素。与具有返回值得pop()方法不同。del语句也可以用来删除列表的一个片段或者清空整个列表
del a[index]
del a[x:y]
del a[:]
del a: 注销变量,收回空间
SP function
range & len
for i in range(begin, stop, step)
print(i)
for i in rande(len(array))
print(array[i])
list
print(range(10))
list(range(10))
append
array.append(element)
Custom function
def
def fun(parameters list)
functional module
return result
def fib(n):
result=[]
a,b=0,1
while b<n:
result.append(b)
a,b=b,a+b
return result
i=6
def f(arg=i):
print(arg)
f()
The ans is 6
默认值只被赋值一次
def f(a,L=[]):
L.append(a)
return L
print f(2)
print f(1)
print f(3)
The ans is
[2]
[2, 1]
[2, 1, 3]
You can also use none to replace the [] to avoid the accumulate ans.
Of course, you can point many parameters who has its default value.
learn of argments
1> * & **
*用来传递任意个无名字参数,这些参数会以一个元组的形式访问
**用来传递任意个有名字的参数,这些参数用字典来访问
(*name必须出现在**name之前)
changeable parameters list
We use *name to include this point
Also, **name can distinguish the parameters.
Lambda
fun = lambda [parameter]:ans
print(fun(argments))
def make_incrementor(n):
return lambda x:x+n
>>fun = make_incrementor(42) [fun=x+42]
>>fun(2)
>>44
Data Structure
list
list.append(x): 在列表的末尾添加一个元素
list.extend(L): 在列表的末尾添加一个指定列表的所有元素
list.insert(i,x): 在指定位置插入一个元素
list.remove(x): 删除列表中值为x的第一个元素,如果不存在这样的元素则引发错误
list.pop(i): 删除列表中指定位置元素并返回它(指元素值)。如果省略索引,a.pop()会删除并返回列表中的最后一个元素
list.index(x): 返回列表中值为x的第一个元素的索引,如果不存在这样的元素则引发错误
list.count(x): 返回列表中元素x出现的次数
list.sort(): 对列表中的元素进行排序
list.reverse(): 反转列表中的元素
用列表实现堆栈
要想在堆栈顶部添加一个元素,可以使用append()方法;要想要返回堆栈顶部的元素,可以使用不指定索引参数的pop()方法
用列表实现队列
想要在对列尾部添加一个元素,可以使用append()方法;想要返回对列首部的元素,可以使用指定参数为0的pop()方法
简单的列表推导式
[x*y for x in vec1 for y in vec2]
[vec1[i]*vec2[i] for i in range(len(vec1))]
元组
u = x, y, z: 合并
x, y, z = u: 分解
set [no repeat elements]
set = {x, y, z, ...}
set2 = set({x, y, z, ...})
- | & ^ operations
a={x for x in 'abracadabra' if x not in 'abc'}
key:value
Deal Case
String
Class
This is a big top, I learn it because of necessary to modify gem5.
definition
class<__class_name>
<code>
__private_attrs // private statement, self.__private_attrs
def fun // it should include argument-self
__private_method // private fun, self.__private_method
__init__ 构造函数,在生成对象时调用
__del__ 析构函数,释放对象时使用
__repr__ 打印,转换
__setitem__按照索引赋值
__getitem__按照索引获取值
__len__获得长度
__cmp__比较运算
__call__函数调用
__add__加运算
__sub__减运算
__mul__乘运算
__div__除运算
__mod__求余运算
Example:
#类定义
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s is speaking: I am %d years old" %(self.name,self.age))
p = people('tom',10,30)
p.speak()
inherit
class<__class_name>(__parent_class)
<code>
Example:
#单继承示例
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#调用父类的构函
people.__init__(self,n,a,w)
self.grade = g
#覆写父类的方法
def speak(self):
print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))
s = student('ken',20,60,3)
s.speak()