欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

python

python 教程 有大用 有大大用

6种打包Python代码的方法,让你的程序变成exe应用! 有大用

Python是一种高级编程语言,它具有易学易用、跨平台等优点,因此在开发中得到了广泛的应用。

普通分类: 

developer-tools 1.0.7 开发者工具 安装 有大用 有大大用

普通分类: 

vscode vscode在开发python时 Auto Import 失效(代码不自动提示)有大用

首次使用Python开发的时候,发现

普通分类: 

Python try except else(异常处理)用法详解 有大用 有大大用

Python 的异常处理机制可以让程序具有极好的容错性,让程序更加健壮。

普通分类: 

python随机数(random) 有大用

需要导入的库:

import random
import string
  • 1

  • 2

普通分类: 

pip命令使用详解 有大用

pip很像CentOS系统中的yum命令,用于安装及维护Python包。

普通分类: 

async/await 廖雪峰

async/await

阅读: 6846267

普通分类: 

python中items =[[x, y]for (y, x) in pairs]是什么意思

    普通分类: 

    马哥 今日小技巧 关键代码可以依赖于扩展包 有大用

    普通分类: 

    马哥 今日小技巧 【Python小技巧】如何将一串大写字符转化成小写? 有大用

    普通分类: 

    马哥 今日小技巧 优化循环 有大用

    普通分类: 

    马哥 今日小技巧 python 使用新版本 有大用

    任何一个在线上搜索Python资料的人都会发现无数关于Python版本迁移的信息。通常,Python每一个版本都针对之前的一个版本做了优化和改进,以让Python运行的更快。限制因素是你喜欢的函数库是否也针对Python的新版本做了改进。

    普通分类: 

    马哥 今日小技巧 itertools生成排列 有大用



    # itertools生成排列


    # itertools.permutations() generates permutations 


    # for an iterable. Time to brute-force those passwords ;-)


    >>> import itertools


    >>> for p in itertools.permutations('ABCD'):


    ...     print(p)


    ('A', 'B', 'C', 'D')


    ('A', 'B', 'D', 'C')


    ('A', 'C', 'B', 'D')


    ('A', 'C', 'D', 'B')


    ('A', 'D', 'B', 'C')


    ('A', 'D', 'C', 'B')

    普通分类: 

    马哥 今日小技巧 使用dis查看python虚拟机中字节码 有大用

    # You can use Python's built-in "dis"

    # module to disassemble functions and

    # inspect their CPython VM bytecode:


    >>> def greet(name):

    ...     return 'Hello, ' + name + '!'


    >>> greet('Dan')

    'Hello, Dan!'


    >>> import dis

    >>> dis.dis(greet)

    2   0 LOAD_CONST     1 ('Hello, ')

        2 LOAD_FAST      0 (name)

        4 BINARY_ADD

        6 LOAD_CONST     2 ('!')

        8 BINARY_ADD

       10 RETURN_VALUE


    普通分类: 

    马哥 今日小技巧 字典中get方法设置默认值 有大用

     字典中get方法设置默认值



    # The get() method on dicts

    # and its "default" argument


    name_for_userid = {

        382: "Alice",

        590: "Bob",

        951: "Dilbert",

    }


    def greeting(userid):

        return "Hi %s!" % name_for_userid.get(userid, "there")


    >>> greeting(382)

    "Hi Alice!"


    >>> greeting(333333)

    "Hi there!"


    普通分类: 

    马哥 今日小技巧 类方法和静态方法 实例方法差异 有大用

    # @classmethod vs @staticmethod vs "plain" methods

    # What's the difference?


    class MyClass:

        def method(self):

            """

            Instance methods need a class instance and

            can access the instance through `self`.

            """

            return 'instance method called', self


        @classmethod

        def classmethod(cls):

            """

            Class methods don't need a class instance.

            They can't access the instance (self) but

    普通分类: 

    马哥 今日小技巧 s = "ajldjlajfdljfddd",去重并从小到大排序输出"adfjl" 有大用

    s = "ajldjlajfdljfddd",去重并从小到大排序输出"adfjl"

    先使用set去重特性,然后排序,再通过join方法连接成字符串,代码如下


    s = "ajldjlajfdljfddd"

    s = set(s)

    s = sorted(s)

    print(‘’.join(s))



    来自  http://ke.magedu.com/article/112

    普通分类: 

    马哥 今日小技巧 按键值升序排序 通过sorted结合key参数排序 有大用

    dic={"name":"zs","age":18,"city":"深圳","tel":"1362626627"},按键值升序排序


    通过sorted结合key参数排序,再重新组成字典


    dic = {"name":"zs","age":18,"city":"深圳","tel":"1362626627"}


    tmp = sorted(dic.items(), key=lambda x: x[0])


    print(dict(tmp))


    来自  http://ke.magedu.com/article/113

    普通分类: 

    马哥 今日小技巧 Python的try语句中except、else和finally的区别 有大用

    except 为try语句块内发生异常时执行


    else 为try语句块内未发生异常时执行


    finally 不管是否有异常发生都会执行


    来自  http://ke.magedu.com/article/115?tdsourcetag=s_pctim_aiomsg

    普通分类: 

    马哥 今日小技巧 运用python切片技巧 有大用

    # You can clear all elements from a list:

    >>> lst = [1, 2, 3, 4, 5]

    >>> del lst[:]

    >>> lst

    []


    # You can replace all elements of a list

    # without creating a new list object:

    >>> a = lst

    >>> lst[:] = [7, 8, 9]

    >>> lst

    [7, 8, 9]

    >>> a

    [7, 8, 9]

    >>> a is lst

    True


    # You can also create a (shallow) copy of a list:

    >>> b = lst[:]

    >>> b

    [7, 8, 9]

    >>> b is lst

    普通分类: 

    马哥 今日小技巧 按value排序字典 有大用

    按value排序字典

    Python的内置字典数据类型是无序的,而key可以被用来获取对应的value。有时我们需要根据value对字典中的item进行排序输出。方法如下所示:


    方法一:用sorted函数排序,其中key参数是lamda表达式。

    a = {'a':100,'b':20, 'c':150, 'd':1}

    print(sorted(a.items(), key=lambda x:x[1]))


    方法二:用operator.itemgetter而不是lamda表达式进行排序。

    from operator import itemgetter

    print(sorted(a.items(), key=itemgetter[1]))


    方法三:如果只需得到排序后的key,可用.get

    print(sorted(a, key=a.get()))       

    更多历史小技巧,技术交流,岗位内推访问【马哥社区 club.magedu.com】


    普通分类: 

    马哥 今日小技巧 Python3.8的functools中新增了叫cached_property的装饰器 有大用

    Python3.8的functools中新增了叫cached_property的装饰器,看名字应该大概知道什么作用了,看以下例子:

    from functors import cached_property

    class A:

    @cached_property

    def say(self):

    print(‘****’)

    return ‘hello’


    a = A()

    a.say

    以上例子只有第一次访问say属性时才会打印****


    普通分类: 

    马哥 今日小技巧 Python中的is和==的区别 有大用

    Python中的is和==的区别,is时检查对象的标识符是否一致,也就是比较两个对象在内存中的地址是否一样,而==用来检查两个对象是否相等。例如在判断a is b时 相当于在判断id(a) == id(b),a == b 则是调用对象的__eq__方法,相当于a.__eq__(b)。

    linux:

    Nginx Method Limit

    [xxx@xx conf]$ cat http_method_limit.conf

    if ($request_method = PUT ) {

        return 403;

    }


    if ($request_method = TRACE ) {

        return 403;

    }


    if ($request_method = DELETE ) {

        return 403;

    }


    普通分类: 

    马哥 今日小技巧 通过zip函数同时迭代多个序列,其可迭代次数取决于长度最小的序列。 有大用

    通过zip函数同时迭代多个序列,其可迭代次数取决于长度最小的序列。

    for a, b in zip(['a', 'b', 'c'], [1, 2, 3]):

        print(a, b)

        

    输出结果:

    a 1

    b 2

    c 3

    linux:

    Nginx Upload Deny

    [xx@xxx conf]$ cat deny_uploads.conf

    location /uploads/ {

        location ~ .*\.(php|php5)?$ {

            deny all;

        }

    }


    普通分类: 

    马哥 今日小技巧 通过itertools.chain连续迭代多个序列,看以下例子 有大用

    通过itertools.chain连续迭代多个序列,看以下例子:

    from itertools import chain

    a = ['a', 'b', 'c']

    b = [1, 2, 3]


    for x in chain(a, b):

        print(x)


    输出结果:

    a

    b

    c

    1

    2

    3





    普通分类: 

    利用MD5对字符串和文件进行加密

    普通分类: 

    计算文件的md5值

    利用Python计算文件MD5值(从前台上传一个文件,后台计算MD5值后,返给前端)

    普通分类: 

    python

    image.png

    普通分类: 

    [Python]新手写爬虫全过程(已完成)

    普通分类: 

    页面

    Subscribe to RSS - python