网站首页 > 技术文章 正文

Python 是面向对象的编程语言,所以Python del 的主要目标是删除 Python 编程语言中的对象。这里的对象可以是变量、列表等。

「del 的语法格式:」
?
del obj_name
?
del 是 Python 的关键字。
obj_name 可以是变量、用户定义的对象、列表、列表中的元素、字典等。
Python del 语句不返回任何内容。
示例 1:删除变量
>>> n = 123
>>> n
123
>>> del n
>>> n
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
NameError: name 'n' is not defined
因为 del 运算符删除了变量 n,内存中没有名为 n 的变量。返回 NameError: name 'n' is not defined。
示例 2:删除列表元素
>>> m = [1, 2, 3, 4, 5, 6]
>>> del m[0]
>>> m
[2, 3, 4, 5, 6]
>>> del m[-1]
>>> m
[2, 3, 4, 5]
>>> del m[1:3]
>>> m
[2, 5]
使用 del 删除了列表的一个或多个元素。注意, del 语句用于通过索引(而不是值)从列表中删除元素。
示例 3:删除用户定义的对象
可以用 “a” 或 “A” 作为初始化变量,可以使用 chr 和 ord 函数,不断递增变量的 ASCII 值。循环 26 次,获取 26 个英文字母。
class info:
name = "wang"
age = 18
p = info()
print("Name: ", p.name)
print("Age: ", p.age)
del p
print("Name: ", p.name)
print("Age: ", p.age)
# 输出:
Name: wang
Age: 18
Traceback (most recent call last):
File "C:\info.py", line 9, in <module>
print("Name: ", p.name)
NameError: name 'p' is not defined
示例 4:删除字典中的键值
>>> dict1 = {'a':1, 'b':2, 'c':3}
>>> del dict1['b']
>>> dict1
{'a': 1, 'c': 3}
使用 del 的注意事项
不能使用 del 删除元组和字符串中的元素,因为元组和字符串是不可变的,创建后无法更改的对象。
>>> m = "Python"
>>> del m[0]
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
TypeError: 'str' object doesn't support item deletion
>>> n = (1,2,3)
>>> del n[0]
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
del,remove,pop 之间的区别
- remove() 是列表的方法,用于删除指定的值,不用关心元素在列表中的位置。
- pop() 是列表的方法,用于删除指定位置元素。如果为 pop() 指定了参数,将删除对应位置的元素。如果不指定参数,则默认参数 -1(即默认删除末位元素)。pop() 删除指定位置元素后并返回该元素。
>>> m = [1, 2, 3, 4, 5, 6]
>>> m.remove(6)
>>> m
[1, 2, 3, 4, 5]
>>> m = [1, 2, 3, 4, 5, 6]
>>> m.pop()
6
>>> m
[1, 2, 3, 4, 5]
>>> m.pop(-1)
5
>>> m
[1, 2, 3, 4]
>>> m.pop(0)
1
>>> m
[2, 3, 4]
「文章创作不易,如果您喜欢这篇文章,请关注、点赞并分享给朋友。如有意见和建议,请在评论中反馈!」
猜你喜欢
- 2024-12-26 Python 中的==操作符 和 is关键字
- 2024-12-26 揭秘 Python 中的 with 关键字
- 2024-12-26 简单学Python——关键字7——if、elif、else
- 2024-12-26 python关键字assert(断言)实例
- 2024-12-26 Python中yield关键字有什么作用
- 2024-12-26 【python编程】__all__关键字的作用
- 2024-12-26 简单学Python——关键字10——from
- 2024-12-26 Python中的yield关键字——生成器函数的用法
- 2024-12-26 学会 Python yield 关键词,此文就够了
- 2024-12-26 探索Python中的关键字
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)