0
点赞
收藏
分享

微信扫一扫

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


题目:

  1. 输出 “Hello, World!”。

print("Hello, World!")

  1. 创建一个变量,并为其赋值,然后输出该变量的值。

x = 10
print(x)

  1. 输入两个数,然后输出它们的和。

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a + b)

  1. 使用if语句判断一个数是奇数还是偶数。

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

  1. 创建一个列表,然后添加、删除元素。

lst = [1, 2, 3]
lst.append(4)  # 添加
lst.remove(2)  # 删除
print(lst)

  1. 使用for循环输出列表中的每一个元素。

for item in lst:
    print(item)

  1. 使用while循环输出从1到10的数字。

i = 1
while i <= 10:
    print(i)
    i += 1

  1. 定义一个函数,该函数接受两个参数并返回它们的和。

def add(a, b):
    return a + b

  1. 使用异常处理来避免除以0的错误。

try:
    x = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")

  1. 使用列表推导创建一个包含10个偶数的列表。

evens = [i for i in range(2, 21, 2)]
print(evens)

  1. 创建一个字典并进行增、删、查、改操作。

dictionary = {"a": 1, "b": 2}
dictionary["c"] = 3  # 增
del dictionary["a"]  # 删
print(dictionary.get("b"))  # 查
dictionary["b"] = 4  # 改
print(dictionary)

  1. 使用Python内置的len()函数计算字符串的长度。

s = "Python"
print(len(s))

  1. 使用切片获取字符串的子串。

print(s[1:4])

  1. 创建一个集合,并进行添加和删除操作。

my_set = {1, 2, 3}
my_set.add(4)  # 添加
my_set.remove(2)  # 删除
print(my_set)

  1. 使用import导入一个模块,并使用其中的函数。(这个例子假设我们导入了math模块)

import math
print(math.sqrt(16))

  1. 创建一个包含多个函数的模块。(这需要在一个.py文件中做,这里只是一个示例)

# my_module.py
def func1():
    pass

def func2():
    pass

  1. 使用lambda表达式定义一个匿名函数。

f = lambda x: x * 2
print(f(5))

  1. 使用filter()lambda输出一个列表中的所有偶数。

nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)

  1. 使用map()lambda将列表中的每个数字乘以2。

doubled = list(map(lambda x: x * 2, nums))
print(doubled)

  1. 使用文件操作写入和读取一个文件。

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关键字定义一个接受不定数量参数的函数。

def print_args(*args):
    for arg in args:
        print(arg)

print_args(1, 2, 3, 4)

  1. 使用列表、元组和字典创建一个复合数据结构。

data = {
    "names": ["Alice", "Bob", "Charlie"],
    "ages": (25, 30, 35),
    "scores": [85, 90, 95]
}
print(data)

  1. 使用enumerate()遍历列表的索引和元素。

for index, value in enumerate(["a", "b", "c"]):
    print(index, value)

  1. 使用zip()合并两个列表。

names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]
combined = list(zip(names, scores))
print(combined)

  1. 使用列表的sort()方法和sorted()函数排序一个列表。

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()方法找到两个集合的交集。

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print(set1.intersection(set2))

  1. 使用字典的get()方法获取字典中的值,如果键不存在则返回默认值。

print(dictionary.get("z", "Default Value"))

  1. 使用列表的append()extend()方法添加元素。

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()方法分割字符串。

s = "Hello, World!"
print(s.split(", "))

  1. 使用f-string格式化字符串。

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")


举报

相关推荐

0 条评论