【python3】2. 数据类型

本系列摘自莫烦python

1 list列表

  • 列表用的【】
files = ["f1.txt", "f2.txt", "f3.txt", "f4.txt", "f5.txt"]
print("files[:3] ", files[:3])
print("files[2:4] ", files[2:4])
print("files[-3:] ", files[-3:])

files[:3]  ['f1.txt', 'f2.txt', 'f3.txt']
files[2:4]  ['f3.txt', 'f4.txt']
files[-3:]  ['f3.txt', 'f4.txt', 'f5.txt']
  • 在列表中,你可以存放不同类型的元素,字符,数字,甚至列表里还能有列表。
l = [1, "file", ["2", 3.2]]
print(l)
l[2][0] = "new string"
print(l)
[1, 'file', ['2', 3.2]]
[1, 'file', ['new string', 3.2]]
  • for循环
files = ["f1.txt", "f2.txt", "f3.txt", "f4.txt", "f5.txt"]
for f in files:
    if f == "f3.txt":
        print("I got f3.txt")

2 Dict 字典

有标签,{}来括起来,标签不能重复

files = {"ID": 111, "passport": "my passport", "books": [1,2,3]}
print(files)
print(files["books"])
{'ID': 111, 'passport': 'my passport', 'books': [1, 2, 3]}
[1, 2, 3]
  • 注意哦,在字典中的元素不像列表,字典元素是没有顺序的,只能通过ID来查找,不能通过下标

  • for循环取值
    字典也能和 for 进行简化的应用。我们只需要调用 dict.items(), dict.keys(), dict.values() 分别取字典的某些部分就能简化 for 循环了

files = {"ID": 111, "passport": "my passport", "books": [1,2,3]}
for key in files.keys():
    print("key:", key)

for value in files.values():
    print("value:", value)

for key, value in files.items():
    print("key:", key, ", value:", value)
key: ID
key: passport
key: books
value: 111
value: my passport
value: [1, 2, 3]
key: ID , value: 111
key: passport , value: my passport
key: books , value: [1, 2, 3]

3 Tuple 元组

用小括号( ),可以通过下标访问,不能修改

files = ("file1", "file2", "file3")
print(files[1])
files[1] = "file4"   # 这里会报错
file2

TypeError: 'tuple' object does not support item assignment

我们就希望它不能改变,特别是你的代码要给别人使用的时候,你没法控制,也不希望他们改变你的这些固定值。

4 Set 合集

set 里面只会存在非重复的元素,用{},无序的,无法下标访问通常使用循环结构

https://www.jianshu.com/p/f60fabfefc091

set_demo1 = {11, 2.1, True, 4 + 5j, '羊老羊', (1, 2, 3)}

for i in set_demo1:
    print(i)
True
2.1
11
(1, 2, 3)
(4+5j)
羊老羊

5 常用功能

  • pop()
files = []
for i in range(5):
    files.append("f"+str(i)+".txt") # 添加
    print("has", files)

for i in range(len(files)):
    print("pop", files.pop())   # 从最后一个开始 pop 出
    print("remain", files)
has ['f0.txt']
has ['f0.txt', 'f1.txt']
has ['f0.txt', 'f1.txt', 'f2.txt']
has ['f0.txt', 'f1.txt', 'f2.txt', 'f3.txt']
has ['f0.txt', 'f1.txt', 'f2.txt', 'f3.txt', 'f4.txt']
pop f4.txt
remain ['f0.txt', 'f1.txt', 'f2.txt', 'f3.txt']
pop f3.txt
remain ['f0.txt', 'f1.txt', 'f2.txt']
pop f2.txt
remain ['f0.txt', 'f1.txt']
pop f1.txt
remain ['f0.txt']
pop f0.txt
remain []
  • 添加和删除
files = ["f1.txt", "f2.txt"]

# 扩充入另一个列表
files.extend(["f3.txt", "f4.txt"])
print("extend", files)

# 按位置添加
files.insert(1, "file5.txt")     # 添加入第1位(首位是0哦)
print("insert", files)

# 移除某索引
del files[1]
print("del", files)

# 移除某值 
files.remove("f3.txt")
print("remove", files)
extend ['f1.txt', 'f2.txt', 'f3.txt', 'f4.txt']
insert ['f1.txt', 'file5.txt', 'f2.txt', 'f3.txt', 'f4.txt']
del ['f1.txt', 'f2.txt', 'f3.txt', 'f4.txt']
remove ['f1.txt', 'f2.txt', 'f4.txt']
  • 字典的get() updata()
files = {"ID": 111, "passport": "my passport", "books": [1,2,3]}

# 按key拿取,并在拿取失败的时候给一个设定好的默认值
print('files["ID"]:', files["ID"])
print('files.get("ID"):', files.get("unknown ID", "不存在这个 ID"))

# 将另一个字典补充到当前字典
files.update({"files": ["1", "2"]})
print('update:', files)

# pop 调一个item,和列表的 pop 类似
popped = files.pop("ID")
print('popped:', popped)
print("remain:", files)
files["ID"]: 111
files.get("ID"): 不存在这个 ID
update: {'ID': 111, 'passport': 'my passport', 'books': [1, 2, 3], 'files': ['1', '2']}
popped: 111
remain: {'passport': 'my passport', 'books': [1, 2, 3], 'files': ['1', '2']}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羊老羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值