python小猪蹄儿
夜的第七章,打字机继续向前推向,微亮!
请写一个栈
class Stack: #初始化栈(列表) def __init__(self): self.items=[] #栈的大小 def size(self): return len(self.items) #给栈增加元素 def push(self,item): return self.items.append(item) #删除栈元素 def pop(self): return self.items.pop() #判断栈是否为空 def isempty(self): return self.items==[] #打印栈顶元素 def peek(self): return self.items[len(self.items)-1] s=Stack() print(s.isempty()) s.push(5) s.push(8) print(s.peek()) s.push(True) print(s.size()) print(s.isempty()) s.push(8.666) print(s.pop()) print(s.size())
写一个队列
# 队列 class Quene(object): #初始化队列 def __init__(self): self.__list=[] #往队列添加元素 def enquene(self,item): self.__list.append(item) #从队列删除元素 def dequene(self): return self.__list.pop(0) #判断队列是否为空 def isempty(self): return self.__list==[] #队列长度 def length(self): return len(self.__list) if __name__=="__main__": q=Quene() print(q.length()) print(q.isempty()) q.enquene(1) q.enquene(2) q.enquene(3) q.enquene(4) print(q.length()) #4 print(q.dequene()) #1 print(q.dequene()) #2 print(q.dequene()) #3 print(q.length()) #1
下边的输出结果是?
class parent(object): x=1 class child1(parent): pass class child2(parent): pass print(parent.x,child1.x,child2.x) # 1 1 1 child1.x=2 print(parent.x,child1.x,child2.x) # 1 2 1 parent.x=3 print(parent.x,child1.x,child2.x) #3 2 3
def multipliers(): return [lambda x:i*x for i in range(4)] #i=3 print([m(2) for m in [lambda x:i*x for i in range(4)]]) #[6, 6, 6, 6]