Python函数

一、数值运算模块

  1.math

    (1)print(math.ceil(1.2))   #向上取整

    (2)print(math.floor(1.2))     #向下取整

    (3)print(math.round(1.6))#四舍五入

    (4)print (math.pow(2,3))  #x的y次方

    (5)print(math.max(1,2,3,4,5))  #  取最大值

    (6)print(math.min(1,2,3,4,5))#取最小值

二、随机数

  1.random

    (1)print(random.choice([1,2,3,4]))  #随机某一个值,列表;    print(random.choice(‘abcdefg’))   #随机某一个值字符串

    (2)print (random.choices(‘abcdef’,k=2))    #随机多个值,若不给k值,默认k=1

    (3)print(random.randrange(1,3))  #返回一个范围的值,包头不包尾。

    (4)random.shuffle(list)   #  打乱给定list的值

    (5)print(random.randint(1,3))   #打印随机的值

三、字符串

  eg:str=“我喜欢学习”

    print(str[0])   #我

    print(str[-1])  #  习

    print (str[0:3])  #我喜

    print (str[0:]) #我喜欢学习

    print (str [:3])  #我喜

    print (str [: : 1])  #步长默认为1, 我喜欢学习

    print (str [: : 2])  # 更改步长为 2  我欢习

    print(str [-1::-1])   #倒着输出所有值

原文地址:https://www.cnblogs.com/xp0813/p/12833183.html