python学习之——015set

练习代码:

#set:类似dict,是一组key的集合,不存储value
#本质:无序和无重复元素的集合

#创建set需要一个list或者tuple或者dict作为输入集合
#重复的在set中会自动被过滤
s1 = set([1,2,3,4,5,3,5,6,4,7,8,9])
print(s1)

s2 = set((1,2,3,3,2,1))
print(s2)

s3 = set({1:"good",2:"nice"})
print(s3)

#添加
s4 = set([1,2,3,4,5])
s4.add(6)
s4.add(3) #可以添加重复的,但是不会有效果
#s4.add([7,8,9])  报错  list可变,set要求存储的元素不可变
s4.add((7,8,9))
#s4.add(1:"a")  报错  字典可变,set要求存储的元素不可变
print(s4)
print("**********")

#插入整个list,tuple,字符串,打碎插入
s5 = set([1,2,3,4,5])
s5.update([6,7,8])
s5.update((9,10))
s5.update("sunck")
print(s5)
print("**********")

#删除
s6 = set([1,2,3,4,5])
s6.remove(3)
print(s6)
print("**********")

#遍历,  set没有索引
s7  = set([1,2,3,4,5])
for i in s7:
    print(i)
print("**********")

s8  = set([1,2,3,4,5])
s9  = set([3,4,5,6,7])
#交集
a1 = s8 & s9
print(a1)
print(type(a1))

#并集
a2 = s8 | s9
print(a2)
print(type(a2))
print("**********")

#类型转换
#list -> set
l1 = [1,2,3,4,5,6,2,3]
s1 = set(l1)
print(s1)
print("**********")

#tuple -> set
t2 = (1,2,3,4,5)
t3 = set(t2)
print(t3)
print("**********")

# set -> list
t4 = {1,2,3,4}
l3 = list(t4)
print(l3)
print("**********")

#set -> tuple
t5 = {2,3,4,5}
l4 = tuple(t5)
print(l4)
print("**********")

#list 去重复
l = [1,2,3,4,5,6,3,2,1,5,]
s =set(l)
print(s)

 

运行效果:

D:\pycharm\python\venv\Scripts\python.exe D:/pycharm/python/015set.py
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3}
{1, 2}
{1, 2, 3, 4, 5, 6, (7, 8, 9)}
**********
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 's', 'u', 'k', 'n', 'c'}
**********
{1, 2, 4, 5}
**********
1
2
3
4
5
**********
{3, 4, 5}
<class 'set'>
{1, 2, 3, 4, 5, 6, 7}
<class 'set'>
**********
{1, 2, 3, 4, 5, 6}
**********
{1, 2, 3, 4, 5}
**********
[1, 2, 3, 4]
**********
(2, 3, 4, 5)
**********
{1, 2, 3, 4, 5, 6}

Process finished with exit code 0
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!使用Python的海龟绘图库可以让我们绘制出皮卡丘的图像。下面是一个简单的示例代码: ```python import turtle as t # 设置画布大小和背景色 t.setup(800, 600) t.bgcolor('yellow') # 绘制皮卡丘的头部 t.penup() t.goto(-100, 100) t.pendown() t.fillcolor('yellow') t.begin_fill() t.circle(100) t.end_fill() # 绘制皮卡丘的眼睛 t.penup() t.goto(-60, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(20) t.end_fill() t.penup() t.goto(-40, 120) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(10) t.end_fill() t.penup() t.goto(-140, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(20) t.end_fill() t.penup() t.goto(-120, 120) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(10) t.end_fill() # 绘制皮卡丘的嘴巴 t.penup() t.goto(-80, 30) t.pendown() t.setheading(-60) t.circle(80, 120) t.fillcolor('black') t.begin_fill() t.circle(80, 120) t.setheading(0) t.circle(80, 120) t.end_fill() # 绘制皮卡丘的脚 t.penup() t.goto(-70, -70) t.pendown() t.setheading(-30) t.forward(80) t.setheading(0) t.forward(40) t.setheading(90) t.fillcolor('black') t.begin_fill() t.circle(40, 180) t.end_fill() t.setheading(180) t.forward(40) t.setheading(-90) t.circle(40, 180) t.setheading(-60) t.forward(80) # 绘制皮卡丘的手臂 t.penup() t.goto(30, -70) t.pendown() t.setheading(-30) t.forward(80) t.setheading(0) t.forward(40) t.setheading(90) t.fillcolor('black') t.begin_fill() t.circle(40, 180) t.end_fill() t.setheading(180) t.forward(40) t.setheading(-90) t.circle(40, 180) t.setheading(-60) t.forward(80) # 隐藏画笔 t.hideturtle() # 完成绘制 t.done() ``` 运行以上代码,即可在窗口中看到绘制出的皮卡丘图像。你也可以根据需要调整代码中的坐标和尺寸来绘制更精确的图像。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值