alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【333】Python3.6 格式化文本

看如下例子进行体会:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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之间的一个整数

建立随机列表

1
2
3
4
5
6
7
8
9
10
11
>>> [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]

列表变换

1
2
3
4
5
6
7
8
9
>>> 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']

 

posted on   McDelfino  阅读(217)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示