• python 用到的函数记录


    1.time.ctime() 获取当前的时间

    2.产生随机数字的方法

    import  random

    random.randint(0,99)

     随机产生0到99之间的数值 (包含0和99) (整数!!)

    3.往列表添加数值  

    list.append()

     list.extend() 插到列表最后

    list.insert(位置,字符) 字符插到某个位置

    >>> list1
    ['a', 'b', 'c', 'd']
    >>> list1.insert(1,'x')
    >>> list1
    ['a', 'x', 'b', 'c', 'd']

    >>> list1=['a','b']
    >>> list1.append('c')
    >>> list1
    ['a', 'b', 'c']

    参考:https://www.cnblogs.com/chaofn/p/4592258.html (还有删除列表)

    4.range( )

    产生列表  range(1,101)产生1到100的列表    range(101)产生0到100的列表

    range(101,1,-1) 加-1的话 可以反过来生产 101 到2的数组

    5.eval()函数

    是把原来要先执行的一句话,先进行拼接再执行

    用法实例见 selenium系列 --更加灵活的编写控制层的方法__结合eval函数

    http://www.cnblogs.com/kaibindirver/p/8401350.html

    6.  sort()函数    列表数值排序

     由数字大到小,再到字母a到z

    7.  execfile() 函数    可以用来执行一个文件。

    execfile('hello.py')

    8.判断数值的奇偶方法

    num = int(input("输入一个数字: "))

    if (num % 2) == 0:

      print("{0} 是偶数".format(num))

    else:

      print("{0} 是奇数".format(num))

    9.让列表里的每个字符合并成一个字符

    ls1=[1,2,3,4]

    ls2=[str(i) for i in ls1]

    ls3=''.join(ls2) #''里面可以加其他字符让他们拼接在一起

    10.让字符翻转

    a='hello,word'
    l=list(a)
    l.reverse()
    result = "".join(l)
    print result

  • 相关阅读:
    Randomization Tests
    关于Spring中的<context:annotationconfig/>配置
    PUT method support
    在对话框picture control中利用opengl进行绘图
    【学习笔记】《卓有成效的管理者》 第三章 我能贡献什么
    程序员的黄金时代
    nginx webdav配置
    iphone4s 如何强制关机
    并查集
    实战虚拟化存储设计之三MultiPathing
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/8038048.html
Copyright © 2020-2023  润新知