鱼C论坛

 找回密码
 立即注册
查看: 2234|回复: 0

[技术交流] Python del 语句

[复制链接]
发表于 2020-3-22 11:02:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python del 语句


语法:

  1. del sth
复制代码


可以删除掉一个变量,一个列表,一个列表中的键。。。等等等等。

实例:

  1. >>> a = 5
  2. >>> a
  3. 5
  4. >>> del a
  5. >>> a
  6. Traceback (most recent call last):
  7.   File "<pyshell#17>", line 1, in <module>
  8.     a
  9. NameError: name 'a' is not defined
  10. >>> a = [1,2,3,4,5]
  11. >>> del a[4]
  12. >>> a
  13. [1, 2, 3, 4]
  14. >>> del a[0]
  15. >>> a
  16. [2, 3, 4]
  17. >>> del a
  18. >>> a
  19. Traceback (most recent call last):
  20.   File "<pyshell#24>", line 1, in <module>
  21.     a
  22. NameError: name 'a' is not defined
  23. >>> a = {0: '0', 1: '1'}
  24. >>> del a[0]
  25. >>> a
  26. {1: '1'}
  27. >>> del a
  28. >>> a
  29. Traceback (most recent call last):
  30.   File "<pyshell#29>", line 1, in <module>
  31.     a
  32. NameError: name 'a' is not defined
  33. >>> def func():
  34.         print("FUNC!")

  35.        
  36. >>> func()
  37. FUNC!
  38. >>> del func
  39. >>> func()
  40. Traceback (most recent call last):
  41.   File "<pyshell#35>", line 1, in <module>
  42.     func()
  43. NameError: name 'func' is not defined
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-20 02:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表