Python study notes (two): list, tuple and dictionary chapter exercises

1. Prompt the user to enter N strings, encapsulate them into a tuple, then calculate and output the result of multiplying the tuple by 3, and then calculate and output the result of adding ('Java','Python') to the tuple 

# 方法一:提示用户输入N个字符串
str1 = input("请输入第一个字符串:")
str2 = input("请输入第二个字符串:")
str3 = input("请输入第三个字符串:")

a_tuple = (str1, str2, str3)
print("a_tuple:", a_tuple)

# 该元组乘以3的结果
b_tuple = a_tuple * 3
print("b_tuple:", b_tuple)

# 该元组加上('Java', 'Python')的结果
c_tuple = ('Java', 'Python')
print("d_tuple:", b_tuple + c_tuple)



# 方法二:
s = input("请输入3个字符串,使用逗号隔开:")
# 分割字符串
my_list = s.split(",")
# 将列表封装成元组
my_tuple = tuple(my_list)

# 打印元组
print("my_tuple:", my_tuple)

2. Given a list, copy all the elements of the list from start to end to another list 

# 定义一个列表a_list
a_list = ['allan', 'crystal', 'hxs']
print("a_list:", a_list)
# 定义一个空列表b_list
b_list = []

# 将a_list中的元素复制到b_list中
b_list.extend(a_list)

print("b_list:", b_list)

3. The user enters an integer n, generates a list of length n, and puts n random numbers into the list 

import random
n = int(input("请输入一个整数:"))
print("n = ", n)

my_list = []

for i in range(0, n):
    my_list.append(random.random())

print(my_list)

4. The user enters an integer n, generates a list of length n, and puts n random odd numbers into the list 

import random
n = int(input("请输入一个整数:"))
print("n = ", n)

my_list = []

for i in range(0, n) :
    num = random.randint(1, 100)
    while num % 2 == 0 :
        num = random.randint(1, 100)
    my_list.append(num)

print("排序前:", my_list)

# 排序
my_list.sort()
print("排序后:", my_list)
 
5. The user enters an integer n to generate a list of length n, and put n random uppercase characters into the list 
  A~Z(65, 90) 
  a~z(97, 122)
import random
n = int(input("请输入一个整数:"))
print("n = ", n)

my_list = []
for i in range(n):
    num = random.randint(65, 90)
    my_list.append(chr(num))

print(my_list)

# 把一个字符串全部变成大写
print("Hello world".upper()) # HELLO WORLD

6. The user enters N strings, collects these strings into a list, and then removes the repeated strings and outputs the list 

str = input("请输入一个字符串:")

my_list = str.split()
print("去重前my_list:", my_list)

new_list = []
for i in range(len(my_list)):
    if my_list[i] not in new_list:
        new_list.append(my_list[i])

print("去重后my_list:", new_list)

7. The user inputs multiple integers separated by spaces, the program converts these integers into tuple elements, and outputs the tuple and its hash value (using the built-in hash function 

str = input("请输入多个整数,并使用空格隔开:")
my_list = str.split();
# 使用tuple()函数将列表对象转换成元组
my_tuple = tuple(my_list)

# 输出元组my_tuple
print("my_tuple:", my_tuple)
print(hash(my_tuple))
print(my_tuple.__hash__())

8. The user randomly enters N uppercase letters, and the program uses dict to count the number of times each letter entered by the user 

str = input("请输入5个大写字母:")
my_list = str.split();
print("my_list:", my_list)

# 创建空的字典
my_dict = dict()
for i in range(len(my_list)):
    # 统计元素在列表中出现的次数
    num = my_list.count(my_list[i])
    # 通过key添加key-value对
    my_dict[my_list[i]] = num

print("my_dict:", my_dict)

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/113790495