12/05
1 ''' 2 6.定义一个字符串"python的创始人是Guido",试分别打印出"Guido","odiuG", "ph的> 人uo" 3 ''' 4 s = "python的创始人是Guido" 5 print(s[len(s)-5:len(s)]) 6 print(s[len(s):len(s)-6:-1]) 7 print(s[::3]) 8 9 ''' 10 7.有字符串"你"和"最帅",如何得到字符串"你最帅",又如何得到字符串"你最帅最帅> 最帅" 11 ''' 12 s1 = ("你") 13 s2 = ("最帅") 14 i = s1 + s2 15 print(i) 16 n = i + s2 + s2 17 print(n) 18 ''' 19 2. 像121 11 111等对称的整型数称为回文整型数,随机产生1000以内的10个整型数,打 印其中的回文整型数 20 ''' 21 import random 22 for i in range (0,10): 23 n = random.randint(0,1000) 24 print(n) 25 s = str(n) 26 res = s[::-1] 27 if s == res: 28 print("{}是回文整型数".format(n)) 29 30 ''' 31 4. 公鸡5文钱1只,母鸡3文钱1只,小鸡3只1文钱,用100文钱买100只鸡,可以怎么买> ? 32 ''' 33 for g in range(0,21): 34 for m in range(0,34): 35 for x in range(0,301): 36 if (5*g + 3*m + 1/3*x)== 100 and (g + m + x)== 100: 37 print("公鸡{}只,母鸡{}只,小鸡{}只".format(g,m,x)) 38 ''' 39 5.使用input()读入一个字符串,统计字符串中字母的个数,和数字的个数 40 ''' 41 s = input("写入一个字符串:") 42 a = 0 43 b = 0 44 for i in s: 45 if i > "a"and i < "z"or i> "A"and i< "Z": 46 a += 1 47 elif i in "1234567890": 48 b += 1 49 print("字母的个数是{},数字的个数是{}".format(a,b))
附加:
已知1990.1.1是星期一,用户输入一个1990后的任意年月,计算其1号星期几
参考命令cal 12 2018
1 year,month = eval(input("输入一个年份月份:"))
2 sumd = 0
3 for y in range (1990,year):
4 if y % 4 == 0 and y % 100 != 0 or y % 400 == 0:
5 sumd += 366
6 else:
7 sumd += 365
8 for m in range (1,month):
9 if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or \
10 m == 10 or m == 12:
11 sumd += 31
12 elif m == 4 or m == 6 or m == 9 or m == 11:
13 sumd += 30
14 else:
15 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
16 sumd += 29
17 else:
18 sumd += 28
19 sumd += 1
20 print("{}/{}/1是星期{}".format(year,month,sumd%7))