代码第四天

1、集合

集合(set)是一个无序的不重复元素序列。

集合中的元素不会重复,并且可以进行交集、并集、差集等常见的集合操作。

可以使用大括号 { } 创建集合,元素之间用逗号 , 分隔, 或者也可以使用 set() 函数创建集合。

创建格式:

parame = {value01,value02,...}
或者
set(value)

1、代码

>>>set1={'a','b','c','d'}
>>>set2=set([1,2,3,4,})
SyntaxError: closing parenthesis '}' does not match opening parenthesis '['
>>>set2=set([1,2,3,4,5])
>>>print(set1)
{'a', 'c', 'd', 'b'}
>>>'a'in set1
True
>>>'a'in set2
False
>>>a=set('abcde')
>>>b=set('12345')
>>>a
{'c', 'e', 'a', 'd', 'b'}
>>>b
{'1', '5', '2', '3', '4'}
>>>a-b
{'c', 'e', 'a', 'd', 'b'}
>>>b=('abc')
>>>b
'abc'
>>>b=set('abc')
>>>b
{'a', 'c', 'b'}
>>>a-b
{'d', 'e'}
>>>a | b
{'c', 'e', 'a', 'd', 'b'}
>>>a&b
{'a', 'c', 'b'}
>>>a^bn
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    a^bn
NameError: name 'bn' is not defined. Did you mean: 'b'?
>>>a^b
{'d', 'e'}
>>>a={x for x in 'abcdef' if x not in 'abc'}
>>>a
{'d', 'e', 'f'}
>>>a.add('blue')
>>>a
{'blue', 'd', 'e', 'f'}
>>>a.update({1,6})
>>>a
{1, 'e', 'f', 6, 'blue', 'd'}
>>>a.updata([7,8],[9,0])
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    a.updata([7,8],[9,0])
AttributeError: 'set' object has no attribute 'updata'. Did you mean: 'update'?
>>>a.update([7,8],[9,0])
>>>a
{0, 1, 'f', 6, 7, 8, 9, 'd', 'e', 'blue'}
>>>a.remove(0)
>>>a
{1, 'f', 6, 7, 8, 9, 'd', 'e', 'blue'}
>>>a.remove(b)
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    a.remove(b)
KeyError: {'a', 'c', 'b'}
>>>a.discard(1)
>>>a
{'f', 6, 7, 8, 9, 'd', 'e', 'blue'}
>>>a.discard(b)
>>>a
{'f', 6, 7, 8, 9, 'd', 'e', 'blue'}
>>>x=a,pop()
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    x=a,pop()
NameError: name 'pop' is not defined. Did you mean: 'pow'?
>>>a=set(('bcder'))
>>>a
{'c', 'e', 'd', 'r', 'b'}
>>>x = a.pop()
>>>x
'c'
>>>len(a)
4
>>>len(x)
1
>>>x.clear()
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    x.clear()
AttributeError: 'str' object has no attribute 'clear'
>>>x.clear()
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    x.clear()
AttributeError: 'str' object has no attribute 'clear'
>>>a.clear()
>>>a
set()
>>>a in b
False
>>>b=set('12345')
>>>b
{'1', '5', '2', '3', '4'}
>>>5 in b
False
>>>'5' in b
True

2、函数表达

方法描述
add()为集合添加元素
clear()移除集合中的所有元素
copy()拷贝一个集合
difference()返回多个集合的差集
difference_update()移除集合中的元素,该元素在指定的集合也存在。
discard()删除集合中指定的元素
intersection()返回集合的交集
intersection_update()返回集合的交集。
isdisjoint()判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
issubset()判断指定集合是否为该方法参数集合的子集。
issuperset()判断该方法的参数集合是否为指定集合的子集
pop()随机移除元素
remove()移除指定元素
symmetric_difference()返回两个集合中不重复的元素集合。
symmetric_difference_update()移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。
union()返回两个集合的并集
update()给集合添加元素

2、条件控制

 if语句的关键字为:if – elif – else

>>>age=int(input("请输入你家修勾的年龄: "))
请输入你家修勾的年龄: 1
>>>if age<=0:
...    print("你在骗我。")
...elif age==1:
...    print("相当于14岁的人。")
...elif age==2:
...    print("相当于28岁的人。")
...elif age > 2:
...    age*=2
...    print("相当于人类的:",age)

    
相当于14岁的人。

操作符

操作符描述
<小于
<=小于或等于
>大于
>=大于或等于
==等于,比较两个值是否相等
!=不等于

嵌套

num=8
guess=-1
print("猜数字游戏!")
while guess!=num:
    guess=int(input("请输入你猜的数字: "))
    if guess==num:
        print("Yes")
        if guess==6:
            print(guess)
    elif guess<num:
        print("less")
        if guess==7:
            print(num)
    elif guess>num:
        print("more")
        if guess==10:
            print(guess)

结果

猜数字游戏!
请输入你猜的数字: 6
less
请输入你猜的数字: 7
less
8
请输入你猜的数字: 8
Yes

matn_case

 match...case 的条件判断,不需要再使用一连串的 if-else 来判断了。

match 后的对象会依次与 case 后的内容进行匹配,如果匹配成功,则执行匹配到的表达式,否则直接跳过,_ 可以匹配一切。

case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功

3、循环

1、while

while 判断条件(condition):
    执行语句(statements)……

  Python 中没有 do..while 循环

a=1
while a<10:
    print(a)
    a+=1

结果

1
2
3
4
5
6
7
8
9

无限循环,按ctrl+C就可以结束

a=1
while a==1:
    num=int(input("输入一个字:"))
    print("你输入的是:",num)

print("bye")
输入一个字:1
你输入的是: 1
输入一个字:2
你输入的是: 2
输入一个字:4
你输入的是: 4
输入一个字:7
你输入的是: 7
输入一个字:8
你输入的是: 8
输入一个字:

2、for

 

>>>sites=['a','b','c','d']
>>>for site in sites:
    ...print(site)

    
a
b
c
d
>>>b='12345'
>>>for c in b:
   ... print(c)

    
1
2
3
4
5
>>>for num in(0,10)
SyntaxError: incomplete input
>>>for num in (0,10):
   ... print(num)

    
0
10
>>>for num in range (0,10):
 ...   print(num)

    
0
1
2
3
4
5
6
7
8
9
>>>for x in range (9):
   ... print(x)
...else:
 ...   print("bye")

    
0
1
2
3
4
5
6
7
8
bye

3、break continue

a=5
while a>0:
    a-=1
    if a==2:
        break
    print(a)
print("循环结束")



a=5
while a>0:
    a-=1
    if a==2:
        continue
    print(a)
print("循环结束")

结果

4
3
循环结束

========================= RESTART: D:/python file/1.py =========================
4
3
1
0
循环结束

4、pass

for a in 'python':
    if a=='o':
        pass
        print('执行pass块')
    print('当前字母:',a)
print("Good bye!")
当前字母: p
当前字母: y
当前字母: t
当前字母: h
执行pass块
当前字母: o
当前字母: n
Good bye!

 pass是空语句,是为了保持程序结构的完整性。

pass 不做任何事情,一般用做占位语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值