【program100道】小白专用~

题目:编写一个 Python 程序,接收用户输入的字符串,将其中的大写字母转换成小写字母,并输出转换后的字符串。
代码:
s = input(“请输入字符串:”)
s_lower = s.lower()
print(s_lower)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的偶数提取出来并输出。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
even_lst = [num for num in lst if num % 2 == 0]
print(even_lst)

题目:编写一个 Python 程序,判断一个字符串是否是回文字符串。
代码:
s = input(“请输入字符串:”)
if s == s[::-1]:
print(“是回文字符串”)
else:
print(“不是回文字符串”)

题目:编写一个 Python 程序,给定一个列表,将其中的元素去重后输出。
代码:
lst = [1, 2, 3, 3, 4, 5, 5, 6, 6, 7]
unique_lst = list(set(lst))
print(unique_lst)

题目:编写一个 Python 程序,统计一个字符串中每个单词出现的次数,输出字典类型结果。
代码:
s = “This is a test string, and this is another test string.”
lst = s.split()
d = {}
for word in lst:
if word in d:
d[word] += 1
else:
d[word] = 1
print(d)

题目:编写一个 Python 程序,给定两个列表,找出它们中共同出现的元素。
代码:
lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 5, 6, 7, 8]
common_lst = list(set(lst1) & set(lst2))
print(common_lst)

题目:编写一个 Python 程序,将一个整数转换成二进制、八进制和十六进制,并输出结果。
代码:
num = 123
bin_num = bin(num)
oct_num = oct(num)
hex_num = hex(num)
print(“二进制:”, bin_num)
print(“八进制:”, oct_num)
print(“十六进制:”, hex_num)

题目:编写一个 Python 程序,给定一个字符串和一个子字符串,统计子字符串在原字符串中出现的次数。
代码:
s = “This is a test string, and this is another test string.”
sub_s = “is”
count = s.count(sub_s)
print(count)

题目:编写一个 Python 程序,生成一个包含 10 个随机浮点数的列表,然后将其中的最大值和最小值输出。
代码:
import random

lst = [random.uniform(1, 10) for i in range(10)]
max_num = max(lst)
min_num = min(lst)
print(“最大值:”, max_num)
print(“最小值:”, min_num)

题目:编写一个 Python 程序,给定一个字符串和一个整数 n,将字符串每个长度为 n 的子串反转后输出。
代码:
s = “This is a test string, and this is another test string.”
n = 3
result = “”
for i in range(0, len(s), n):
result += s[i:i+n][::-1]
print(result)

题目:编写一个 Python 程序,生成斐波那契数列的前 10 个数并输出。
代码:
n = 10
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
print(fib)

题目:编写一个 Python 程序,给定一个列表,将其中的奇数提取出来并输出。
代码:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_lst = [num for num in lst if num % 2 != 0]
print(odd_lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的数字提取出来并输出。
代码:
import re
s = “This is a test string with 123 and 456.”
nums = re.findall(r’\d+', s)
print(nums)

题目:编写一个 Python 程序,生成一个包含 10 个随机字符串的列表,然后将其中的长度大于等于 5 的字符串提取出来并输出。
代码:
import random
import string
lst = [‘’.join(random.choices(string.ascii_lowercase, k=5)) for i in range(10)]
long_lst = [s for s in lst if len(s) >= 5]
print(long_lst)
题目:编写一个 Python 程序,给定两个字符串,找出它们中最长的公共子串。
代码:
def find_longest_common_substring(s1, s2):
m = [[0] * (1 + len(s2)) for i in range(1 + len(s1))]
longest, x_longest = 0, 0
for x in range(1, 1 + len(s1)):
for y in range(1, 1 + len(s2)):
if s1[x - 1] == s2[y - 1]:
m[x][y] = m[x - 1][y - 1] + 1
if m[x][y] > longest:
longest = m[x][y]
x_longest = x
else:
m[x][y] = 0
return s1[x_longest - longest: x_longest]

s1 = “This is a test string.”
s2 = “Another test string.”
common_substring = find_longest_common_substring(s1, s2)
print(common_substring)

题目:编写一个 Python 程序,给定一个列表,计算其中所有元素的平均值并输出。
代码:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
avg = sum(lst) / len(lst)
print(avg)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词的首字母大写并输出。
代码:
s = “this is a test string.”
s_title = ’ '.join(word.capitalize() for word in s.split())
print(s_title)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的最大值和最小值交换,并输出结果。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
max_num = max(lst)
min_num = min(lst)
max_index = lst.index(max_num)
min_index = lst.index(min_num)
lst[max_index], lst[min_index] = lst[min_index], lst[max_index]
print(lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照字典序排序并输出。
代码:
s = “this is a test string.”
s_sorted = ’ '.join(sorted(s.split()))
print(s_sorted)

题目:编写一个程序,将列表中的元素去重并按照大小排序
代码:
lst = [3, 1, 4, 2, 5, 2, 1, 6]
lst = sorted(list(set(lst)))
print(lst)

题目:编写一个函数,判断一个数是否为质数
代码:
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True

题目:给定两个字符串s和t,请编写一个函数来判断它们是否是同构的。如果s中的字符可以被替换得到t,那么这两个字符串是同构的。
代码:
def is_isomorphic(s: str, t: str) -> bool:
s2t, t2s = {}, {}
for i in range(len(s)):
if s[i] not in s2t and t[i] not in t2s:
s2t[s[i]] = t[i]
t2s[t[i]] = s[i]
elif s2t.get(s[i]) != t[i] or t2s.get(t[i]) != s[i]:
return False
return True

题目:给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点
代码:
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
def remove_nth_from_end(head: ListNode, n: int) -> ListNode:
if not head:
return None
dummy = ListNode(0)
dummy.next = head
fast = slow = dummy
for _ in range(n):
fast = fast.next
while fast and fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next

题目:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
代码:
def length_of_longest_substring(s: str) -> int:
s2idx, start, res = {}, 0, 0
for end in range(len(s)):
if s[end] in s2idx and s2idx[s[end]] >= start:
start = s2idx[s[end]] + 1
s2idx[s[end]] = end
res = max(res, end - start + 1)
return res

题目:编写一个函数,输入一个整数,输出该数二进制表示中 1 的个数
代码:
def hamming_weight(n: int) -> int:
res = 0
while n:
n &= n - 1
res += 1
return res

题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回它们的数组下标
代码:
def two_sum(nums: List[int], target: int) -> List[int]:
d = {}
for i in range(len(nums)):
if target - nums[i] in d:
return [d[target-nums[i]], i]
d[nums[i]] = i

题目:翻转一棵二叉树
代码:
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

def invert_tree(root: TreeNode) -> TreeNode:
if not root:
return None
root.left, root.right = invert_tree(root.right), invert_tree(root.left)
return root

题目:编写一个程序,输出所有由1、2、3、4组成的不同排列,例如1234、1243等
代码:
def permute(nums: List[int]) -> List[List[int]]:
res = []
def dfs(cur):
if len(cur) == len(nums):
res.append(cur[:])
return
for num in nums:
if num not in cur:
cur.append(num)
dfs(cur)
cur.pop()
dfs([])
return res

题目:编写一个 Python 程序,给定一个列表和一个元素,统计该元素在列表中出现的次数并输出。
代码:

lst = [1, 2, 3, 4, 5, 3, 2, 1, 3, 4, 5, 6]
elem = 3
count = lst.count(elem)
print(count)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有空格去除并输出。
代码:

s = “This is a test string.”
s_no_space = s.replace(’ ', ‘’)
print(s_no_space)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的元素按照从大到小的顺序排序并输出。
代码:

import random

lst = [random.randint(1, 100) for i in range(10)]
lst_sorted = sorted(lst, reverse=True)
print(lst_sorted)

题目:编写一个 Python 程序,给定一个字符串和一个子字符串,将字符串中所有的子字符串替换成给定的字符串并输出。
代码:
s = “This is a test string.”
sub_s = “test”
new_s = s.replace(sub_s, “example”)
print(new_s)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的元素求和并输出。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
lst_sum = sum(lst)
print(lst_sum)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有大写字母转换成小写字母并输出。
代码:
s = “This is a test string.”
s_lower = s.lower()
print(s_lower)

题目:编写一个 Python 程序,给定两个列表,将它们合并成一个列表并去重后输出。
代码:
lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 5, 6, 7, 8]
merged_lst = list(set(lst1 + lst2))
print(merged_lst)

题目:编写一个 Python 程序,给定一个字符串和一个整数 n,将字符串每个长度为 n 的子串输出。
代码:
s = “This is a test string.”
n = 3
substrings = [s[i:i+n] for i in range(0, len(s), n)]
print(substrings)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素插入到列表的末尾并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 6
lst.append(elem)
print(lst)

题目:判断一个数是否为素数,如果是,输出“是素数”,否则输出“不是素数”
代码:
num = int(input(“请输入一个正整数:”))
flag = True
if num <= 1:
flag = False
else:
for i in range(2, int(num/2)+1):
if num % i == 0:
flag = False
break
if flag:
print(num, “是素数”)
else:
print(num, “不是素数”)

题目:求列表中的最大值和最小值,输出结果
代码:
lst = [23, 45, 67, 12, 89, 34]
max_val = max(lst)
min_val = min(lst)
print(“最大值:”, max_val)
print(“最小值:”, min_val)

题目:将一个列表中的元素按照从大到小的顺序排列,输出结果
代码:
lst = [23, 45, 67, 12, 89, 34]
lst.sort(reverse=True)
print(lst)

题目:将一个字符串反转,输出结果
代码:
s = “hello world”
s_reversed = s[::-1]
print(s_reversed)

题目:给定一个年份,判断是否是闰年,输出“是闰年”或“不是闰年”
代码:
year = int(input(“请输入一个年份:”))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, “是闰年”)
else:
print(year, “不是闰年”)

题目:计算圆的面积,输入半径,输出结果
代码:
import math
r = float(input(“请输入圆的半径:”))
area = math.pi * r ** 2
print(“圆的面积为:”, area)

题目:将一个列表中的元素去重并按照从小到大的顺序排列,输出结果
代码:
lst = [23, 45, 67, 12, 89, 34, 12, 23]
lst = list(set(lst))
lst.sort()
print(lst)

题目:判断一个字符串是否是回文,输出“是回文”或“不是回文”
代码:
s = input(“请输入一个字符串:”)
if s == s[::-1]:
print(s, “是回文”)
else:
print(s, “不是回文”)

题目:将一个二维数组(矩阵)进行转置,输出结果
代码:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transpose_matrix = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print(transpose_matrix)

题目:计算斐波那契数列的第n项,输出结果
代码:
n = int(input(“请输入一个正整数:”))
fibonacci_seq = [0, 1]
for i in range(2, n+1):
fibonacci_seq.append(fibonacci_seq[i-1] + fibonacci_seq[i-2])
print(“斐波那契数列的第%d项为:%d” % (n, fibonacci_seq[n]))

题目:编写一个 Python 程序,给定一个列表,将其中的重复元素去除并输出。
代码:
lst = [1, 2, 3, 4, 5, 2, 3, 1, 4, 6, 7]
unique_lst = list(set(lst))
print(unique_lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有字母顺序反转并输出。
代码:
s = “This is a test string.”
s_reversed = s[::-1]
print(s_reversed)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的奇数提取出来并求和并输出。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
odd_lst = [num for num in lst if num % 2 != 0]
odd_sum = sum(odd_lst)
print(odd_sum)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词反转并输出。
代码:
s = “This is a test string.”
s_reversed = ’ '.join(word[::-1] for word in s.split())
print(s_reversed)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的元素按照从小到大的顺序排序并输出。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
lst_sorted = sorted(lst)
print(lst_sorted)

题目:编写一个 Python 程序,给定一个字符串和一个子字符串,统计该子字符串在字符串中出现的次数并输出。
代码:
s = “This is a test string.”
sub_s = “test”
count = s.count(sub_s)
print(count)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的偶数提取出来并输出。
代码:
import random
lst = [random.randint(1, 100) for i in range(10)]
even_lst = [num for num in lst if num % 2 == 0]
print(even_lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照长度从小到大的顺序排序并输出。
代码:
s = “This is a test string.”
s_sorted = ’ '.join(sorted(s.split(), key=len))
print(s_sorted)

题目:编写一个 Python 程序,生成一个包含 10 个随机整数的列表,然后将其中的元素求积并输出。
代码:
import random
from functools import reduce
lst = [random.randint(1, 10) for i in range(10)]
lst_product = reduce(lambda x, y: x*y, lst)
print(lst_product)

题目:编写一个 Python 程序,给定两个字符串,判断它们是否互为旋转字符串。
代码:
def is_rotation(s1, s2):
if len(s1) != len(s2):
return False
s1s1 = s1 + s1
if s2 in s1s1:
return True
else:
return False
s1 = “This is a test string.”
s2 = “string.This is a test”
if is_rotation(s1, s2):
print(“The two strings are rotations of each other.”)
else:
print(“The two strings are not rotations of each other.”)

题目:编写一个 Python 程序,输出 “Hello, World!”。
代码:
print(“Hello, World!”)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有整数。
代码:
n = 10
for i in range(1, n+1):
print(i)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有大写字母转换成小写字母并输出。
代码:
s = “This is a Test String.”
s_lower = s.lower()
print(s_lower)

题目:编写一个 Python 程序,给定一个列表,输出其中的最大值和最小值。
代码:
lst = [1, 2, 3, 4, 5]
max_val = max(lst)
min_val = min(lst)
print(“Max value:”, max_val)
print(“Min value:”, min_val)

题目:编写一个 Python 程序,给定一个字符串,输出它的长度。
代码:
s = “This is a test string.”
length = len(s)
print(length)

题目:编写一个 Python 程序,给定一个整数 n,输出从 n 到 1 的所有整数。
代码:
n = 10
for i in range(n, 0, -1):
print(i)

题目:编写一个 Python 程序,给定两个整数,输出它们的和。
代码:
a = 3
b = 4
sum = a + b
print(sum)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有空格去除并输出。
代码:
s = “This is a test string.”
s_no_space = s.replace(’ ', ‘’)
print(s_no_space)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照长度从大到小的顺序排序并输出。
代码:
s = “This is a test string.”
s_sorted = ’ '.join(sorted(s.split(), key=len, reverse=True))
print(s_sorted)

题目:编写一个 Python 程序,给定一个列表和一个元素,输出该元素在列表中的索引位置(如果存在)。
代码:
lst = [1, 2, 3, 4, 5]
elem = 3
if elem in lst:
index = lst.index(elem)
print(index)
else:
print(“Element not found in list.”)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有偶数。
代码:
lst = [1, 2, 3, 4, 5]
even_lst = [num for num in lst if num % 2 == 0]
print(even_lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有字母转换成大写字母并输出。
代码:
s = “This is a Test String.”
s_upper = s.upper()
print(s_upper)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有整数。
代码:
n = 10
for i in range(n+1):
print(i)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词首字母大写并输出。
代码:
s = “this is a test string.”
s_title = s.title()
print(s_title)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有奇数。
代码:
lst = [1, 2, 3, 4, 5]
odd_lst = [num for num in lst if num % 2 != 0]
print(odd_lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成小写字母并输出。
代码:
s = “This is a Test String.”
s_lower = s.lower()
print(s_lower)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有偶数。
代码:
n = 10
for i in range(2, n+1, 2):
print(i)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有字母顺序反转并输出。
代码:
s = “This is a test string.”
s_reversed = s[::-1]
print(s_reversed)

题目:编写一个 Python 程序,给定一个列表和一个元素,向该列表中添加该元素并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 6
lst.append(elem)
print(lst)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有奇数。
代码:
n = 10
for i in range(1, n+1, 2):
print(i)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素。
代码:
lst = [1, 2, 3, 4, 5]
for elem in lst:
print(elem)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成大写字母并输出。
代码:
s = “This is a Test String.”
s_upper = s.upper()
print(s_upper)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有整数的平方。
代码:
n = 5
for i in range(1, n+1):
print(i**2)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词首字母小写并输出。
代码:
s = “This is a Test String.”
s_lower = s.lower()
s_capitalized = s_lower.capitalize()
print(s_capitalized)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的和。
代码:
lst = [1, 2, 3, 4, 5]
sum = 0
for elem in lst:
sum += elem
print(sum)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有字母顺序反转并输出。
代码:
s = “This is a test string.”
s_reversed = s[::-1]
print(s_reversed)

题目:编写一个 Python 程序,给定一个整数 n,输出从 n 到 1 的所有整数的平方。
代码:
n = 5
for i in range(n, 0, -1):
print(i**2)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照长度从小到大的顺序排序并输出。
代码:
s = “This is a test string.”
s_sorted = ’ '.join(sorted(s.split(), key=len))
print(s_sorted)

题目:编写一个 Python 程序,给定一个列表和一个元素,从该列表中删除该元素并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 3
if elem in lst:
lst.remove(elem)
print(lst)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有整数的平方。
代码:
n = 5
for i in range(n+1):
print(i**2)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的平均值。
代码:
lst = [1, 2, 3, 4, 5]
avg = sum(lst) / len(lst)
print(avg)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词首字母大写并输出。
代码:
s = “this is a test string.”
s_title = s.title()
print(s_title)

题目:编写一个 Python 程序,给定两个整数,输出它们的差。
代码:
a = 3
b = 4
diff = a - b
print(diff)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照长度从大到小的顺序排序并输出。
代码:
s = “This is a test string.”
s_sorted = ’ '.join(sorted(s.split(), key=len, reverse=True))
print(s_sorted)

题目:编写一个 Python 程序,给定一个整数 n,输出从 n 到 1 的所有整数的立方。
代码:
n = 5
for i in range(n, 0, -1):
print(i**3)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成小写字母并去除其中的标点符号后输出。
代码:
import string
s = “This is a Test String, with some punctuation.”
s_lower = s.lower()
s_no_punct = s_lower.translate(str.maketrans(‘’, ‘’, string.punctuation))
print(s_no_punct)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的最大值和最小值。
代码:
lst = [1, 2, 3, 4, 5]
max_val = max(lst)
min_val = min(lst)
print(“Max value:”, max_val)
print(“Min value:”, min_val)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有整数的立方。
代码:
n = 5
for i in range(1, n+1):
print(i**3)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照长度从小到大的顺序排序并去除其中的标点符号后输出。
代码:
import string

s = “This is a test string, with some punctuation.”
s_no_punct = s.translate(str.maketrans(‘’, ‘’, string.punctuation))
s_sorted = ’ '.join(sorted(s_no_punct.split(), key=len))
print(s_sorted)

题目:编写一个 Python 程序,给定一个列表和一个元素,判断该元素是否在列表中出现并输出 True 或 False。
代码:
lst = [1, 2, 3, 4, 5]
elem = 3
if elem in lst:
print(True)
else:
print(False)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词按照字典序从小到大的顺序排序并输出。
代码:
s = “This is a test string.”
s_sorted = ’ '.join(sorted(s.split()))
print(s_sorted)

题目:编写一个 Python 程序,给定两个整数,输出它们的和。
代码:
a = 3
b = 4
sum = a + b
print(sum)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有字母转换成大写字母并输出。
代码:
s = “This is a test string.”
s_upper = s.upper()
print(s_upper)

题目:编写一个 Python 程序,给定一个整数 n,输出从 1 到 n 的所有整数的和。
代码:
n = 5
sum = 0
for i in range(1, n+1):
sum += i
print(sum)

题目:编写一个 Python 程序,给定一个字符串和一个子串,判断该子串是否在字符串中出现并输出 True 或 False。
代码:
s = “This is a test string.”
sub = “test”
if sub in s:
print(True)
else:
print(False)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的乘积。
代码:
lst = [1, 2, 3, 4, 5]
product = 1
for elem in lst:
product *= elem
print(product)

题目:编写一个 Python 程序,给定一个整数 n,输出从 n 到 1 的所有整数的和。
代码:
n = 5
sum = 0
for i in range(n, 0, -1):
sum += i
print(sum)

题目:编写一个 Python 程序,给定一个字符串和一个子串,统计该子串在字符串中出现的次数并输出。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
count = s.count(sub)
print(count)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素插入到列表的末尾并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 6
lst.append(elem)
print(lst)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有整数的和。
代码:
n = 5
sum = 0
for i in range(n+1):
sum += i
print(sum)

题目:编写一个 Python 程序,给定一个列表和一个元素,统计该元素在列表中出现的次数并输出。
代码:
lst = [1, 2, 3, 4, 5, 3, 4, 3]
elem = 3
count = lst.count(elem)
print(count)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词逆序输出。
代码:
s = “This is a test string.”
s_reversed = ’ '.join(s.split()[::-1])
print(s_reversed)

题目:编写一个 Python 程序,给定两个整数,判断它们是否相等并输出 True 或 False。
代码:
a = 3
b = 4
if a == b:
print(True)
else:
print(False)

题目:编写一个 Python 程序,给定一个字符串和一个子串,统计该子串在字符串中出现的位置并输出。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
indices = [i for i in range(len(s)) if s.startswith(sub, i)]
print(indices)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的平方。
代码:
lst = [1, 2, 3, 4, 5]
squared = [elem**2 for elem in lst]
print(squared)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有偶数。
代码:
n = 10
evens = [i for i in range(n+1) if i % 2 == 0]
print(evens)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成大写字母并输出。
代码:
s = “This is a test string.”
s_upper = s.upper()
print(s_upper)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素插入到列表的开头并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 6
lst.insert(0, elem)
print(lst)

题目:编写一个 Python 程序,给定一个字符串和一个子串,将其中的所有子串都替换成另一个字符串并输出。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
replacement = “example”
s_replaced = s.replace(sub, replacement)
print(s_replaced)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素从列表中删除并输出。
代码:
lst = [1, 2, 3, 4, 5]
elem = 3
lst.remove(elem)
print(lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词首字母大写并输出。
代码:
s = “this is a test string.”
s_title = s.title()
print(s_title)

题目:编写一个 Python 程序,给定两个整数,输出它们的差。
代码:
a = 3
b = 4
diff = b - a
print(diff)

题目:编写一个 Python 程序,给定一个字符串和一个子串,将其中第一个出现的子串替换成另一个字符串并输出。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
replacement = “example”
s_replaced = s.replace(sub, replacement, 1)
print(s_replaced)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的平均值。
代码:
lst = [1, 2, 3, 4, 5]
avg = sum(lst) / len(lst)
print(avg)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有奇数。
代码:
n = 10
odds = [i for i in range(n+1) if i % 2 == 1]
print(odds)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成小写字母并输出。
代码:
s = “This is a test string.”
s_lower = s.lower()
print(s_lower)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素从列表中删除所有出现的位置并输出。
代码:
lst = [1, 2, 3, 4, 5, 3, 4, 3]
elem = 3
while elem in lst:
lst.remove(elem)
print(lst)

题目:编写一个 Python 程序,给定一个字符串和一个子串,将其中的所有子串都替换成另一个字符串,并输出替换后的字符串和替换的次数。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
replacement = “example”
s_replaced, count = s.replace(sub, replacement), s.count(sub)
print(s_replaced, count)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素。
代码:
lst = [1, 2, 3, 4, 5]
print(lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词倒序输出。
代码:
s = “This is a test string.”
s_reversed = ’ '.join(s.split()[::-1])
print(s_reversed)

题目:编写一个 Python 程序,给定两个整数,输出它们的和。
代码:
a = 3
b = 4
sum = a + b
print(sum)

题目:编写一个 Python 程序,给定一个字符串和一个子串,判断该子串是否在字符串中出现并输出 True 或 False。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
if sub in s:
print(True)
else:
print(False)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的和。
代码:
lst = [1, 2, 3, 4, 5]
sum = sum(lst)
print(sum)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有整数。
代码:
n = 10
nums = [i for i in range(n+1)]
print(nums)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成首字母大写并输出。
代码:
s = “this is a test string.”
s_capitalized = s.title()
print(s_capitalized)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素从列表中删除所有出现的位置并输出删除后的列表。
代码:
lst = [1, 2, 3, 4, 5, 3, 4, 3]
elem = 3
lst = [i for i in lst if i != elem]
print(lst)

题目:编写一个 Python 程序,给定一个字符串和一个子串,判断该子串在字符串中出现的位置并输出。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
indices = [i for i in range(len(s)) if s.startswith(sub, i)]
print(indices)

题目:编写一个 Python 程序,给定一个字符串和一个子串,判断该子串是否在字符串中出现且仅出现一次,并输出 True 或 False。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
if s.count(sub) == 1:
print(True)
else:
print(False)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素插入到列表的末尾并输出更新后的列表。
代码:
lst = [1, 2, 3, 4, 5]
elem = 6
lst.append(elem)
print(lst)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词逆序输出。
代码:
s = “This is a test string.”
s_reversed = ’ '.join(s.split()[::-1])[::-1]
print(s_reversed)

题目:编写一个 Python 程序,给定两个整数,输出它们的积。
代码:
a = 3
b = 4
product = a * b
print(product)

题目:编写一个 Python 程序,给定一个字符串和一个子串,将该子串从字符串中删除并输出删除后的字符串。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
s_removed = s.replace(sub, “”, 1)
print(s_removed)

题目:编写一个 Python 程序,给定一个列表,输出其中的所有元素的最大值。
代码:
lst = [1, 2, 3, 4, 5]
max_num = max(lst)
print(max_num)

题目:编写一个 Python 程序,给定一个整数 n,输出从 0 到 n 的所有偶数。
代码:
n = 10
evens = [i for i in range(n+1) if i % 2 == 0]
print(evens)

题目:编写一个 Python 程序,给定一个字符串,将其中的所有单词转换成全部大写字母并输出。
代码:
s = “This is a test string.”
s_upper = s.upper()
print(s_upper)

题目:编写一个 Python 程序,给定一个列表和一个元素,将该元素从列表中删除第一次出现的位置并输出更新后的列表。
代码:
lst = [1, 2, 3, 4, 5, 3, 4, 3]
elem = 3
lst.remove(elem)
print(lst)

题目:编写一个 Python 程序,给定一个字符串和一个子串,将其中所有子串都删除并输出删除后的字符串。
代码:
s = “This is a test string, and this is another test string.”
sub = “test”
s_removed = s.replace(sub, “”)
print(s_removed)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值