#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 (含1和10)
print( random.random() ) # 产生 0 到 1 之间的随机浮点数
print( random.uniform(1.1,5.4) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.choice('tomorrow') ) # 从序列中随机选取一个元素
print( random.randrange(1,100,2) ) # 生成从1到100的间隔为2的随机整数
a=[1,3,5,6,7] # 将序列a中的元素顺序打乱
random.shuffle(a)
print(a)
with open("文件路径", “读写方式(rb)”) as f:
f.write("字符串")
a = range(5)
a = [2*x for x in a]
在文件头申明utf-8:# -- coding: UTF-8 -- 或者 #coding=utf-8
import math
a = math.sin(1)
b = math.pi # 3.14159628.....
a = ["a", "b", "c"]
b = [1, 2, 3]
c = "abc"
with open("./test.txt", 'r') as f:
f.write(c) //只能为字符串
f.writelines(a)
f.writelines(c) //可为字符串或字符序列
# f.writelines(b)会报错
import time
localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/
https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20
a = u'\xa0' // 不可直接写入文件否则报错
a.encode("utf-8")
f.open("./test.txt")
f.write(a)