zoukankan      html  css  js  c++  java
  • 【333】Python3.6 格式化文本

    看如下例子进行体会:

    min_temperature = 0
    max_temperature = 300
    step = 20
    
    # 	: A tab
    print('Fahrenheit	Celsius')
    # 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}	{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']
    
  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/9638454.html
Copyright © 2011-2022 走看看