以下是带有简单库使用的Python基础入门小练习:
from datetime import datetime
now = datetime.now()
print("Current date and time: ", now.strftime("%Y-%m-%d %H:%M:%S"))
import random
num = random.randint(1, 100)
print(num)
from collections import Counter
lst = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
counter = Counter(lst)
print(counter)
num = int(input("输入一个数字: "))
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "是质数")
else:
print(num, "不是质数")
str = "this is a test string for testing purposes"
word_counts = {}
words = str.split()
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
print(word_counts)
lst = [5, 2, 8, 1, 9, 3]
sort_type = input("Enter 'asc' for ascending or 'desc' for descending: ")
if sort_type == 'asc':
sorted_lst = sorted(lst)
elif sort_type == 'desc':
sorted_lst = sorted(lst, reverse=True)
else:
print("Invalid input")
print(sorted_lst)