Python 基础30道测试题「答案」

题目:https://blog.csdn.net/qq_33254766/article/details/133895035

  1. 输出 "Hello, World!"。
python 复制代码
print("Hello, World!")
  1. 创建一个变量,并为其赋值,然后输出该变量的值。
python 复制代码
x = 10
print(x)
  1. 输入两个数,然后输出它们的和。
python 复制代码
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a + b)
  1. 使用if语句判断一个数是奇数还是偶数。
python 复制代码
num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
  1. 创建一个列表,然后添加、删除元素。
python 复制代码
lst = [1, 2, 3]
lst.append(4)  # 添加
lst.remove(2)  # 删除
print(lst)
  1. 使用for循环输出列表中的每一个元素。
python 复制代码
for item in lst:
    print(item)
  1. 使用while循环输出从1到10的数字。
python 复制代码
i = 1
while i <= 10:
    print(i)
    i += 1
  1. 定义一个函数,该函数接受两个参数并返回它们的和。
python 复制代码
def add(a, b):
    return a + b
  1. 使用异常处理来避免除以0的错误。
python 复制代码
try:
    x = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
  1. 使用列表推导创建一个包含10个偶数的列表。
python 复制代码
evens = [i for i in range(2, 21, 2)]
print(evens)
  1. 创建一个字典并进行增、删、查、改操作。
python 复制代码
dictionary = {"a": 1, "b": 2}
dictionary["c"] = 3  # 增
del dictionary["a"]  # 删
print(dictionary.get("b"))  # 查
dictionary["b"] = 4  # 改
print(dictionary)
  1. 使用Python内置的len()函数计算字符串的长度。
python 复制代码
s = "Python"
print(len(s))
  1. 使用切片获取字符串的子串。
python 复制代码
print(s[1:4])
  1. 创建一个集合,并进行添加和删除操作。
python 复制代码
my_set = {1, 2, 3}
my_set.add(4)  # 添加
my_set.remove(2)  # 删除
print(my_set)
  1. 使用import导入一个模块,并使用其中的函数。(这个例子假设我们导入了math模块)
python 复制代码
import math
print(math.sqrt(16))
  1. 创建一个包含多个函数的模块。(这需要在一个.py文件中做,这里只是一个示例)
python 复制代码
# my_module.py
def func1():
    pass

def func2():
    pass
  1. 使用lambda表达式定义一个匿名函数。
python 复制代码
f = lambda x: x * 2
print(f(5))
  1. 使用filter()lambda输出一个列表中的所有偶数。
python 复制代码
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)
  1. 使用map()lambda将列表中的每个数字乘以2。
python 复制代码
doubled = list(map(lambda x: x * 2, nums))
print(doubled)
  1. 使用文件操作写入和读取一个文件。
python 复制代码
with open("file.txt", "w") as f:
    f.write("Hello, World!")

with open("file.txt", "r") as f:
    content = f.read()
    print(content)
  1. 使用def关键字定义一个接受不定数量参数的函数。
python 复制代码
def print_args(*args):
    for arg in args:
        print(arg)

print_args(1, 2, 3, 4)
  1. 使用列表、元组和字典创建一个复合数据结构。
python 复制代码
data = {
    "names": ["Alice", "Bob", "Charlie"],
    "ages": (25, 30, 35),
    "scores": [85, 90, 95]
}
print(data)
  1. 使用enumerate()遍历列表的索引和元素。
python 复制代码
for index, value in enumerate(["a", "b", "c"]):
    print(index, value)
  1. 使用zip()合并两个列表。
python 复制代码
names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]
combined = list(zip(names, scores))
print(combined)
  1. 使用列表的sort()方法和sorted()函数排序一个列表。
python 复制代码
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5]
lst.sort()
print(lst)

lst2 = [3, 1, 4, 1, 5, 9, 2, 6, 5]
sorted_lst2 = sorted(lst2)
print(sorted_lst2)
  1. 使用集合的intersection()方法找到两个集合的交集。
python 复制代码
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print(set1.intersection(set2))
  1. 使用字典的get()方法获取字典中的值,如果键不存在则返回默认值。
python 复制代码
print(dictionary.get("z", "Default Value"))
  1. 使用列表的append()extend()方法添加元素。
python 复制代码
lst = [1, 2, 3]
lst.append(4)  # [1, 2, 3, 4]
lst.extend([5, 6])  # [1, 2, 3, 4, 5, 6]
print(lst)
  1. 使用字符串的split()方法分割字符串。
python 复制代码
s = "Hello, World!"
print(s.split(", "))
  1. 使用f-string格式化字符串。
python 复制代码
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
相关推荐
简~7685 分钟前
python for in循环不能遍历整数怎么解决
python·大学生
aiqianji22 分钟前
垂直专业的AI短篇小说写作软件有哪些特点?
人工智能·python
方华世界38 分钟前
企业级Java AI Agent应用平台
java·开发语言·人工智能
aqi0041 分钟前
15天学会AI应用开发(十三)上下文与RAG的阶段性总结
人工智能·python·大模型·ai编程·ai应用
程序员杰哥43 分钟前
接口测试知识总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
大海变好AI1 小时前
专业的智能体算力架构高性价比品牌公司
python
ujainu小1 小时前
原生性能优化:6变量实现高效桥接
开发语言·华为·性能优化·harmonyos
甄同学1 小时前
第十七篇:Bash Executor命令执行器,安全运行Shell命令
开发语言·安全·bash
张3231 小时前
Go语言基础 Map 函数值 闭包
开发语言·golang