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.")
相关推荐
卷无止境2 小时前
智能体开发环境ADE浅析,编程工具的下一次范式跃迁
后端·python
彧azz5 小时前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php
嵌入式学习菌5 小时前
Modbus‑RTU 数据类型分析
开发语言·单片机·bug
matlab代码6 小时前
基于matlab数字图像处理的指纹识别系统【源码68期】
开发语言·matlab
2601_962300816 小时前
机器学习贴士:使用Python编写MapReduce
hadoop·python·机器学习·mapreduce·数据处理
matlab代码6 小时前
基于matlab的水果识别系统(苹果香蕉菠萝梨桃子)【源码65期】
开发语言·matlab
xyz_CDragon6 小时前
GitHub上4个爆款AI开源Skill:拍照后不用P图,用Codex一键生成高级海报(附效果图+使用教程)
人工智能·python·github·codex·skill
wuyk5557 小时前
从零吃透 MQTT 通信|第 8 章 FreeRTOS 多任务架构下 MQTT 工程架构,任务拆分、队列解耦、临界区保护
c语言·开发语言·stm32·学习·架构
weixin199701080167 小时前
[特殊字符]《跨境二手ERP对接Back Market:标准化API + 7~10工作日技术合规审核实录》(附Python源码)
开发语言·python·pandas
尘客-追梦7 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt