- 博客(12)
- 收藏
- 关注
原创 6.2 定义管理员类: 管理员有属性(name,password), 可以创建学校、创建课程、创建老师
class Admin(object): def __init__(self): self.name = "admin" self.password = 666666 def creatSchool(self,school): self.school = school def creatCourse(self,course):...
2019-12-11 15:21:21 1427
原创 3.5 将二维结构[['a',1],['b',2]]和(('x',3),('y',4))转换成字典
list1 = [['a',1],['b',2]]t = (("x", 3), ("y", 4))d = dict(list1)d_t = dict(t)print(d)print(d_t){'a': 1, 'b': 2}{'x': 3, 'y': 4}
2019-12-04 15:30:31 246
原创 若a = dict(),令 b = a,执行b.update({"x":1}),a亦改变,为何,如何避免
这里是引用拷贝a = dict()b = ab.update({1:"x"})print(a)print(b){1: 'x'}{1: 'x'}如何避免import copya = dict()b = copy.deepcopy(a)b.update({"x":1})print(a)print(b){}{'x': 1}这里是直接复制对象的实体 而非引用...
2019-12-04 15:27:41 303
原创 3.3 将列表["mo","deng","ge"]和[1,2,3] 转换成[("mo",1),("deng",2),("ge",3)]
第一种方法list1 = ["mo","deng","ge"]list2 = [1,2,3]list3 = []for i in range(len(list1)): list3.append((list1[i],list2[i]))print(list3)第二种方法list1 = ["mo","deng","ge"]list2 = [1,2,3]list3 = list...
2019-12-04 15:15:55 119
原创 将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1 等于5的元素置为2
list1 = [3,7,0,5,1,8]for i in range(len(list1)): if list1[i]>5: list1[i] = 0 elif list1[i] == 5: list1[i] = 2 else: list1[i]=1print(list1)[1, 0, 1, 2, 1, 0]
2019-12-04 15:10:07 616
原创 3.1 将元组(1,2,3) 和集合{"four",5,6}合成一个列表
tuple1 = (1,2,3)set1 = {"four", 5, 6}list1 = list(tuple1)+list(set1)print(list1)[1, 2, 3, 'four', 5, 6]
2019-12-04 15:06:29 394
原创 2.5 交换某两个变量的值
def demo(n): def inner(x,y): x,y= y,x print(x,y) return inner@demodef func(p): print(p)while True: x = int(input("请输入你要交换的数字")) y = int(input("请输入你要交换的数字")) ...
2019-12-04 15:02:31 101
原创 2.3 计算2-100之间素数的个数,返回结果
def demo(n): def inner(x,y): sum1 = 0 for i in range(2,y+1): if i %2 == 0: continue else: print(i,end=" ") s...
2019-12-04 14:57:06 554
原创 2.2 判断某个数是否是素数,返回结果
def demo(n): def inner(x): if 0<x<=2: result = "True" elif x%2 == 0: result = "False" else: print(x,end="") result = ...
2019-12-04 14:47:08 221
原创 2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和
def get_sum(x, y): sum1 = 0 c = str(x) for n in range(0, y): a = n * c + c x = int(a) sum1 += x return sum1print(get_sum(6, 3))738
2019-12-04 14:38:15 459
原创 1.2 用递归函数来实现获取斐波拉契数列中第n个数字的值
def demo(x): if x <= 1: return x return (demo(x-1)+demo(x-2))while True: x = int(input("请输入数字")) if x<0: break else: print(demo(x))结果请输入数字821请输入数...
2019-12-04 14:24:52 651
原创 1.1 使用递归实现:计算某个数的阶乘
用递归实现计算某个数的阶乘def demo(x): if x == 1: return 1 return x*demo(x-1)print(demo(10))运行结果
2019-12-04 14:15:32 257
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人