python知识精华:嵩天微专业笔记

异常处理

1 基本用法

应对所有情况

try:
    1/0
except:
    print('某原因异常')

应对特定异常情况

try:
    1/0
except ZeroDivisionError:
    print('除数不应为0')
    
try:
    1/0
except ZeroDivisionError as q:
    print(q)

混合使用

def foo(a):
    try :
        100/a
    except ZeroDivisionError :
        print('除数不应为零')
    except :
        print('未知错误')

foo(0)
foo('ry1')
除数不应为零
未知错误

2高级用法

try :
    print(1/a)
except:
    print('except')
else:
    print('else')
finally:
    print('finally')

注:
except可有多组;
try中未有异常时,执行else;
finally一定执行

try-except-else-finally遇到函数时

def f(a):
    try:
        print(1 / a)
        return 1/a
    except:
        print('except')
        return 'except'
    else :
        print('else')
        return 'else'
    finally:
        print('finally')
        return 'finally'

print(f(0))
print(f(2))
except
finally
finally
0.5
finally
finally

注:
无论return在哪,finally一定执行;
try中有return,else不执行;
finally中的return会覆盖之前所有的return

逻辑运算符

and or not
and \Leftrightarrow exp1 if not exp1 else exp2
or \Leftrightarrow exp1 if exp1 else exp2
and 和 or不一定返回True 或 False,而是得到最后一个被计算的表达式,
但not一定得到True 或 False。
在这里插入图片描述

循环的高级用法

for in:
    ***
else:
    ***

while :
    ***
else:
    ***    

else扩展:玄幻完成的奖励( 循环没有被break退出时执行)

for c in 'python':
    
    if c=='y':
        continue
    print(c,end=',')
    
else:    
    print('正常退出')
    
p,t,h,o,n,正常退出
for c in 'python':
    
    if c=='y':
        break
    print(c,end=',')
    
else:    
    print('正常退出')
    
p,

函数参数

1.位置传递
2.名称传递

def f(a,b):
f(1,2)
f(b=2,a=1)    

3.可选参数传递(可选参数必须放在最后)
def f(a,b=1) ✔
def f(b=1,a) ❌
4.可变参数:函数可以接收不确定总数的参数变量
* 接收元组 f(n, *a)
** 接受字典 f(n, **a)
在这里插入图片描述
在这里插入图片描述

局部变量和全局变量

1.局部变量可与全局变量重名
2.局部变量函数调用时生效,函数运算结束后失效
3.局部变量为组合数据类型(列表,字典,集合)且未创建,等同于全局变量

a = 1
def f():
    global a #此时不能赋值,即global a=9 错误
    a +=10   
ls =['a','b']
def f():
    ls.append('c')
    
f() 
print(ls)
['a', 'b', 'c']

面向对象OOP

计算三款产品的原始售价和实际售价之和

class Product:
    def __init__(self,name):
        self.name = name
        self.label = 0
        self.real = 0

a = Product('电脑')
b = Product('打印机')
c = Product('投影仪')
a.label,a.real = 10000, 8000
b.label,b.real = 2000, 1000
c.label,c.real = 1500, 900

s1,s2 = 0, 0
for i in (a,b,c):
    s1 += i.label
    s2 += i.real

print(s1,s2)

类构建

类名使用大写字母组合,如ClassName BasicAuto
在这里插入图片描述
类描述通过 类名.__doc__来访问


class DemoClass:
    "This is a demo for Python class"
    pass

print(DemoClass.__doc__)

类对象:类定义完后,默认生成一个类对象;类对象是type类的实例,表达为type类型


class DemoClass:
    "This is a demo for Python class"
    print('hello')

print(DemoClass.__doc__)
print(type(DemoClass))
hello
This is a demo for Python class
<class 'type'>

类定义即执行,不需要调用

class DemoClass:
    "This is a demo for Python class"
    print('hello')

hello

实例对象不同于类对象,实例对象是python类的最常用使用方式


class DemoClass:
    "This is a demo for Python class"
    print('hello')

a = DemoClass()

print(type(DemoClass))
print(type(a))
hello
<class 'type'>
<class '__main__.DemoClass'>

构造函数:init() l类实例化时使用的函数

class DemoClass:
    def __init__(self,name):
        print(name)

a = DemoClass('老王')
b = DemoClass('老李')

猜你喜欢

转载自blog.csdn.net/baidu_41867252/article/details/87871688
今日推荐