a='tim'#字符串不可变
b=a
a+='e'
print(b)#tim
c=['t','i','m']#列表可变
d=c
c+=['e']
print(d)#['t', 'i', 'm', 'e']
a=[1,2,3,4,5]
print(a[2:4])
print(a[-2:])
print(a[::2])#每隔一个取一个
output:
[3, 4]
[4, 5]
[1, 3, 5]
def foo(a_int,b_list,c_list):
a_int=a_int+1
print(id(a_int))#140712577701696
b_list.append(1)
print(id(b_list))#2777595688768
c_list=c_list+[1]
print(id(c_list))#2777589470720
a_int=5
print(id(a_int))#140712577701664
b_list=[5]
print(id(b_list))#2777595688768
c_list=[5]
print(id(c_list))#2777589470336
foo(a_int,b_list,c_list)
print(a_int,b_list,c_list)
output:
5 [5, 1] [5]
foo=['c','h','e','b','w','e','c','s']
bar={'h','b','c','n'}
foo=set([x for x in foo if foo.count(x)>1])#出现次数大于1
bar=bar.intersection(foo)#求交集
print(foo)#{'c', 'e'}
print(bar)#{'c'}
yield用法参考:
python中yield的用法详解--最简单,最清晰的解释_python_mieleizhi0522的博客-CSDN博客blog.csdn.netdef fibon(n):
a=b=1
for i in range(n):
yield a
a,b=b,a+b
for item in fibon(5):
print(item)
output:
1
1
2
3
5
参数:
def foo(a,b='commit',*c,**d):
print(a,b,c,d)
foo(1,z='merge',b='clone',x=6,y=7)
#位置参数 关键字参数 剩余位置参数打包成的元组 剩余关键字参数打包成的字典
#1 clone () {'z': 'merge', 'x': 6, 'y': 7}
foo(1,2,'push',5,x='pull',y='checkout')
#1 2 ('push', 5) {'x': 'pull', 'y': 'checkout'}
类
class Parent(object):
x=1
class Child1(Parent):
def __str__(self):
return str(self.x*2)
class Child2(Parent):
#当打印一个类的实例对象时,会自动调用str方法,并返回回来一个字符串
def __str__(self):
return str(len(self))
#用print(len(对象))返回实例的“长度”
def __len__(self):
return self.x*3
c1=Child1()
c2=Child2()
#在父类中设置 x = 1 会使得类变量 x 在引用该类和其任何子类中的值为 1
print(Parent.x,Child1.x,Child2.x,c1,c2)
Child1.x=2
#如果任何它的子类重写了该值,该值仅仅在子类中被改变
print(Parent.x,Child1.x,Child2.x,c1,c2)
Parent.x=3
#如果该值在父类中被改变,这个改变会影响到任何未重写该值的子类当中的值,child1已被重写
print(Parent.x,Child1.x,Child2.x,c1,c2)
output:
1 1 1 2 3
1 2 1 4 3
3 2 3 4 9
list=['1','2','3']
print(list[5:])
output:
[]
延迟绑定
def multipliers():
return [lambda x : i*x for i in range(4)]
#只有当运行嵌套函数的时候,才会引用外部变量i,不运行的时候,并不会去找i的值
#当任何 multipliers 返回的函数被调用,此时i 的值是在它被调用时的周围作用域中查找,到那时,无论哪个返回的函数被调用,for 循环都已经完成了,i 最后的值是 3,因此,每个返回的函数 multiplies 的值都是 3。因此一个等于 2 的值被传递进以上代码,它们将返回一个值 6
print([m(2) for m in multipliers()])
def multipliers():
return [lambda x,a=i: a * x for i in range(4)]
print([m(2) for m in multipliers()])
from functools import partial
from operator import mul
def multipliers():
return[partial(mul, i) for i in range(4)]
print([m(2) for m in multipliers()])
output:
[6, 6, 6, 6]
[0, 2, 4, 6]
[0, 2, 4, 6]
def newlist(val,list=[]):
list.append(val)
return list
list1=newlist(1)
list2=newlist(2,[])
list3=newlist(3)
#新的默认列表仅仅只在函数被定义时创建一次。随后当没有被指定的列表参数调用的时候,其使用的是同一个列表
print(list1,list2,list3)
output:
[1,3] [2] [1,3]
def newlist(val,list=[]):
if not list:
list=[]
list.append(val)
return list
list1=newlist(1)
list2=newlist(2,[])
list3=newlist(3)
print(list1,list2,list3)
output:
[1] [2] [3]
装饰器
Python修饰器的函数式编程 | | 酷 壳 - CoolShellcoolshell.cnclass makeHtmlTag(object):
def __init__(self,tag):
self.tag=tag
def __call__(self,fn):
def wrapped():
return "<"+self.tag+">"+fn()+"</"+self.tag+">"
return wrapped
@makeHtmlTag('b')
@makeHtmlTag('i')
def hello():
return "hello world"
print(hello())
output:
<b><i>hello world</i></b>
异常处理
def func1():
try:
return 1
except:
return 2
finally:
print(3)
#在 try 中 raise一个异常,就立刻转入 except 中执行,在except 中遇到 return 时,
#就强制转到 finally 中执行, 在 finally 中遇到 return 时就返回,没有就返回到except的return
def func2():
try:
raise ValueError()
return 1
except:
return 2
finally:
print(3)
print(func1())#3 1
print(func2())#3 2
#在except和try中遇到return时,会锁定return的值,然后跳转到finally中
#如果finally中没有return语句,则finally执行完毕之后仍返回原return点,将之前锁定的值返回(即finally中的动作不影响返回值)
#如果finally中有return语句,则执行finally中的return语句。