⼩学python编程题⼤全_python⼩题⽬汇总
1、编程计算两个⽇期之间的天数与周数
dateutil模块主要有两个函数,parser和rrule。
其中parser是根据字符串解析成datetime,⽽rrule则是根据定义的规则来⽣成datetime。
1 importdatetime
2 from dateutil importrrule3
4 classBetweenDate:
5 def __init__(self,start,stop):
6 self.start = datetime.datetime.strptime(start,"%Y,%m,%d")
7 self.stop = datetime.datetime.strptime(stop,"%Y,%m,%d")
8 defBetdays(self):
9 d =
15 time = BetweenDate('2020,1,14','2020,7,17')16 days =time.Betdays()17 weeks =time.Betweeks()18 print('⽇期间隔天数:',days)19 print('⽇期间隔周数:',weeks)20
21 结果:22 ⽇期间隔天数: 186
23 ⽇期间隔周数: 27
View Code
time模块操作归纳
操作
作⽤
linux图形用户界面
例⼦
time.time()
打印当前时间(结果为浮点数)
a = time.time()
结果:1595468499.5463095
time.localtime()
浮点数--->时间结构体
b=time.localtime(a)
结果:time.struct_time(tm_year=2020, tm_mon=7, tm_mday=23, tm_hour=9, tm_min=43, tm_sec=7, tm_wday=3,
tm_yday=205, tm_isdst=0)
time.asctime()
时间结构体--->时间字符串
c=time.asctime(b)
结果:'Thu Jul 23 09:43:07 2020'
time.strftime()
时间结构体--->指定格式时间字符串
d=time.strftime("%Y-%m-%d %H:%M:%S",b)
结果:'2020-07-23 09:43:07'
time.strptime()
时间字符串--->时间结构体
e=time.strptime(d,"%Y-%m-%d %H:%M:%S")
结果:time.struct_time(tm_year=2020, tm_mon=7, tm_mday=23, tm_hour=9, tm_min=43, tm_sec=7, tm_wday=3,
tm_yday=205, tm_isdst=-1)
对于结构体中元素的说明:
tm_year:年
tm_mon:⽉
tm_mday:⽇
tm_hour:⼩时
tm_min:分
tm_sec:秒
tm_yday:⼀周中的索引([0,6],周⼀的索引是0)
tm_day:⼀年中的索引([1,366])
tm_isdist:1 if summer time is in effect, 0 if not, and -1 if unknown
2、编写程序⽤于判断学⽣的作业是否做完
1 classStudent:oracle数据库图书管理系统
2 def __init__(self,name,grade,subject):
3 self.name =name
ade =grade
5 self.subject =subject6
defdo_work(self,time):7 ade>3 and time > 2:8 returnTrue9 ade<3 and time < 0.5:10 returnTrue11 else:12 returnFalse13 classTeacher:14 def __init__(self,name,subject):15 self.name =name16 self.subject =subject17
defevaluate(self,result):18 ifresult:19 return "You are great."
20 else:21 return "You sholud work hard!"
22
23 stu_zhangshan = Student('zhangshan',5,'math')24 tea_wang = Teacher('wang','math')25 resu =
stu_zhangshan.do_work(1)26 tea_said =tea_wang.evaluate(resu)27 print(tea_said)28
29 结果:30 You sholud work hard!
View Code
3、筛选素数
1 importmath
2 defis_prime(m):
3 if m<=1:
4 return '不是素数'
5 for i in range(2,int(math.sqrt(m))+1):
6 if m%i==0:
7 return '不是素数'
8 return '素数'
9
10 def choice_prime(*args):11 primes = [i for i in args if is_prime(i)=='素数']12 returnprimes13
14 print(choice_prime(1,2,3,4,5,6,7,8,9))15
16 结果:17 [2, 3, 5, 7]
View Code
4、使⽤函数求斐波那切数列
1 deffibs(n):
2 result=[0,1]
3 for i in range(n-2):#由于数列前两项已知,故只需求后n-2项即可
4 result.append(result[-1]+result[-2])
5 returnresult6
7 print(fibs(10))8
9 结果:10 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
View Code
使⽤类和迭代器实现斐波那切数列
1 #使⽤类和迭代器实现斐波那切数列
2 classFibs():
3 def __init__(self,max):
4 self.max =max
5 self.a =0
6 self.b = 1
7 def __iter__(self):8 returnself9 def __next__(self):10 fib =self.a11 if fib >self.max:12 raiseStopIteration13 else:14 self.b, self.a = self.a +self.b, self.b15 returnfib16
17 fibs = Fibs(100000) #不会占⽤太多内存空间
18 lst = [fibs.__next__() for i in range(10)]19 print(lst)20 '''
21 结果:22 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]23 '''
View Code少儿python入门教程
使⽤⽣成器实现斐波那切数列
1 deffibs():
2 a,b = 0,1
3 whileTrue:
4 yielda
5 b,a = a +b, b
6 #⽣成⼀个⽣成器
7 f = fibs() #此时已经⽆限多个菲波那切数列预存到⽣成器,使⽤时将所需的元素督导内存即可
8
9 importitertools10 lst = list(itertools.islice(f,10))11 print(lst)12
13 '''
14 结果:15 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]16 '''
View Code
5、输⼊字符串,转成成数字形式求和
eval("34-23") #结果:11
1 s='44+ 34'
2 s=s.strip().split('+')
3 print(s)
4 n=0
5 for i ins:
6 n=n+int(i)
7 print(n)8
9 结果:10 ['44', '34']11 78
View Code
6、对输⼊的字母⼤⼩写进⾏变换,即⼩写转⼤写,⼤写转⼩写
list-->str⽤join()
str-->list表⽤split()
1 deffun(s):
2 #l = []
3 #for i in s:
4 #if i == i.upper():
5 #l.append(i.lower())
6 #else:
7 #l.append(i.upper())
8 l = [i.lower() if i==i.upper() else i.upper() for i ins]9 return "".join(l)10
11 print(fun('Hello'))12
13 结果:14 hELLO
View Code
snmp协议时运行在
与'Hello'.swapcase()作⽤类似,可参见字符串的基本内容
7、提取字符串中的所有元⾳字母
a = ord('a') #结果:97
b = chr(97) #结果:a
1 classKeeper:
2 def __init__(self,keep):
3 self.keep =set(map(ord,keep))
4 def __getitem__(self,n):
5 if n not inself.keep:
6 returnNone
7 else:
8 returnchr(n)
9 def __call__(self,a):anslate(self)11
12 vowels = Keeper("aeiou")13 result = vowels("Cherry is beautiful!")14 print(result) #结果:'eieauiu'
View Code
8、年、⽉的⽇历图,并判断是否为闰年
1 importcalendar
2 from datetime importdate
3 mydate = day() #结果:datetime.date(2020, 7, 22)
4 print(calendar.calendar(2020)) #打印年的⽇历图
5 h)) #打印⽉的⽇历图
6
7 结果:8 年的⽇历图:9 2020
10
11 January February March12 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su13 1 2 3 4 5 1 2 1
14 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
16 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
17 27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
18 30 31
19
20 April May June21 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su22 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
23 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
24 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
25 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
26 27 28 29 30 25 26 27 28 29 30 31 29 30
27
28 July August September29 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su30 1 2 3 4 5 1 2 1 2 3 4 5 6
接口和抽象类的区别c 31 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
32 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
33 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
34 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
35 31
36
注册登录代码复选框
37 October November December38 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su39 1 2 3 4 1 1 2 3 4 5 6
40 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
41 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
42 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
43 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
44 30
45
46
47 ⽉的⽇历图:48 July 2020
49 Mo Tu We Th Fr Sa Su50 1 2 3 4 5
51 6 7 8 9 10 11 12
52 13 14 15 16 17 18 19
53 20 21 22 23 24 25 26
54 27 28 29 30 31
View Code
判断是否是闰年

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。