python except用法和作用_python笔试题

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.net
a2629ce955d136f1b278111c66fb7716.png
def 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]
理解Python闭包与延迟绑定_python_xie_0723的博客-CSDN博客​blog.csdn.net
e974afa81eaaf729a147ebcdad68aef2.png
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修饰器的函数式编程 | | 酷 壳 - CoolShell​coolshell.cn
f216d1ce88f2cc4340b5633edfc25992.png
class 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语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值