对应文章:
Python学习记录 #3、数据序列
涉及:字符串、列表、元组、字典、集合。
目录
练习题
一、字符串操作
- 定义一个字符串
s = "Hello, World!"
,请完成以下操作:- 找出字符串中所有的大写字母。
- 将字符串中的第一个逗号替换为分号。
- 使用切片操作提取"World"这个单词。
s = 'Hello, World!' # 1、找出字符串中所有的大写字母。 print(s[0], s[7]) # 2、将字符串中的第一个逗号替换为分号。 s1 = s.replace(',', ';') print(s1) # 3、使用切片操作提取"World"这个单词 s2 = s[7:12] print(s2) ''' H W Hello; World! World '''
- 定义一个字符串
sentence = "Python is a popular programming language."
,请完成以下操作:- 将字符串中的所有小写字母转换为大写字母。
- 使用
split()
函数将字符串拆分为单词列表。 - 判断字符串中是否包含子字符串"popular"。
sentence = "Python is a popular programming language" # 1、将字符串中的所有小写字母转换为大写字母。 print(sentence.upper()) # 2、使用split()函数将字符串拆分为单词列表。 list=[] words = sentence.split(" ",5 ) for word in words: list.append(word) print(list) # 3、判断字符串中是否包含子字符串"popular"。 if 'popular' in sentence: print("YES") ''' PYTHON IS A POPULAR PROGRAMMING LANGUAGE ['Python', 'is', 'a', 'popular', 'programming', 'language'] yes '''
二、列表操作
- 定义一个列表
numbers = [1, 2, 3, 4, 5]
,请完成以下操作:- 在列表的末尾添加一个元素6。
- 删除列表中的第一个元素。
- 使用列表推导式创建一个新列表,其中包含原列表中每个元素的平方。
numbers = [1, 2, 3, 4, 5] # 在列表的末尾添加一个元素6。 numbers.append(6) print(numbers) # 删除列表中的第一个元素。 del numbers[0] print(numbers) # 使用列表推导式创建一个新列表,其中包含原列表中每个元素的平方。 a = [i**2 for i in numbers] print(a) ''' [1, 2, 3, 4, 5, 6] [2, 3, 4, 5, 6] [4, 9, 16, 25, 36] '''
- 定义一个列表
fruits = ["apple", "banana", "cherry", "apple", "orange"]
,请完成以下操作:- 使用
count()
方法计算列表中"apple"出现的次数。 - 使用
remove()
方法删除第一个"apple"。 - 使用
sort()
方法对列表进行排序。fruits = ["apple", "banana", "cherry", "apple", "orange"] # 1、用count()方法计算列表中"apple"出现的次数。 print(fruits.count('apple')) # 返回次数 # 2、用remove()方法删除第一个"apple"。 a = fruits.remove('apple') print(fruits, a) # remove 无返回值 # 3、用sort()方法对列表进行排序。 b = fruits.sort() print(fruits, b) # sort 无返回值 ''' 2 ['banana', 'cherry', 'apple', 'orange'] None ['apple', 'banana', 'cherry', 'orange'] None '''
- 使用
三、元组操作
- 定义一个元组
coordinates = (10, 20)
,请完成以下操作:- 提取元组的第一个元素。
- 尝试修改元组的第一个元素(你应该得到一个错误)。
coordinates = (10, 20) # 1、提取元组的第一个元素。 print(coordinates[0]) # 10 # 2、尝试修改元组的第一个元素(你应该得到一个错误)。 # coordinates[0] = 30 # print(coordinates) # 错误
- 定义一个包含多个元组的元组
points = ((1, 2), (3, 4), (5, 6))
,请完成以下操作:- 提取第二个点的y坐标(即4)。
- 计算所有点的x坐标之和。
points = ((1, 2), (3, 4), (5, 6)) # 1、提取第二个点的y坐标(即4)。 print(points[1][1]) # 2、计算所有点的x坐标之和。 sum = 0 for point in points: x = point[0] sum += x print(sum) ''' 4 9 '''
四、字典操作
- 定义一个字典
person = {"name": "Alice", "age": 30, "city": "New York"}
,请完成以下操作:- 添加一个新的键值对
"country": "USA"
。 - 修改"age"的值为31。
- 使用
get()
方法获取"city"的值,如果键不存在则返回一个默认值"Unknown"。person = {"name": "Alice", "age": 30, "city": "New York"} # 1、添加一个新的键值对"country": "USA"。 person['country'] = 'USA' print(person) # 2、修改"age"的值为31。 person['age'] = 31 print(person) # 3、使用get()方法获取"city"的值,如果键不存在则返回一个默认值"Unknown"。 print(person.get('city', 'Unknown')) ''' {'name': 'Alice', 'age': 30, 'city': 'New York', 'country': 'USA'} {'name': 'Alice', 'age': 31, 'city': 'New York', 'country': 'USA'} New York '''
- 添加一个新的键值对
- 定义一个字典
students = {"Alice": 90, "Bob": 85, "Charlie": 92}
,请完成以下操作:- 创建一个新的字典,其中只包含分数大于或等于90的学生。
- 使用
items()
方法遍历字典,并打印每个学生的姓名和分数。students = {"Alice": 90, "Bob": 85, "Charlie": 92} # 1、创建一个新的字典,其中只包含分数大于或等于90的学生。 dic = {} dic = {key:value for key, value in students.items() if value >= 90} print(dic) # 2、使用items()方法遍历字典,并打印每个学生的姓名和分数。 for item in students.items(): print(item) # 返回键值对列表 ''' {'Alice': 90, 'Charlie': 92} ('Alice', 90) ('Bob', 85) ('Charlie', 92) '''
五、集合操作
- 定义两个集合
set1 = {1, 2, 3, 4}
和set2 = {3, 4, 5, 6}
,请完成以下操作:- 计算两个集合的并集。
- 计算两个集合的交集。
- 计算集合
set1
中但不在set2
中的元素(差集)。set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # 1、计算两个集合的并集。 s1 = set1.union(set2) # 使用 union() 方法来获取并集 print(s1) # 2、计算两个集合的交集。 s2 = set1.intersection(set2) # 使用 intersection() 方法来获取交集 print(s2) # 3、计算集合set1中但不在set2中的元素(差集)。 s3 = set1.difference(set2) # 使用 difference() 方法来获取差集 print(s3) ''' {1, 2, 3, 4, 5, 6} {3, 4} {1, 2} '''
- 定义一个集合
letters = {'a', 'b', 'c', 'a', 'd'}
(注意集合中的元素是唯一的),请完成以下操作:- 验证集合中是否包含字母'a'。
- 删除集合中的字母'c'。
- 尝试添加字母'c'到集合中,并验证添加后集合中'c'的数量(你应该得到一个值1)。
letters = {'a', 'b', 'c', 'a', 'd'} # 1、验证集合中是否包含字母'a'。 result = 'a' in letters print(result) # 2、删除集合中的字母'c'。 result = letters.remove('c') print(letters) # 3、尝试添加字母'c'到集合中,并验证添加后集合中'c'的数量(应该得到一个值1) res = letters.add('c') print(letters) # print(letters.count('c')) # 集合没有count()方法,因为集合不包含重复元素 ''' True {'d', 'b', 'a'} {'d', 'c', 'b', 'a'} '''