- Write a program that takes two numbers and calculates their sum.
num1 = float(input("请输入第一个数:"))
num2 = float(input("请输入第二个数:"))
sum = num1 + num2
print("两个数的和为:", sum)
- Write a program that takes a string as input and outputs that string in reverse order.
string = input("请输入一个字符串:")
reverse_string = string[::-1]
print("倒序输出字符串:", reverse_string)
- Write a program to check whether a number is prime or not.
num = int(input("请输入一个整数:"))
if num > 1:
for i in range(2, int(num/2)+1):
if (num % i) == 0:
print(num, "不是质数")
break
else:
print(num, "是质数")
else:
print(num, "不是质数")
- Write a program that calculates and outputs the first n terms of the Fibonacci sequence (n is input by the user).
n = int(input("请输入要输出的斐波那契数列的项数:"))
fibonacci_list = [0, 1]
for i in range(2, n):
fibonacci_list.append(fibonacci_list[i-1] + fibonacci_list[i-2])
print("斐波那契数列的前", n, "项为:", fibonacci_list)
- Write a program to determine whether a string is a palindrome.
string = input("请输入一个字符串:")
if string == string[::-1]:
print(string, "是回文串")
else:
print(string, "不是回文串")
- Write a program to find the largest and smallest value in a list.
num_list = [10, 5, 8, 2, 15, 3]
max_num = max(num_list)
min_num = min(num_list)
print("列表中的最大值为:", max_num)
print("列表中的最小值为:", min_num)
- Write a program to check if a year is a leap year.
year = int(input("请输入一个年份:"))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, "是闰年")
else:
print(year, "不是闰年")
else:
print(year, "是闰年")
else:
print(year, "不是闰年")
- Write a program that merges two lists into a new list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list1 + list2
print("合并后的列表为:", merged_list)
- Write a program to check whether a number is a perfect square.
import math
num = int(input("请输入一个整数:"))
sqrt = math.isqrt(num)
if sqrt * sqrt == num:
print(num, "是完全平方数")
else:
print(num, "不是完全平方数")
- Write a program that counts the number of occurrences of each character in a string.
string = input("请输入一个字符串:")
char_count = {
}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
print("每个字符出现的次数:")
for char, count in char_count.items():
print(char, ":", count)
These are the code samples for the first 10 practice questions