编程语言
首页 > 编程语言> > Python基础学习07——之字符串、集合、字典

Python基础学习07——之字符串、集合、字典

作者:互联网

Python基础学习07——之字符串、集合、字典

文章目录

1. 字符串

1.1 字符串的基本运算

1.2 字符串中特别重要的两个方法

contexts = 'hello,world!'
print(contexts.strip('!')) # 脱去
#hello,world
print(contexts.strip('!').split(',')) # 拆分,生成一个列表
#['hello', 'world']

# join函数可以实现将列表连接成字符串
print(' '.join(contexts.strip('!').split(',')))
#hello world

1.3 字符串的格式化输出

contexts = 'hello,world!'
print(contexts.center(80, '='))
print(contexts.rjust(80, '+'))
print(contexts.ljust(80, '+'))
print(contexts.zfill(20))  # 不够左边补0填充

a,b=12,34
print(f'{a}+{b}={a+b}')
print('{}+{}={}'.format(a,b,a+b))
print('{0}+{1}={2}'.format(a,b,a+b))

print(f'{a}/{b}={a/b:.2%}')
print(f'{a}*{b}={a*b:.2e}')
print(f'{a:<10d}')
print(f'{a:0>10d}')

在这里插入图片描述

1.4字符串的应用场景

n = input('n=')
num = int(input('num='))
total = 0
for _ in range(num):
    total += int(n)
    n += n[0]

print(total)
context = 'i love you than 你 爱 me! 1314520'
space = 0
eng = 0
num = 0
other = 0

# 判断性质
for i in range(len(context)):
    if context[i].isdigit():
        num += 1
    elif context[i].isspace():
        space += 1
    elif context[i].isalpha():
        eng += 1
    else:
        other += 1

print(space)
print(eng)
print(num)
print(other)

2. 集合

2.1 集合的定义

# 定义一个空的集合
set1 = set() 
#set2={} # 不能够这样定义集合,会被python解释器会被认为是字典
print(type(set2)) # <class 'dict'> 
set3={1,23,45,56,78,23}
set3={1,23,45,56,78,23}
for elem in set3:
    print(elem)

2.2 集合的运算

# 集合的成员运算远远高于列表的成员运算
print(1 in set1)  # 体现确定性,要么在集合中,要么不在集合中。
print(1 not in set1)

# 体现无序性,不可用小标去索引
# TypeError: 'set' object is not subscriptable
# print(set1[1])
set1 = {1, 2, 3, 4, 6, 7}
set2 = {1, 3, 55, 56}
# & 交集
print(set1 & set2)
print(set1.intersection(set2))

# | 并集
print(set1 | set2)
print(set1.union(set2))
# 差集
print(set1 - set2)
print(set1.difference(set2))

# 对称差
print(set1 ^ set2)
print((set1 | set2) - (set1 & set2))
print(set1.symmetric_difference(set2))

# 判断真子集
print(set1 < set2)
# 判断子集
print(set1 <= set2)
# 判断超集
print(set1 > set2)
  1. 可变容器都无法计算表哈希码,因此都不能放到集合中
  2. 可变容器,列表、集合、字典
l = [12, 3]
# TypeError: unhashable type: 'list'
# set1={[12,3]}

set1 = {(1, 2), 'tian'}
print(set1)

# TypeError: unhashable type: 'set'
# set2={set1}
set1.add('yu')
print(set1)
# 指定删除某个元素
set1.discard('yu')
print(set1)
# 随机删除
print(set1.pop())
# print(set1.pop())
print(set1)
# 清空集合
set1.clear()

在这里插入图片描述

3. 字典

3.1 字典的定义

# 定义一个空字典
dict1 = dict()
print(dict1, type(dict1)) #{} <class 'dict'>
dict2={'name':'yuping','age':'23','merit':'编程'}
print(dict2,type(dict2))
# {'name': 'yuping', 'age': '23', 'merit': '编程'} <class 'dict'>

# 创建字典
student = {'id': 20181703034,
           'name': '小天',
           'birthday': '11-09',
           'favor': ['reading', 'play', 'music', '编程'],
           'contacts': {'email': '504512467',
                        'tel': '15585270810'
                        }}

print(student['name']) # 小天
print(student['contacts']['tel']) # 15585270810

3.2 字典的常用运算

dict2={'name':'yuping','age':'23','merit':'编程'}
# 根据键来获取对应的值
print(dict2.get('name'))
# 按键删除某个键值对
dict2.pop('name')
print(dict2)
# 清空
dict2.clear()
print(dict2)

标签:07,Python,集合,字符串,set1,set2,print,字典
来源: https://blog.csdn.net/qq_52661119/article/details/119223197