Python容器 之 练习题

1.字符串的基本使用

# 定义一个字符串 str1, 字符串的内容为 "hello world and itcast and itheima and Python"
str1 = "hello world and itcast and itheima and Python"
# 在字符串str1中查找 字符串 and 的下标
num = str1.find('and')
print(num)  # 12
# 在字符串str1中查找字符串 'good'的下标
num1 = str1.find('good')
print(num1)  # -1
# 将字符串str1中的 and 替换为 or
str2 = str1.replace("and", "or")
print(str2)  # hello world or itcast or itheima or Python
# 将字符串 str1 按照 空白字符进行切割,保存到变量 list1 中
list1 = str1.split()
print(list1)  # ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
# 使用 _*_ 将 list1中的字符串进行连接
str3 = "_*_".join(list1)
print(str3)  # hello_*_world_*_and_*_itcast_*_and_*_itheima_*_and_*_Python
# 使用 逗号 将 list1中的字符串进行连接
str4 = ",".join(list1)
print(str4)  # hello,world,and,itcast,and,itheima,and,Python


2.列表的基本使用

# 2.练习对列表的增删改查统计的操作,具体操作如下:
# 1)声明一个列表,包含的数据有:["hello", "python", "itcast", "hello"]
my_list1 = ["hello", "python", "itcast", "hello"]
# 2)在列表中追加一个数据:"Python"
my_list1.append('Python')
print(my_list1)  # ['hello', 'python', 'itcast', 'hello', 'Python']
# 3)删除列表中的第二个数据
my_list1.pop(1)
print(my_list1)  # ['hello', 'itcast', 'hello', 'Python']
# 4)删除列表中的数据:"Python"
my_list1.pop()
print(my_list1)  # ['hello', 'itcast', 'hello']
# 5)将列表中的第二个数据改为:"chuanzhi"
my_list1[1] = 'chuanzhi'
print(my_list1)  # ['hello', 'chuanzhi', 'hello']
# 6)在控制台打印列表中的第一个元素
print(my_list1[0])  # hello
# 7)统计列表中"hello"字符串出现的次数
num = my_list1.count('hello')
print(num)  # 2
# 8)在控制台打印列表的长度
print(len(my_list1))  # 3
# 9)循环遍历列表中的所有数据
for i in my_list1:
    print(i)
# hello
# chuanzhi
# hello


3.列表操作

需求

  • 有一个列表,判断列表中的每一个元素是否以se结尾,如果是,则将其放入一个新的列表中,最后输出这个新的列表
  • list1 = ["red", "apples", "orange", "pink", "bananas", "blue", "black", "white"]
  • 提示: 字符串以什么结尾,即字符串中最后一个字符是 什么?
list1 = ["red", "apples", "orange", "pink", "bananas", "blue", "black", "white"]
list2 = []
for i in list1:
    if i[-1] == 's' or i[-1] == 'e':
        list2.append(i)
print(list2)


4.随机抽签功能[提高]

需求:

  1. 使用 input 输入 5 个学生的名字存入列表
  2. 随机的获取一个学生的名字并打印
# 需求:
# 使用 input 输入 5 个学生的名字存入列表
# 随机的获取一个学生的名字并打印
import random

# 方式一:使用while循环
list1 = []
i = 0
while i < 5:
    name = input("请输入学生的名字:")
    list1.append(name)
    i += 1
print(list1)
num = random.randint(0, 4)
print('随机取一个名字为:', list1[num])

# 方式二:使用for循环
list2 = []
for i in range(5):
    name = input("请输入学生的名字:")
    list2.append(name)
    i += 1
print(list2)
num = random.randint(0, 4)
print('随机取一个名字为:', list2[num])


5.打印列表

需求:

  • 已有列表nums = [10, 20, 30, 40, 50], 将每一个数字在原来的基础上加10,打印列表。
# 方式一:
nums = [10, 20, 30, 40, 50]
for i in range(len(nums)):
    print(nums[i])
    nums[i] += 10
print(nums)

# 方式二:
nums1 = [10, 20, 30, 40, 50]
i = 0
while i < len(nums1):
    print(nums1[i])
    nums1[i] += 10
    i += 1
print(nums1)

 


未完待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

埜玊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值