• pythonic奇淫技巧收集


    1#链式比较
    a=3
    b=1
    print(1<=b<=a<10)
    2#真值测试:0、''、None、False、空容器[](){}为假
    name = 'Tim'
    langs = ['AS3', 'Lua', 'C']
    info = {'name': 'Tim', 'sex': 'Male', 'age':23 }
    if name and langs and info:
    print('All True!') #All True!
    3#包含和迭代
    name = 'Safe Hammad'
    if 'H' in name:
    print('This name has an H in it!')
    pets = ['Dog', 'Cat', 'Hamster']
    for pet in pets:
    print('A', pet, 'can be very cute!')
    4#字符串反转:str(::-1)
    5#连接:字符串列表
    strList = ["Python", "is", "good"]
    res = ' '.join(strList)
    6#EAFP优于LBYL
    7#列表求和,最大值,最小值,乘积
    numList = [1,2,3,4,5]
    sum = sum(numList)
    maxNum = max(numList)
    minNum = min(numList)
    from operator import mul
    from functools import reduce
    prod = reduce(mul, numList, 1)

    8#各种推导式

    9#while...else和for...else语句

    10# 三元符的替代
    a=3
    b=2 if a>2 else 1
    11#enumerate

    12#zip

    13#with

    14#替换
    a,b=b,a
    15#分解
    lis = ['David', 'Pythonista', '+1-514-555-1234']
    first_name, last_name, phone_number = lis
    16#in替代or
    fruit="berry"
    if fruit in("apple","orange"):pass
    #相当于if fruit == "apple" or fruit == "orange":paas
    17#字典setdefault方法
    18#product
    # from itertools import product
    # for x, y, z in product(x_list, y_list, z_list):pass
    ##相当于
    # for x in x_list:
    # for y in y_list:
    # for z in z_list:
    # paas

    19#yeild
    20#魔法函数
  • 相关阅读:
    Windows系统自带工具的 cmd 命令
    阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+部分答案)
    小米3刷机说明
    第3章 常用运算符
    第1章Java入门体验
    jQuery表单验证案例
    jQuery超链接提示,提示跟随鼠标动
    [转载]我的Java后端书架 (2016年暖冬4.0版)
    PHP代码重用与函数编写
    PHP数组操作
  • 原文地址:https://www.cnblogs.com/szmcn/p/15871425.html
Copyright © 2020-2023  润新知