for i in range(100): if i % 2 != 0: print(i)
sum = 0 for i in range(1, 101): sum += i print(sum)
for i in range(2, 101): for j in range(2, i): if i % j == 0: break else: print(i)
a, b = 0, 1 for i in range(20): a, b = b, a + b print(a)
def is_palindrome(num): return str(num) == str(num)[::-1]
def is_palindrome(str): return str == str[::-1]
import random num = random.randint(1, 100) print(str(num)[::-1])
def gcd(a, b): while b != 0: a, b = b, a % b return a
def lcm(a, b): return a * b // gcd(a, b)
a, b = map(int, input().split()) print("和:", a + b) print("差:", a - b) print("积:", a * b) print("商:", a // b) print("余数:", a % b)
def is_perfect(num): factors = [] for i in range(1, num): if num % i == 0: factors.append(i) return sum(factors) == num
num = 0 for i in range(2, 100, 2): num += i if num > 1000: break print(i)
text = "Hello world! I am a Python developer." words = text.split() print(len(words))
list1 = [1, 3, 5, 7] list2 = [2, 4, 6, 8] new_list = sorted(list1 + list2) print(new_list)
list1 = [1, 3, 5, 7] list2 = [2, 4, 6, 8, 1, 7] common_elements = set(list1) & set(list2) print(common_elements)
list1 = [1, 3, 5, 7] list2 = [2, 4, 6, 8, 1, 7] different_elements = set(list1) ^ set(list2) print(different_elements)
list1 = [1, 3, 5, 7] squares = [i**2 for i in list1] print(squares)
dict1 = {'a': 1, 'b': 2, 'c': 3} new_dict1 = {v: k for k, v in dict1.items()} print(new_dict1)
list1 = [1, 2, 3, 2, 1, 4, 5, 4] new_list1 = list(set(list1)) print(new_list1)
list1 = [1, 3, 5, 7, 2, 4, 6, 8] new_list1 = sorted(list1) print(new_list1)
text = "Hello World! I Am A Python Developer." new_text = text.lower().replace(" ", "") print(new_text)
text = "Hello World! I Am A Python Developer." new_text = "" for char in text: if char.isupper(): new_text += char.lower() elif char.islower(): new_text += char.upper() else: new_text += char print(new_text)
from datetime import datetime time1 = datetime.strptime('2018-06-29 08:15:27', '%Y-%m-%d %H:%M:%S') time2 = datetime.strptime('2018-06-29 15:35:16', '%Y-%m-%d %H:%M:%S') diff = time2 - time1 print("小时数: ", diff.seconds // 3600) print("分钟数: ", (diff.seconds // 60) % 60)
def hex_to_rgb(hex): r = int(hex[1:3], 16) g = int(hex[3:5], 16) b = int(hex[5:7], 16) return (r, g, b) print(hex_to_rgb('#FF0000'))
def factorial(num): if num == 0: return 1 else: return num * factorial(num-1) print(factorial(5))