- 博客(14)
- 资源 (1)
- 收藏
- 关注
原创 Learn and Record14
# -*- coding: utf-8 -*-import unittestclass Student(object): def __init__(self, name, score): self.name = name self.score = score def get_grade(self): if self.score >= 80 and self.score <= 100: return 'A'
2020-11-11 22:09:20 169
原创 Learn and Record13
完了,一周没更了,差点忘记自己还有这个博客装了Anaconda,开始学者用Python语言做Abaqus的二次开发,也没感觉有多难的地方,倒是面向对象编程的一些概念给我整的一愣一愣的,合着我之前苦思冥想的那些解决问题的过程还不算什么核心部分,真是让人头秃啊。...
2020-11-02 19:50:39 189
原创 Learn and Record12
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum在这个例子中,我们在函数lazy_sum中又定义了函数sum,并且,内部函数sum可以引用外部函数lazy_sum的参数和局部变量,当lazy_sum返回函数sum时,相关参数和变量都保存在返回的函数中,这种称为“闭包(Closure)”的程序结构
2020-10-25 21:57:43 197
原创 Learn and Record11
def _odd_iter(): n=1 while True: n=n+2 yield ndef _not_divisible(n): return lambda x:x%n>0def primes(): yield 2 it=_odd_iter() while True: n=next(it) yield n it=filter(_not_divisible(n),i
2020-10-24 21:46:36 107
原创 Learn and Record10
def is_odd(n): return n%2==1print('list(filter(is_odd,[1,2,3,4,5,6,56,67]))=',list(filter(is_odd,[1,2,3,4,5,6,56,67])))#找奇数def not_empty(s): return s and s.strip()print("list(filter(not_empty,['A','','B',None,'C',' ']))=",list(filter(not_empty
2020-10-23 21:46:19 84
原创 Learn and Record9
def normalize(name): name=name[0].upper()+name[1:].lower() return nameL1 = ['adam', 'LISA', 'barT']L2 = list(map(normalize, L1))print(L2)#人名from functools import reducedef prod(L): def m(x,y): return x*y return reduce(m,L)#连乘
2020-10-22 21:22:30 117 1
原创 Learn and Record8
找大小已经解决了:def findMinAndMax(L): if L==[]: return None,None for i in range(len(L)): if not isinstance(L[i],(int,float)):#判断list类型 raise('TypeError') for i in range(len(L)): min=L[0] max=L[0]
2020-10-21 21:49:36 88
原创 Learn and Record7
今天和自己算总账,trim和找大小没写出来,汉诺塔的那个明明很接近了却由于不自信不敢写。`def move(n,a,b,c): if n==1: print(a,'-->',c) else: move(n-1,a,c,b) move(1,a,b,c) move(n-1,b,a,c)print(move(7,'A','B','C'))#手算我都看出来结果了,就是写的时候感觉这么写也不行那么写也不行,想那么多做什么,先写
2020-10-20 21:30:41 84
原创 Learn and Record6
函数参数的有关定义演示:def f1(a, b, c=0, *args, **kw): print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)def f2(a, b, c=0, *, d, **kw): print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)运行演示:>>> f1(1, 2)a = 1 b = 2 c = 0 arg
2020-10-19 22:53:58 62
原创 Learn and Record5
迭代和列表生成式中的多层循环,生成器中的yield,话说每次看到自己敲出正确的代码都有一种自己的论文变成了铅字的感觉,该把习题们做一做了>>> g=fib(6)>>> while True:... try:... x = next(g)... print('g:', x)... except StopIteration as e:... print('Generator return value
2020-10-17 22:43:22 73
原创 Learn and Record4
切片操作确实非常灵活,变化也很多L[:]L[:n]L[m:n]L[-m:]L[m:n:k](有点糊弄事了)今天一天都在忙着项目结题和DASP的事,话说东方所的售后是真的给力,当然这也离不开我个人的争取,礼仪到位好说话,明天搞实验,希望别来什么幺蛾子,大连理工的研究生真的可惜,研途不易...
2020-10-15 23:04:09 87
原创 Learn and Record3
pythonimport mathdef quadratic(a,b,c): if not isinstance(a+b+c,(int,float)): raise TypeError('bad operand type') elif b**2-4*a*c<0: raise TypeError('no real solver') else: x1=(-b+math.sqrt(b**2-4*a*c))/(2*a) x2
2020-10-14 22:00:54 105
原创 Learn and Record2
一个小小的一元二次方程,不该的呀import mathdef equation(a,b,c):if not isinstance(a+b+c,(int,float)):raise TypeError(‘bad operand type’)elif b**2-4ac<0:raise TypeError(‘no real solver’)else:x1=(-b+math.sqrt(b2-4ac))/2ax2=(-b-math.sqrt(b2-4ac))/2areturn x1,x2p
2020-10-13 21:54:59 170
原创 Learn and Record1
小白的第一天(Python)1.应始终坚持使用四个空格的缩进2.代码要自己亲手打出来才算数3.注意大小写4.编辑器不听使唤应该是因为没有重启以添加路径5.变量本身类型不固定为动态语言6.作为key的对象不能变,在Python中,字符串、整数等都是不可变的,因此,可以放心地作为key,而list是可变的,就不能作为key小小循环,可笑可笑(空格不见了)L = [‘Bart’, ‘Lisa’, ‘Adam’]n=0while n<3:print(‘Hello,’,L[n],’!’)
2020-10-12 21:26:54 394
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人