【333】Python3.6 格式化文本
看如下例子进行体会:
min_temperature = 0
max_temperature = 300
step = 20
# \t: A tab
print('Fahrenheit\tCelsius')
# We let fahrenheit take the values
# - min_temperature
# - min_temperature + step
# - min_temperature + 2 * step
# - min_temperature + 3 * step
# ...
# up to the largest value smaller than max_temperature + step
for fahrenheit in range(min_temperature, max_temperature + step, step):
celsius = 5 * (fahrenheit - 32) / 9
# {:10d}: fahrenheit as a decimal number in a field of width 10
# {:7.1f}: celsius as a floating point number in a field of width 7
# with 1 digit after the decimal point
print(f'{fahrenheit:10d}\t{celsius:7.1f}')
random.random():返回一个0与1之间的浮点数
random.randint(a, b):返回a与b之间的一个整数
建立随机列表
>>> [10] * 5 [10, 10, 10, 10, 10] >>> [10 for _ in range(5)] [10, 10, 10, 10, 10] >>> [random.randint(1,10)] * 5 [6, 6, 6, 6, 6] >>> [random.randint(1,10) for _ in range(5)] [10, 7, 9, 10, 4]
列表变换
>>> a = [1, 2, 4, 5] >>> b = [str(i) for i in a] >>> b ['1', '2', '4', '5'] >>> c = ["A"+i+"A" for i in b] >>> c ['A1A', 'A2A', 'A4A', 'A5A']
浙公网安备 33010602011771号