某马python day07

Python基础学习

Python基础学习——字典

  1. 字典的定义及其简单应用:
    字典:字典⾥⾯的数据是以键值对形式出现,字典数据和数据顺序没有关系,即字典不⽀持下标,后期⽆论数据如何变化,只需要按照对应的键的名字查找数据即可。
    字典的创建注意事项:
    符号为⼤括号
    数据为键值对形式出现
    各个键值对之间⽤逗号隔开
'''
创建字典:{}键值对的形式,注意创建空字典的方法
'''
dict1={"name":"Tom","age":"20","sex":"男"};
dict2={};  # 创建空字典的方法1
dict3=dict(); # 创建空字典的方法2
print(dict1);  # {'name': 'Tom', 'age': '20', 'sex': '男'}
print(dict2);  # {}
print(dict3);  # {}
  1. 字典操作——增加数据和修改数据:
    注意:如果key存在则修改这个key对应的值;如果key不存在则新增此键值对
    修改的语法:字典序列[key] = 值
# 修改字典中的数据。
dict1["name"]="java";
print(dict1);   # {'name': 'java', 'age': '20', 'sex': '男'}
dict1["class"]="三班";
print(dict1);   # {'name': 'java', 'age': '20', 'sex': '男', 'class': '三班'}
  1. 字典操作——删除数据
    删除字典中数据的函数:del()具体的注意事项在代码中体现。
# 删除字典中的数据:del() / del:删除字典或删除字典中指定键值对
dict1={"name":"Tom","age":"20","sex":"男"};
del(dict1);  # 删除整个字典,并且请空字典的地址
del dict1["name"];
print(dict1);   # {'age': '20', 'sex': '男'}
# clear() 清空字典中的数据,并不会释放空间中字典的地址。
print(dict1.clear());   # None

4.字典操作——查找数据
查找的函数及其具体函数的用法:字典序列.get(key, 默认值)
注意事项如果当前查找的key不存在则返回第⼆个参数(默认值),如果省略第⼆个参数,则返回None

# 查找字典中的数据:
dict1={"name":"Tom","age":"20","sex":"男"};
print(dict1.get("name", "没有的情况下返回的默认值"))    # Tom
print(dict1.get("classroom", "没有该数据"))    # 没有该数据
print(dict1.keys());   # dict_keys(['name', 'age', 'sex'])
print(dict1.values()); # dict_values(['Tom', '20', '男'])
print(dict1.items());  # dict_items([('name', 'Tom'), ('age', '20'), ('sex', '男')])
  1. 字典操作——对于字典中的数据的遍历操作:
    对于字典中数据、键值对、值、键的遍历输出操作如下所示:
# 字典值键值对的遍历
dict1={"name":"Tom","age":"20","sex":"男"};
# 遍历字典中的数值
for i in dict1.values():
    print(i,end=" ");   # Tom 20 男
print();
# 遍历字典中的键
for i in dict1.keys():
    print(i, end=" ");   # name age sex
print();
# 遍历字典中的元素
for i in dict1.items():
    print(i, end=" ");   # ('name', 'Tom') ('age', '20') ('sex', '男')
print();
# 遍历字典中的键值对
for key,values in dict1.items(): 
    print(f"{key}={values}",end=" ");    # name=Tom age=20 sex=男 

python基础学习——集合

  1. 集合的定义应用及其集合的特征:
    集合的特点:集合中的数据是不允许有重复的元素出现的
    集合的创建:创建集合使⽤ {} 或 set() , 但是如果要创建空集合只能使⽤ set() ,因为 {} ⽤来创建空字典。
    注意事项:
    集合可以去掉重复数据;集合数据是⽆序的,故不⽀持下标
    创建集合的代码实现:
'''
集合的创建:集合中的元素是不可以有重复的数据,
并且数据可以进行自动的进行去重的操作
'''
t1={1,2,3,4,5,6};
t2={1,1,2,2,3,3,4,4,5,5,6,6,7,7};
t3=set("abccde");
t4=set();  # 创建空的集合
t5={};   # 创建空的字典
print(t1);    # {1, 2, 3, 4, 5, 6}
print(t2);    # {1, 2, 3, 4, 5, 6,7}
print(t3);    # {'b', 'a', 'd', 'e', 'c'}
print(type(t4));   # <class 'set'>
print(type(t5));   # <class 'dict'>
  1. 集合的操作——增加数据
    增加数据的函数:add(),update(),具体的实现如下所示:
# 集合的增加数据的操作:add()
t1={1,2,3,4,5,6};
t1.add("增加数据");
print(t1);  # {1, 2, 3, 4, 5, 6, '增加数据'}
t1.add(1);
# 为集合有去重功能,所以,当向集合内追加的数据是当前集合已有数据的话,则不进⾏任何操作。
print(t1);  # {1, 2, 3, 4, 5, 6, '增加数据'}
# update(),追加的数据是序列的形式
t1.update("abc");
t1.update(["100","200"]);
print(t1);  # {1, 2, 3, 4, 5, 6, 'a', 'b', 'c', '100', '200', '增加数据'}
  1. 集合操作——删除数据:
    删除数据的函数:remove()、discard()、pop(),具体实现及其区别在代码中体现:
t1={1,2,3,4,5,6};
# remove(),删除集合中的指定数据,如果数据不存在则报错。
t1.remove(1);
print(t1);    # {2, 3, 4, 5, 6}
#print(t1.remove(9));  # Traceback (most recent call last):
# discard(),删除集合中的指定数据,如果数据不存在也不会报错,没有删除数据的返回值
t1.discard(3);
print(t1);    # {2, 4, 5, 6}
# 删除不存在的数据也不会报错,并且没有返回值
print(t1.discard(10));
# pop(),随机删除集合中的某个数据,并返回这个数据
numbers=t1.pop();
print(t1);    #  {4, 5, 6}
print(numbers);   # 2
  1. 集合操作——查找
    查找所用的函数:in、not in
# 集合的查找操作:
t1={1,2,3,4,5,6};
print(1 in t1);     # True
print(10 in t1);    # False
print(9 not in t1); # True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值