由于最近期末考,整理一下我的部分python代码
一、输入某年某月某日,判断这一天是这一年的第几天。
# _*_ coding:utf-8 _*_
a = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))
if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
a[2] += 1
for i in range(0,month):
day+= a[i]
print(day)
然后还可以导datatime包,格式化时间
%j:年内的一天(001-366)
import datetime
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日:"))
result = datetime.datetime(year, month, day)
print(result.strftime("%j"))
参考老师的代码如下:
# _*_ coding:utf-8 _*_
import datetime
dtstr = input('Enter th datetime(20120708):')
dt = datetime.datetime.strptime(dtstr,"%Y%m%d")
another_dtstr=dtstr[:4]+'0101'
another_dt=datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(int((dt-another_dt).days)+1)
二、python键盘中输入年龄判断年龄大于等于18为合法成年人,否则为未成年,要求使用断言
断言:assert
# _*_ coding:utf-8 _*_
print("请输入年龄",end="")
age= int(input())
try:
assert age>=18,str(age)+"未成年!"
print(age,"岁是成年人啦!")
except Exception as e:
print(e)
三、统计文件中单词的个数并输出统计结果 (另附其他大佬链接 ,很nice)
# _*_ coding:utf-8 _*_
import re
words = {}
num=0
r = re.compile(r"[,!\*\.]")
with open("g.txt","r",encoding="utf-8") as f:
for line in f:
for word in r.sub("",line.strip()).split(" "):
num = num+1
print(num)
以及还可以打印单词出现次数和不重复单词个数
# _*_ coding:utf-8 _*_
import re
words = {}
r = re.compile(r"[,!\*\.]")
with open("g.txt","r",encoding="utf-8") as f:
for line in f:
for word in r.sub("",line.strip()).split(" "):
if word in words:
words[word] += 1
words.setdefault(word,1)
# 打印每个单词出现的次数
print(words)
# 打印不重复单词个数
print(len(words))
g.txt 参考内容:
It's been a long day without you my friend, And I'll tell you all about it when I see you again, We've come a long way from where we began. Oh I'll tell you all about it when I see you again.
四、 编写函数,接收字符串参数,统计大写字母和小写字母的个数,通过一个元组返回。
# _*_ coding:utf-8 _*_
def gs(st):
dx = 0
xx = 0
for i in st:
if i.islower():
xx += 1
elif i.isupper():
dx += 1
return (dx,xx)
print(gs(input("请输入一串带有大小写字母的字符串:")))
五、定义一个类完成圆的周长和面积的计算,重写构造器:
# _*_ coding:utf-8 _*_
import math
class Circle(object):
def __init__(self,r):
self.r=r
def getC(self):
return 2*math. pi*self.r
def getS(self):
return math.pi * math.pow(self.r, 2)
if __name__ == '__main__':
r=int(input("请输入圆的半径:"))
c=Circle(r)
print("该圆的周长为:{}".format(c.getC()))
print("该圆的面积为:{}".format(c.getS()))
六、从键盘上输入任意一串大小写混合的字符,并将其分别转换成全部大写字母字符串、全部小写字母字符串,然后写入到 “1.txt”文件中。
# -*- coding: utf-8 -*-
s=input()
with open('1.txt','w',encoding="UTF-8") as fp:
fp.writelines("全部小写:"+s.lower()+"\n")
fp.writelines("全部大写:"+s.upper())
七、将文件“4.txt”内容5,3,6,2,4,1读出,进行冒泡排序,并将降序后的结果写到源文件数据下方。
# _*_ coding:utf-8 _*_
def bubble_sort(array):#冒泡排序
for i in range(1, len(array)):
for j in range(0, len(array) - i):
if array[j] < array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
return array
with open('4.txt', 'r',encoding="utf-8") as f:
data = f.read()
array = data.split(",")
array1=bubble_sort(array)
str = ','
with open('4.txt','w',encoding="utf-8") as f:
f.writelines(data)
f.writelines("\n排序后为:\n")
f.write(str.join(array1))
最后,看到的朋友期末考高分!!!