zoukankan      html  css  js  c++  java
  • python第四天

    1.内置函数:有太多了,只列举一些有卵用的

    #!/usr/bin/env python
    import random

    print(chr(90)) #将数字转换成对应的ASCII
    print(ord('A')) #将ASCII换成对应的数字

    l1 = [] #生成6位随机验证码
    for i in range(6):
    r = random.randrange(0,5)
    if r == 2 or r ==4:
    num = random.randrange(0,10)
    l1.append(str(num))
    else:
    temp = random.randrange(65,91)
    c = chr(temp)
    l1.append(c)
    ret = ''.join(l1)
    print(ret)


    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study4/day4/lianxi.py
    Z
    65
    BPVXNE

    Process finished with exit code 0

    #!/usr/bin/env python
    s = 'print("hello world")'
    r = compile(s, '<string>', 'exec') #将字符串编译成python代码
    exec(r) #执行python代码

    s = '8 * 8'
    r = eval(s) #执行表达式,有返回值
    print(r)


    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study4/day4/lianxi.py
    hello world
    64

    Process finished with exit code 0

    #!/usr/bin/env python
    r =divmod(33, 5) #求商及余数
    print(r)

    s = 'abc' #生成hash值,长度固定
    print(hash(s))


    s = 'abc'
    r = isinstance(s, str) #验证s是否为str的实例,对返回True
    print(r)
    r = isinstance(s, dict) #验证s是否为dict的实例,对返回False
    print(r)


    l1 = [10, 20, 30, 40]
    r1 = filter(lambda a: a > 10, l1) #返回值为True,将返回值加入元素
    print(list(r1))

    r2 = map(lambda a: a + 100, l1) #将返回值添加到结果中
    print(list(r2))


    s1 = '萌萌哒'
    print(len(s1)) #计算字符串长度
    b1 = bytes(s1, encoding='gbk') #以gbk格式计算字节长度
    print(len(b1))
    b2 = bytes(s1, encoding='utf-8') #以utf-8格式计算字节长度
    print(len(b2))


    l = [11, 22, 33]
    print(max(l)) #找最大值
    print(min(l)) #找最小值
    print(sum(l)) #求和

    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study4/day4/lianxi.py
    (6, 3)
    488479957
    True
    False
    [20, 30, 40]
    [110, 120, 130, 140]
    3
    6
    9
    33
    11
    66

    Process finished with exit code 0

    #!/usr/bin/env python
    r = round(1.7) #4舍5入
    print(r)
    r = round(1.4)
    print(r)

    l1 = [11, 22, 1, 543] #排序
    l1.sort()
    print(l1)

    l1 = ['A', 423, 452356]
    l2 = ['C', 423, 31561]
    l3 = ['E', 423, 452356]
    r = zip(l1, l2, l3) #对应合并
    t = list(r)[0]
    ret = ''.join(t)
    print(ret)

    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study4/day4/lianxi.py
    2
    1
    [1, 11, 22, 543]
    ACE

    Process finished with exit code 0

    2.装逼器,不对不对,装饰器:语文学的不好,我实在说不清楚,反正我自己心里面清楚就可以了....

    #!/usr/bin/env python
    def outer(func):
    def inner(*args, **kwargs):
    print('A')
    r = func(*args, **kwargs)
    print('B')
    return r
    return inner


    @outer
    def f1(arg):
    print(arg)
    return 'MMD'
    #!/usr/bin/env python
    import zsq
    ret = zsq.f1('FFF')
    print(ret)


    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study4/s2.py
    A
    FFF
    B
    MMD

    Process finished with exit code 0

    看不懂的话就对了,反正我是懂得话说有人看吗

  • 相关阅读:
    js用8421码实现10进制转2进制
    什么?toggle(fn1, fn2)函数在1.9版本jq被移除? 来来来,自己撸一个
    js获取鼠标点击的对象,点击另一个按钮删除该对象
    html5小结
    iphone状态栏高度?
    制作手机相册 全屏滚动插件fullpage.js
    js 相关知识整理(一)
    css 居中问题
    进度条
    @Html.Raw()
  • 原文地址:https://www.cnblogs.com/bfmq/p/5747336.html
Copyright © 2011-2022 走看看