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.")
相关推荐
凯瑟琳.奥古斯特6 小时前
力扣1016高效解法:剪枝优化技巧
开发语言·c++·算法·leetcode·职场和发展
databook6 小时前
时间序列数据中,如何找出缺失值
python·数据分析
坤坤子吖6 小时前
C++继承:友元、静态成员、多继承与组合
开发语言·c++·笔记·学习·1024程序员节
技术小甜甜7 小时前
【Cursor】Skill 被标黄色警告,怎么改成合格的 SKILL.md
开发语言·javascript·ai编程·cursor·1024程序员节·ai agent·skills
小小龙学IT7 小时前
Python Polars 高性能数据处理引擎深度解析
开发语言·python·rust·1024程序员节
csdn_aspnet7 小时前
JavaScript 未排序数组中第 k 个最小/最大元素 (预期线性时间)
python
quantdash_cc7 小时前
如何利用标的信息为研究股票池补充名称和交易所信息?
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
数字化转型分享点滴7 小时前
机加工企业上大数据分析系统有哪些容易踩的坑?
python·1024程序员节
B2_Proxy8 小时前
Scrapy 代理 IP 调度进阶:基于 Redis 与中间件的高可用架构实战
python·scrapy·1024程序员节
在世修行8 小时前
智学文枢:AI 客户端封装
python·http·ai·1024程序员节·pyside