Skip to content

Commit c7ffffd

Browse files
committed
update time
1 parent 4266675 commit c7ffffd

File tree

3 files changed

+294
-1
lines changed

3 files changed

+294
-1
lines changed

basic/11time.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,43 @@
4242

4343
# 将格式字符串转换为时间戳
4444
time_format = "Mon Feb 26 14:10:46 2018"
45-
print(time.mktime(time.strptime(time_format, "%a %b %d %H:%M:%S %Y")))
45+
print(time.mktime(time.strptime(time_format, "%a %b %d %H:%M:%S %Y")))
46+
47+
48+
# 考试题6:time操作
49+
# (1)将字符串的时间"2017-12-31 23:24:00" 转为时间戳和时间元组
50+
# (2)字符串格式更改
51+
# (3)获取两天前的时间
52+
# (4)通过time输出时间
53+
import time
54+
55+
time_format = "2017-12-31 23:24:00"
56+
# (1)转为时间戳
57+
timestamp = time.mktime(time.strptime(time_format, "%Y-%m-%d %H:%M:%S"))
58+
print(timestamp)
59+
# 转为时间元组
60+
localtime = time.localtime(timestamp)
61+
print(localtime)
62+
63+
# (2)格式修改
64+
time_format1 = time_format.replace("12", "10")
65+
time_format1 = time_format1.replace("31", "10")
66+
print(time_format1)
67+
68+
69+
timestamp = time.mktime(time.strptime(time_format1, "%Y-%m-%d %H:%M:%S"))
70+
localtime = time.localtime(timestamp)
71+
time1 = time.strftime("%Y/%m/%d %H-%M-%S", localtime)
72+
print(time1)
73+
74+
# (3)获取前两天的时间
75+
# 使用datetime 模块
76+
import datetime
77+
now_time = datetime.datetime.now()
78+
print(now_time)
79+
80+
yesterday = now_time + datetime.timedelta(days=-2)
81+
print(yesterday)
82+
83+
formated_yesterday = yesterday.strftime("%Y-%m-%d %H:%M:%S")
84+
print(formated_yesterday)

review.py

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Feb 25 13:34:54 2018
4+
5+
@author: Administrator
6+
"""
7+
8+
# 容器的操作
9+
tuple1 = (1, 2, 3)
10+
# 访问
11+
print(tuple1[:])
12+
print(tuple1[0])
13+
del(tuple1[0]) # tuple中元素不可改变
14+
del(tuple1)
15+
16+
str1 = "test"
17+
print(str1[:])
18+
print(str1[0])
19+
del(str1[0])
20+
21+
list1 = [1, 2, 3]
22+
print(list1[:])
23+
print(list1[0])
24+
del(list1[0])
25+
# 添加元素
26+
# 元组不可以被修改,包括添加元素,任何方法都不可以
27+
#tuple1[3] = 4 # 'tuple' object does not support item assignment
28+
#list1.append(5)
29+
30+
#修改元素
31+
#tuple1[0:1] = 0 # 元组切片不可以
32+
#tuple1[0] = 0 # 元组索引不可以
33+
34+
#list1[0:2] = [0] # 列表切片可以
35+
#list1[0] = 0 # 列表索引可以
36+
37+
#str1[0:2] = '0' #不可以
38+
#str1[0] = '0' #不可以
39+
40+
# 小结:
41+
# 1.str、元组、列表可以进行切片操作,可以索引访问
42+
# 2.元组不可以被修改,包括添加元素、修改原有元素;任何方法都不可以(包括切面、索引)
43+
# 3.str不可以被修改,索引、切片都不可以
44+
45+
print("ddd22".isalnum()) # True
46+
print("mm".isalpha()) # True
47+
print("mm334".isalpha()) # False
48+
print([1,2,3].count(4)) # 0 ???
49+
50+
dict1 = {"name":"geyang", "age":24, "height":173}
51+
#dict1["weight"] = 70 # 增
52+
#del(dict1["name"]) # 删
53+
#dict1["weight"] = 80 # 改
54+
#print(dict1["weight"]) # 查
55+
56+
keys = dict1.keys()
57+
print(keys)
58+
values = dict1.values()
59+
print(values)
60+
print(dict1.get("name"))
61+
print(dict1.get("name1")) # get()方法,如果查找键不存在,返回None
62+
items = dict1.items()
63+
print(items)
64+
#dict_items([('name', 'geyang'), ('age', 24), ('height', 173)])
65+
66+
a = 0
67+
while a < 2:
68+
print("a = %d" % a)
69+
a += 1
70+
else:
71+
print("test")
72+
73+
# 小结:
74+
# for 和 while 可以搭配else语句
75+
76+
list2 = [5, 2, 8]
77+
result = list2.sort() # sort()方法没有返回值,直接对list进行排序
78+
print(list2)
79+
print(list2[-1])
80+
81+
#考试题1 小明和女朋友买士力架吃,每天小明吃1个,女朋友吃半个,
82+
# 直到第6天时,剩下1个。问小明买了几个?
83+
# 分析:count(6) = 1 count(5) = count(6) + 1.5 求count(1)
84+
# count(n) = count(n + 1) + 1.5
85+
def count(day):
86+
if day == 6:
87+
return 1
88+
else:
89+
return count(day + 1) + 1.5
90+
91+
92+
print(count(1))
93+
94+
# 考试题2:一次性输入任意不少于5个数字(数字之间以逗号为界)保存在list中,
95+
# 对输入的数字进行排序,不准使用内置函数,封装成函数
96+
def sort_number():
97+
input_str = input()
98+
number_list = input_str.split(",")
99+
# print(number_list)
100+
number_list = [ int(index) for index in number_list]
101+
print("before sort:", number_list)
102+
# 冒泡排序
103+
bubble_sort(number_list)
104+
print("after sort:", number_list)
105+
106+
def bubble_sort(number_list):
107+
for j in range(1, len(number_list)):
108+
for i in range(len(number_list) - j):
109+
if number_list[i] > number_list[i+1]:
110+
# 交换
111+
temp = number_list[i+1]
112+
number_list[i + 1] = number_list[i]
113+
number_list[i] = temp
114+
115+
# 测试
116+
#number_list = [5, 3, 8, 1]
117+
#bubble_sort(number_list)
118+
119+
sort_number()
120+
121+
# 考试3:切割列表元素,如果相邻元素相同,则切割存放在一起,不同则单独切割
122+
# 封装为函数
123+
def slice_list():
124+
temp_list = []
125+
result_list = []
126+
index = 0
127+
input_list = [1, 1, 0, 2, 2, 2, 4, 3, 3, 4, 2, 0, 0]
128+
129+
while True:
130+
if index == len(input_list):
131+
break
132+
while input_list[index] == input_list[index+1]:
133+
temp_list.append(input_list[index])
134+
index += 1
135+
temp_list.append(index)
136+
result_list.append(temp_list)
137+
index += 1
138+
139+
140+
slice_list()
141+
142+
# 考试题4:写一个程序,可以任意输入文件名,打开该文件,如果文件不存在则创建该文件。
143+
# 然后输入内容,可重复输入追加到文件中。
144+
145+
# 考试题5:计算年龄
146+
#f(1) = 10 f(2) = f(1) + 2 f(3) = f(2) + 2
147+
def count_age(number):
148+
if number == 1:
149+
return 10
150+
else:
151+
return count_age(number -1) + 2
152+
153+
print(count_age(5))
154+
155+
# 考试题6:time操作
156+
# (1)将字符串的时间"2017-12-31 23:24:00" 转为时间戳和时间元组
157+
# (2)字符串格式更改
158+
# (3)获取两天前的时间
159+
# (4)通过time输出时间
160+
import time
161+
162+
time_format = "2017-12-31 23:24:00"
163+
# (1)转为时间戳
164+
timestamp = time.mktime(time.strptime(time_format, "%Y-%m-%d %H:%M:%S"))
165+
print(timestamp)
166+
# 转为时间元组
167+
localtime = time.localtime(timestamp)
168+
print(localtime)
169+
170+
# (2)格式修改
171+
time_format1 = time_format.replace("12", "10")
172+
time_format1 = time_format1.replace("31", "10")
173+
print(time_format1)
174+
175+
176+
timestamp = time.mktime(time.strptime(time_format1, "%Y-%m-%d %H:%M:%S"))
177+
localtime = time.localtime(timestamp)
178+
time1 = time.strftime("%Y/%m/%d %H-%M-%S", localtime)
179+
print(time1)
180+
181+
# (3)获取前两天的时间
182+
# 使用datetime 模块
183+
import datetime
184+
now_time = datetime.datetime.now()
185+
print(now_time)
186+
187+
yesterday = now_time + datetime.timedelta(days=-2)
188+
print(yesterday)
189+
190+
formated_yesterday = yesterday.strftime("%Y-%m-%d %H:%M:%S")
191+
print(formated_yesterday)
192+
193+
# (4)通过time输出时间
194+
time_format_4 = "2018-01-01 08:08:08"
195+
timestamp4 = time.mktime(time.strptime(time_format_4, "%Y-%m-%d %H:%M:%S"))
196+
localtime4 = time.localtime(timestamp4)
197+
print(localtime4)
198+
time4 = time.strftime("今天是%Y年%m月%d日 %H点%M分%S秒", localtime4)
199+
print(time4)
200+
201+
#考试题7 定义一个班级类
202+
class Class(object):
203+
204+
__person_number = 0
205+
206+
def __init__(self, person_number):
207+
Class.__person_number = person_number
208+
209+
def increase_person_number(self):
210+
pass
211+
212+
def get_peson_number(self):
213+
return Class.__person_number
214+
215+
class Student(Class):
216+
217+
def __init__(self, name, age, score):
218+
self.name = name
219+
self.age = age
220+
self.score = score
221+
222+
def get_name(self):
223+
return self.name
224+
225+
def get_age(self):
226+
return self.age
227+
228+
def get_score(self, class_name):
229+
if class_name in self.score:
230+
return "%d:%d" % (class_name, self.score[class_name])

temp.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Feb 12 00:26:01 2018
4+
5+
@author: Administrator
6+
"""
7+
8+
def countOff(n, m, out):
9+
out = []
10+
for index in range(n):
11+
out.append(0)
12+
# print(out)
13+
location = 0
14+
# 遍历序号,给元素表序号
15+
for index in range(1, n+1):
16+
# 数m个,标一个序号
17+
number = 1
18+
while number < m:
19+
if out[location] == 0:
20+
number += 1
21+
location = (location + 1) % n
22+
out[location] = index
23+
print(out)
24+
countOff(11, 3, [])

0 commit comments

Comments
 (0)