python进阶

太过爱你忘了你带给我的痛 2021-11-04 15:18 528阅读 0赞

python进阶

异常

捕捉异常

  1. s1 = 'hello'
  2. try:
  3. int(s1)
  4. except IndexError as e:
  5. print(e)
  6. except KeyError as e:
  7. print(e)
  8. except ValueError as e:
  9. print(e)
  10. #except Exception as e:
  11. # print(e)
  12. else:
  13. print('try内代码块没有异常则执行我')
  14. finally:
  15. print('无论异常与否,都会执行该模块,通常是进行清理工作')

抛出异常raise

  1. try:
  2. a = 10
  3. b = 0
  4. # print(a/b)
  5. except Exception as e:
  6. print(e)
  7. raise e
  8. else:
  9. print("ok")
  10. finally:
  11. print("end")

当raise异常,程序中断

str容易遗忘,重点内置方法

1.按索引取值

2.切片(顾头不顾尾,步长)

  1. # 索引切片
  2. msg = 'hello nick'
  3. # 0123456789 # 索引序号
  4. print(f'切片: {msg[0:9:1]}')
  5. print(f'切片: {msg[0:9:2]}')
  6. print(f'切片: {msg[0:9:-1]}')
  7. print(f'切片: {msg[-1:-10:1]}')
  8. print(f'切片: {msg[-1:-10:-1]}')
  9. print(f'切片: {msg[-1:-10:-2]}')
  10. # 了解,步长为正从左到右;步长为负从右到左
  11. print('\n**了解知识点**')
  12. print(f'切片所有: {msg[:]}')
  13. print(f'反转所有: {msg[::-1]}')
  14. print(f'切片-5--2: {msg[-5:-2:1]}')
  15. print(f'切片-2--5: {msg[-2:-5:-1]}')

3.长度len(str)

4.成员运算in和not in

5.移出空白strip(str)

6.切分.split

7.遍历

  1. msg = 'hello nick'
  2. for i in msg:
  3. print(i)

h
e
l
l
o
n
i
c
k

lstrip&rstrip 去除左右两边指定字符

  1. # str之lstrip()和rstrip()
  2. name = '&&nick&&'
  3. print(f"nick.lstrip('&'): {name.lstrip('&')}")
  4. print(f"nick.rstrip('&'): {name.rstrip('&')}")

nick.lstrip(‘&’): nick&&
nick.rstrip(‘&’): &&nick

lower&upper

startswith&endswith 是否又指定字符开头或者结尾,区分大小写

rsplit 从右开始切割

join 字符串拼接,数字不能与字符串拼接

replace

isdigit 是否可以转化成整数

列表的内置方法(list)[]

1.按索引取值(正向取值+反向取值),即可存也可以取

2.切片

3.长度len

4.成员运算in和not in

5.追加append

6.删除del

7.循环

需要掌握

1.insert

根据索引插入,在之前插入

2.pop

list之pop(),pop()默认删除最后一个元素,可按索引删除,并返回删除字符的值

name_list.remove(‘nick’): None
name_list: [‘jason’, ‘tank’, ‘sean’]

3.remove

根据值删除,删除返回none

name_list.remove(‘nick’): None
name_list: [‘jason’, ‘tank’, ‘sean’]

4.count

name_list.count(‘nick’): 1

5.index

name_list.index(‘nick’): 0

6.clear

清空list

7.copy

  1. list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
  2. list3=list1
  3. list2 = list1.copy()
  4. print ("list2 列表: ", list2)
  5. print(id(list1))
  6. print(id(list2))
  7. print(id(list3))

list2 列表: [‘Google’, ‘Runoob’, ‘Taobao’, ‘Baidu’]
2099324874376
2099324896456
2099324874376

8.extend

  1. name_list = ['nick', 'jason', 'tank', 'sean']
  2. print(id(name_list))
  3. name_list2 = ['nick handsome']
  4. name_list.extend(name_list2)
  5. print(name_list)
  6. print(id(name_list))

2246807243080
[‘nick’, ‘jason’, ‘tank’, ‘sean’, ‘nick handsome’]
2246807243080

9.reverse

list反一下

10sort

元组(tuple)()

元组是不可变的列表,即元组的值不可更改,因此元组一般只用于只存不取的需求。也因此元组可以被列表取代掉,所以元组相比较列表使用的很少。元组相比较列表的优点为:列表的值修改后,列表的结构将会发生改变,而元组只需要存储,因此列表在某种程度上而言需要占用更多的内存。但是目前工业上内存已经不是问题了,所以工业上元组一般不会使用。

字典的内置方法(dict){}

按key存取值:可存可取

长度len

成员运算in和not in

只能判断有没有key

删除del

del dic[‘a’]

dic.pop(‘a’) # 指定元素删除

键keys()、值values()、键值对items()

  1. dic = {'a': 1, 'b': 2}
  2. print(f"dic.keys(): {dic.keys()}")
  3. print(f"dic.values(): {dic.values()}")
  4. print(f"dic.items(): {dic.items()}")

dic.keys(): dict_keys([‘a’, ‘b’])
dic.values(): dict_values([1, 2])
dic.items(): dict_items([(‘a’, 1), (‘b’, 2)])

循环

  1. dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  2. list=[[1,2,5,6],[3,4,6,0]]
  3. for k,*_,c in list:
  4. print(k,*_,c)
  5. for k, v in dic.items(): # items可以换成keys()、values()
  6. print(k, v)

循环原理就是解压缩

get

dic.get(‘a’): 1
dic.get(‘c’): None

update

可以用于添加

fromkeys

  1. dic = dict.fromkeys(['name', 'age', 'sex'], None)
  2. print(f"dic: {dic}")

dic: {‘name’: None, ‘age’: None, ‘sex’: None}

setdefault

有指定key不会改变值;无指定key则改变值

set{}

长度len

成员运算in和not in

|并集、union

&交集、intersection

-差集、difference

^对称差集、symmetric_difference

==

父集:>、>= 、issuperset

子集:<、<= 、issubse

add

remove

difference_update

discard

isdisjoin

Python深浅拷贝

拷贝/浅拷贝/深拷贝都是针对可变类型数据而言的

转载于:https://www.cnblogs.com/zx125/p/11283322.html

发表评论

表情:
评论列表 (有 0 条评论,528人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python

    大家好,我是易安!今天我们继续Python的学习,内容稍微有些多,不过我会尽可能举一些例子让你理解。 对象比较与拷贝 在前面的学习中,我们其实已经接触到了很多 Pyth