python笔记(转存ipynb)------1

list1 = ["tom","cat","Lili"]
print(list1[0].title())
Tom
#append()列表方法在列表末尾添加新元素
list1.append(233)
print(list1)
#可以先创建空列表,再进行追加append(..)以添加
['tom', 'cat', 'Lili', 233]
#insert()列表方法插入元素
list1.insert(1,"kate")
print(list1)
['tom', 'kate', 'cat', 'Lili', 233]
#del...语句删除元素,注意这里是语句不是方法
del list1[2]
print(list1)
['tom', 'kate', 'Lili', 233]
#pop()列表方法删除(弹出)列表末尾的元素(对象)
poped_ele = list1.pop()
print(f"The poped element is {poped_ele}!")
print(f"The current list1 is {list1}")
#pop(n)可以从列表中部分弹出下标为n的元素
print(f"The 2th([1]) element 'kate' will be poped,the result is {list1.pop(1)} ")

The poped element is 233!
The current list1 is ['tom', 'kate', 'Lili']
The 2th([1]) element 'kate' will be poped,the result is kate 
print(f"The current list1 is {list1}")
The current list1 is ['tom', 'Lili']
#remove()列表方法根据值删除列表中的元素
list1.remove("tom")
print(f"The current list1 is {list1}")
The current list1 is ['Lili']
#sort()列表方法对列表进行默认永久排序(升)
list2 = ["d","a","c","b"]
list2.sort()
print(f"The sorted list2 is {list2}")
#reverse参数决定升降序,True则降序
The sorted list2 is ['a', 'b', 'c', 'd']
#sorted(listname)方法创建排序后的对象
list3= ["d","a","c","b"]
sorted_list3 = sorted(list3)
print(sorted_list3)
sorted_list3_reverse = sorted(list3,reverse=True)
print(sorted_list3_reverse)
['a', 'b', 'c', 'd']
['d', 'c', 'b', 'a']
#reverse(listname)方法反转列表顺序(不进行排序)
#len(list)确定列表长度
#4.操作列表
#list()将range()的结果转化为列表
numbers = list(range(1,6)) #[1,6)
print(numbers)
[1, 2, 3, 4, 5]
#min(listname),max(listname),sum(listname)对列表元素分别求最大最小与和
print(f"min:{min(numbers)} max:{max(numbers)} sum:{sum(numbers)}")
min:1 max:5 sum:15
#切片
list5 = list(range(1,6))
print(list5)
cut_list = list5[0:3]
print(cut_list)
[1, 2, 3, 4, 5]
[1, 2, 3]
#复制列表(浅拷贝)
list6 = list5[:]
print(list6)
print(id(list5) == id(list6))
[1, 2, 3, 4, 5]
False
#元组(元素不可变)
dimensions = (200,50)
print(f"({dimensions[0]},{dimensions[1]})")
for dimension in dimensions:
    print(dimension)
(200,50)
200
50
#in 和 not in判断元素是否在集合中
print(200 in dimensions)
print(300 not in dimensions)
True
True
#字典(键(字符串)值(任何对象)对集合)
dic = {'color' : 'yellow', "score" : 5,"a" : "3"}
dic["score"] = 3 #键不能变,值可以变,且可添加减少
print(dic)
print(dic["color"])
print(dic["score"])
print(dic["a"])
print("添加键值对:'name':'zhang san'")
dic['name'] = 'zhang san'
dic['name'].title()
print(f"添加后的字典是{dic}")
#也能进行遍历
print("遍历dic")
for key,value in dic.items():
    print(f"key:{key} value:{value}")
{'color': 'yellow', 'score': 3, 'a': '3'}
yellow
3
3
添加键值对:'name':'zhang san'
添加后的字典是{'color': 'yellow', 'score': 3, 'a': '3', 'name': 'zhang san'}
遍历dic
key:color value:yellow
key:score value:3
key:a value:3
key:name value:zhang san
#del() 语句可以删除键值对
del dic['score']
print()
#从字典中分离键值对,创建键值列表,items()方法可将字典生成列表(特殊列表,只可用for拷贝后使用)
#key()/values()方法可将字典的键/值生成列表(特殊列表,只可用for拷贝后使用)
keys = []
values = []
for key,value in dic.items():
    keys.append(key)
    values.append(value)
    pass
print(dic)
print(keys)
print(dic.keys())
print(dic.values())
print(values)
{'color': 'yellow', 'a': '3', 'name': 'zhang san'}
['color', 'a', 'name']
dict_keys(['color', 'a', 'name'])
dict_values(['yellow', '3', 'zhang san'])
['yellow', '3', 'zhang san']
#set()函数可以将传入的列表/元组去掉重复项,返回一个集合{...}
sets = set(list([1,1,1,2,3,4]))
print(sets)
{1, 2, 3, 4}
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碳酸不酸鸭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值