- a = {1, 2, 3, 4, 5, 6, 7}
- b = {1, 2, 3, 4}
- print(a.issuperset(b))
- # True
- a = {1, 3, 5, 7}
- b = {2, 4, 6, 8}
- a.update(b)
- print(a)
- # {1, 2, 3, 4, 5, 6, 7, 8}
- a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
- b = {1, 3, 5, 7, 9}
- a.difference_update(b)
- print(a)
- # {0, 2, 4, 6, 8}
- 覆盖式操作,原有值会被盖掉
- x = frozenset([1, 2, 3])
- y = frozenset([4, 5, 6])
- print(x, y)
- print(x & y, x | y)
- # frozenset({1, 2, 3}) frozenset({4, 5, 6})
- # frozenset() frozenset({1, 2, 3, 4, 5, 6})
- s1 = 'china' # 字符串常量
- print(s1[0]) # 可通过内置容器的索引方式来遍历元素 c
- # s1[1]='h' # 报错,字符串常量是一个不可变的序列
- s2 = ['china', 'hello', 'world'] # 字符串列表
- s1 = '我存款金额为:'
- num = -100
- print(s1 + str(num))
- # PS:字符串的拼接对象只能是字符串
- s1 = '''china china china
- china china china
- china china china'''
- print(s1)
- # china china china
- # china china china
- # china china china
- s1 = '2022.加油!中国!'
- print(s1) # 2022.加油!中国!
- print(s1[0]) # 2
- print(s1[0:]) # 2022.加油!中国!
- print(s1[5:]) # 加油!中国!
- print(s1[:4]) # 2022
- print(s1[1:11:2]) # 02加!国
- print(s1[-1:-4:-1]) # !国中
- s1 = input('请输入您的身份证号:')
- print('您的身份证号是:', s1)
- if int(s1[16]) % 2 == 0:
- print('性别:女')
- else:
- print('性别:男')
- print('出生日期为:' + s1[6:10] + '年' + s1[10:12] + '月' + s1[12:14] + '日')
- # 请输入您的身份证号:1233456200203182343
- # 您的身份证号是: 1233456200203182343
- # 性别:男
- # 出生日期为:6200年20月31日
- # 分隔
- s1 = 'Python 软件官网 >>> https://www.python.org/'
- print('原串:', s1)
- l1 = s1.split()
- l2 = s1.split('>>>')
- l3 = s1.split('.')
- l4 = s1.split(" ", 3)
- print(l1, l2, l3, l4, sep='\n')
- # 原串: Python 软件官网 >>> https://www.python.org/
- # ['Python', '软件官网', '>>>', 'https://www.python.org/']
- # ['Python 软件官网 ', ' https://www.python.org/']
- # ['Python 软件官网 >>> https://www', 'python', 'org/']
- # ['Python', '软件官网', '>>>', 'https://www.python.org/']
- # 合并
- s1 = 'fsegfgsdggrdgs'
- s3 = '.'.join(s1)
- print(s3)
- # f.s.e.g.f.g.s.d.g.g.r.d.g.s
- s1 = '****ABC***DEF****'
- c1 = s1.count('*')
- c2 = s1.count('?')
- print('次数:', c1, c2)
- # 次数: 11 0
- s1 = input('主串:')
- s2 = input('子串:')
- n = s1.find(s2)
- if n > -1:
- print('存在,第一次出现索引为:', n)
- else:
- print('不存在')
- # 主串:23456AS
- # 子串:AS
- # 存在,第一次出现索引为: 5
- # PS:一般用于在主串中查找子串是否存在
- s1 = input()
- print(s1.swapcase())
- print(s1.upper())
- print(s1.lower())
- # sdfgaetASFEF
- # SDFGAETasfef
- # SDFGAETASFEF
- # sdfgaetasfef
- s1 = input('请输入:')
- c = {} # 定义空字典
- for i in s1:
- if i.isalpha() == 1:
- c[i] = c.get(i, 0) + 1 # 计算i的字典的键值
- print(c)
- # 请输入:awsedrfgt
- # {'a': 1, 'w': 1, 's': 1, 'e': 1, 'd': 1, 'r': 1, 'f': 1, 'g': 1, 't': 1}
- s1 = input('请输入单词:')
- s1 = s1.strip() # 删除左右两侧空格
- cnt = 1
- print(s1)
- i = 0
- while i < len(s1) - 1:
- if s1[i] != ' ' and s1[i + 1] == ' ':
- cnt += 1
- i += 1
- if s1 == ' ':
- cnt = 0
- print('单词个数:', cnt)
- # 请输入单词:napkin system mmm
- # napkin system mmm
- # 单词个数: 3
- s1 = '****ABC***123****'
- print(s1.replace('*', '?'))
- # ????ABC???123????
- poem='静夜思','李白','窗前明月光','疑是地上霜','举头望明月','低头思故乡'
- for line in poem:
- print('%s'%line.center(10)) # 居中,指定字符宽度
- for line in poem:
- print('%s'%line.rjust(10)) # 右对齐
- for line in poem:
- print('%s'%line.ljust(10)) # 左对齐
- str1 = input('请输入您的密码')
- t = 0
- for i in str1:
- if i.isdigit(): # 判断字符串是否为数字,计算数字个数
- t += 1
- if len(str1) >= 8:
- if str1.isalnum(): # 判断字符串是否只包含字母和数字
- if t >= 2:
- print('密码设置成功')
- else:
- print('密码输出数字少于两个')
- else:
- print('密码只能包含字母和数字')
- else:
- print('密码至少要有8个字符')
- # 请输入您的密码517318tqwei
- # 密码设置成功
- str1 = input('请输入二进制:')
- d1 = 0 # 接收转换结果
- t = 0 # 权值
- f = 1
- if str1.isnumeric(): # 判断字符串中至少有一个字符且所有字符均为数字
- str2 = str1[::-1] # 对字符串进行切片,而后逆置,str2存储逆序的str1
- for i in str2:
- if i == '0' or i == '1':
- d1 = d1 + int(i) * 2 ** t
- else:
- print('无效数字')
- f = 0
- break
- t += 1 # 权值加1
- if f:
- print(str1, '转为十进制整数是:', d1)
- # 请输入二进制:11111111
- # 11111111 转为十进制整数是: 255
- str1 = input('请输入国际标准书号前9位:')
- check = 0
- t = 0
- for i in str1:
- check = check + int(i) * t
- t += 1
- check %= 11
- if check == 10:
- check = 'x'
- print(str1 + str(check))
- # 请输入国际标准书号前9位:987654321
- # 987654321x