91时间函数举例1
if __name__ == '__main__':
import time
print(time.ctime(time.time()))
print(time.asctime(time.localtime(time.time())))
print(time.asctime(time.gmtime(time.time())))
Fri Apr 19 13:57:52 2024
Fri Apr 19 13:57:52 2024
Fri Apr 19 05:57:52 2024
time ctime() 函数描述
Python time ctime() 函数把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于 asctime(localtime(secs))。
语法
ctime()方法语法:
time.ctime([ sec ])
参数
sec -- 要转换为字符串时间的秒数。
返回值
该函数没有任何返回值。
Python time asctime()方法
描述
Python time asctime() 函数接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
语法
asctime()方法语法:
time.asctime([t]))
参数
t -- 9个元素的元组或者通过函数 gmtime() 或 localtime() 返回的时间值。
返回值
返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
import time
t = time.localtime()
print "time.asctime(t): %s " % time.asctime(t)
time.asctime(t): Tue Feb 17 09:42:58 2009
time.time() 描述
Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
语法
time()方法语法:
time.time()
参数
NA。
返回值
返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
实例
以下实例展示了 time() 函数的使用方法:
#!/usr/bin/python
import time
print ("time.time(): %f " % time.time())
print (time.localtime( time.time() ))
print( time.asctime( time.localtime(time.time()) ))
以上实例输出结果为:
time.time(): 1234892919.655932
(2009, 2, 17, 10, 48, 39, 1, 48, 0)
Tue Feb 17 10:48:39 2009
92 时间函数举例2
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
import time
start = time.time()
for i in range(3000):
print(i)
end = time.time()
print(end-start) #计算循环打印3000个数字需要的时间
0
1
2
....
2998
2999
0.022022724151611328
93 时间函数举例3
Time.clock()和time.time()一样,都是计算程序运行的时间。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
import time
start = time.clock()
for i in range(10000):
print(i)
end = time.clock()
print ('different is %6.3f' % (end - start))
Traceback (most recent call last):
File "D:\Python Project\practice\ex13.py", line 6, in <module>
start = time.clock()
AttributeError: module 'time' has no attribute 'clock'
Python 中 time.clock() 和 time.time() 的联系和区别-CSDN博客
94 猜数字判断反应快慢
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
import time
import random
play_it = input('do you want to play it.(\'y\' or \'n\')')
while play_it == 'y':
c = input('input a character:\n')
i = random.randint(0, 2 ** 32) % 100 #**代表次方 随机输出一个整数
print('please input number you guess:\n')
start = time.time() #开始的时间
guess = int(input('input your guess:\n')) #输入猜测的数字
while guess != i: #如果猜测的数字不对
if guess > i: # 如果猜的数字大
print('please input a little smaller')
guess = int(input('input your guess:\n'))
else: #如果才的数字小
print('please input a little bigger')
guess = int(input('input your guess:\n'))
end = time.time() #结束的时间
var = (end - start) / 18.2
print(var)
# print 'It took you %6.3 seconds' % time.difftime(b,a))
if var < 15:
print( 'you are very clever!')
elif var < 25:
print('you are normal!')
else:
print('you are stupid!')
print( 'Congradulations')
print('The number you guess is %d' % i)
play_it = input('do you want to play it.')
do you want to play it.('y' or 'n')y
input a character:
5
please input number you guess:
input your guess:
55
please input a little smaller
input your guess:
40
please input a little smaller
input your guess:
30
please input a little smaller
input your guess:
10
please input a little bigger
input your guess:
15
please input a little smaller
input your guess:
13
please input a little smaller
input your guess:
11
1.6078965218512566
you are very clever!
Congradulations
The number you guess is 11
do you want to play it.
95字符串日期转换为易读的日期格式
【Python】Parser 用法-通俗易懂!_python parser-CSDN博客
#对参数进行设置
from dateutil import parser
dt = parser.parse("Aug 28 2015 12:00AM")
print(dt)
2015-08-28 00:00:00
96计算字符串中子串出现的次数
if __name__ == '__main__':
str1 = input('请输入一个字符串:\n')
str2 = input('请输入一个子字符串:\n')
ncount = str1.count(str2)
print(ncount)
请输入一个字符串:
abcdedacde
请输入一个子字符串:
a
2
97从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from sys import stdout
filename = input('输入文件名:\n')
fp = open(filename, "w")
ch = input('输入字符串:\n')
while ch != '#':
fp.write(ch)
stdout.write(ch)
ch = input('')
fp.close()
输入文件名:
runoobfile.txt
输入字符串:
run
run
ooob
ooob#
在.py文件所在的文件夹下,确实是新建了一个txt文本。
98从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。
if __name__ == '__main__':
fp = open('test.txt', 'w')
string = input('please input a string:\n')
string = string.upper() #全部大写
fp.write(string) #写入txt
fp = open('test.txt', 'r') #打开txt,读取
print(fp.read()) #打印txt内容
fp.close() #关闭
please input a string:
abcd
ABCD
99 txt文本内容的合并
有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
import string
fp = open('test1.txt')
a = fp.read()
fp.close()
fp = open('test2.txt')
b = fp.read()
fp.close()
fp = open('test3.txt', 'w')
l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp.write(s)
fp.close()
运行以上程序前,你需要在脚本执行的目录下创建 test1.txt、test2.txt 文件。
以上程序执行成功后,打开 test3.txt 文件可以看到内容如下所示:
1ABCD
100列表转换为字典
i = ['a', 'b']
l = [1, 2]
print(dict([i,l]))
#{'a': 'b', 1: 2}
keys = ['a', 'b']
values = [1, 2]
print({keys[i]: values[i] for i in range(len(keys))})
#{'a': 1, 'b': 2}
r = range(ord('a'), ord('z') + 1)
a = (i for i in r)
b = map(chr, r)
print(dict(zip(a, b)))
'''
{97: 'a', 98: 'b', 99: 'c', 100: 'd', 101: 'e', 102: 'f', 103: 'g', 104: 'h', 105: 'i', 106: 'j', 107: 'k', 108: 'l', 109: 'm', 110: 'n', 111: 'o', 112: 'p', 113: 'q', 114: 'r', 115: 's', 116: 't', 117: 'u', 118: 'v', 119: 'w', 120: 'x', 121: 'y', 122: 'z'}
'''
Python zip() 函数
Python 内置函数 Python 内置函数
描述
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。
如果需要了解 Python3 的应用,可以参考 Python3 zip()。
语法
zip 语法:
zip([iterable, ...])
参数说明:
iterable -- 一个或多个迭代器;
返回值
返回元组列表。
至此,笨办法学python3和python菜鸟教程100例基本过了一遍,现在简单的循环和输入输出函数,列表的调用和字典的调用都接触过了,觉得难的地方是发现问题的数学逻辑关系,如果把逻辑关系转换为代码表达还需要锻炼,下一步多看别人的代码,多结合一些具有实际应用的例子来学习。比如:晶圆图的代码如何实现?
另外,vip文章可能需要原创性高一点,字数多一点,内容多一点,还需要总结的更完美一点