• 【终极版】刷完这100行Python,从新人变成大佬


    基础入门

    1 python
    即在命令行输入python,进入Python开发环境

    2 x = 1+2*3-4/5+6**2
    加减乘除四则混合运算,可当作计算器使用,其中**表示乘方。

    3 print(x)
    输出x的值,如果感觉麻烦,可以直接输入x,然后回车,也能看到x的值。

    4 if x>5 : print(x)
    简单的判断,如果x>5,则打印x

    5 for i in range(10): print(i)
    简单的循环,其中range(10)表示创建一个可迭代的自然序列,range(10)表示0,1,2...9

    6 'hello '+"world"
    python中可用单引号或双引号表示字符串,+可以拼接两个字符串。

    7 def addOne(x):return x+1
    python中通过def来声明函数,函数名称和函数主体之间用:分隔,声明上式之后可以直接在命令行中调用。

    >>> def addOne(x):return x+1
    ...
    >>> addOne(1)
    2
    
    • 1
    • 2
    • 3
    • 4

    8 x = [1,2,'abc',3,2]
    []可创建一个列表,列表中的成员可以为任意数据类型。

    >>> x = [1,2,'abc',3,2]
    >>> x
    [1, 2, 'abc', 3, 2]
    
    • 1
    • 2
    • 3

    9 x[0]
    通过方括号和冒号可对列表进行索引,列表的索引值从0开始。

    >>> x[0]
    1
    
    • 1
    • 2

    10 y = set(x)

    set为集合,集合中不允许存在相同的元素,所以将一个列表转成集合之后,会删除列表中的重复元素。

    >>> y = set(x)
    >>> y
    {1, 2, 3, 'abc'}
    
    • 1
    • 2
    • 3

    菜鸟提升

    11 import antigravity

    import用于导入Python模块,antigravity是一个彩蛋性质的模块,导入之后会打开一个反重力漫画

    在这里插入图片描述

    12 pip install numpy
    命令行中运行pip命令,可安装相应的模块,然后在Python中输入import numpy as np,就可以导入numpy包,并给与其np的标识,从而可用np.来调用numpy中的函数。

    13 x = np.arange(10)
    生成一个自然序列,与range相似,但是np.arange得到的可进行运算的数组(array)。

    >>> import numpy as np
    >>> x = np.arange(10)
    >>> x
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    • 1
    • 2
    • 3
    • 4

    14 x**2
    只是演示一下,array可以使用运算符。

    >>> x**2
    array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81], dtype=int32)
    
    • 1
    • 2

    15 x.tolist()

    将x从array转成list,由于列表(list)并不支持乘方运算,所以下面第二行代码报了错。

    >>> x.tolist()
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x.tolist()**2
    Traceback (most recent call last):
      File "", line 1, in <module>
    TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    16-18

    >>> if len(x)==5:print(x)
    ... elif len(x)==10: print(x.tolist()+x)
    ... else: print(x[0])
    ...
    [ 0  2  4  6  8 10 12 14 16 18]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    len表示获取x的长度,python用==来判断二者是否相等。上式表示,如果x的长度等于5,则打印x;或者x的长度为10,则打印x.tolist()+x;如果x的长度为其他值,则打印x[0]

    由于x的长度是10,所以执行了第2行代码。而且python非常智能地按照array的规则计算了x.tolist()+x。这说明,当表达式中同时存在arraylist的时候,python会自动将list转为array

    19-20

    >>> d = {"a":1,"b":2,"c":3}
    >>> d["a"]
    1
    
    • 1
    • 2
    • 3

    d即为字典,可通过键值对的形式进行索引。案例中,"a","b","c"为键(key),1,2,3为值(value),通过key来索引value,非常便利。

    基础晋级

    21 a = 1,2,3

    逗号分隔的变量会默认组成元组,元组会根据等号左边变量的个数来进行赋值。

    >>> a = 1,2,3
    >>> a
    (1, 2, 3)
    
    • 1
    • 2
    • 3

    22 a,b = 1,2

    元组可以通过元素对应的位置来进行一一赋值,由此而带来的便利就是可以更快速地交换两个变量的值。

    >>> a,b = 1,2
    >>> print(a,b)
    1 2
    >>> b,a = a,b
    >>> print(a,b)
    2 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    23 print(f"a={a}")

    在python中,字符串前面可有四种前缀,其中f代表字符串格式化,即format,在f字符串中,大括号内部会自动转换为变量。

    >>> print(f"a={a}")     # a是在22中定义的
    a=2
    
    • 1
    • 2

    24 a = False if a==2 else True

    在Python中,FalseTrue为bool型的两个值。

    在python中,可通过if...else构成三元表达式,上式可等价为响应的C语言a = a==2 ? 0 : 1

    >>> a = False if a==2 else True
    >>> a
    False
    
    • 1
    • 2
    • 3

    25 x = [i for i in range(10)]

    在python中,可通过for循环来创建元组、列表以及字典。

    >>> x = [i for i in range(10)]
    >>> x
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    • 1
    • 2
    • 3

    26-30 函数封装

    def fac(n):
        if n == 0:
            return 1
        else:
            return n*fac(n-1) 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这是一个阶乘函数。在pyhton中,代码块以空格的形式存在。

    31 conda activate
    anaconda内置了Python中许多科学计算模块,用了之后非常省心,而anaconda中默认提供了conda环境,通过conda activate可激活基础Python环境。相应地,退出环境的命令为conda deactivate

    32 conda create -n py311 python=3.11
    此为conda创建python环境的语句,create -n表示创建一个新的环境,py311为新环境的名字,python=3.11表示新环境的python版本为3.11

    33 jupyter notebook
    这行代码用于开启jupyter notebook服务,jupyter notebook是一个交互式笔记本,可以像写文档一样敲代码。

    34 for i,x in enumerate(xs, N): print(i,x)\

    enumerate可以逐次返回由列表索引和元素构成的元组,其中N表示起始序号,默认为0,效果如下

    >>> xs = np.arange(5)
    >>> for i,x in enumerate(xs, 1) : 
    ...     print(i,x)
    ...
    1 0
    2 1
    3 2
    4 3
    5 4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    35 [*a,*b]

    *可以取出列表中的元素,所以[*a,*b]可以合并列表。

    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> [*a,*b]
    [1, 2, 3, 4, 5, 6]
    
    • 1
    • 2
    • 3
    • 4

    星号索引的用途不止于此,在函数传参时也有意想不到的后果

    >>> def addd(a,b,c):
    ...     return a+b+c
    ...
    >>> addd(*a)
    6
    
    • 1
    • 2
    • 3
    • 4
    • 5

    36 {**a,**b}

    双星号可以取出字典中的元素,实现字典合并的功能。

    >>> a = {"b":1,"c":2}
    >>> b = {"d":3,"e":4}
    >>> {**a,**b}
    {'b': 1, 'c': 2, 'd': 3, 'e': 4}
    
    • 1
    • 2
    • 3
    • 4

    37 s == s[::-1]

    python支持:索引语法,a:b指的是从a到b的数据;a:b:c表示从a到b,间隔为c的数据。

    据此,可以得到::-1表示从头到尾,间隔为-1,换言之可将字符串或列表颠倒过来,据此可以判断一个字符串是否为回文结构。

    38 for i,j in zip(xs, ys): print(i,j)

    zip像拉链一样将数组中对应的值缝合起来,根据这个特性,可以达到同步打印xs, ys中元素的效果

    >>> xs = np.arange(5)
    >>> ys = np.arange(5,0,-1)
    >>> for i,j in zip(xs, ys): 
    ...     print(i,j)
    ...
    0 5
    1 4
    2 3
    3 2
    4 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    39 for i,j in product(xs, ys):print(i,j)

    productitertools中的迭代工具,可以实现xsys中元素的排列组合,这行代码等价为

    for i in xs:
        for j in ys:
            print(i,j)
    
    • 1
    • 2
    • 3

    效果为

    >>> from itertools import product
    >>> for i,j in product(xs, ys):print(i,j)
    ...
    0 5
    0 4
    0 3
    0 2
    0 1
    # 太长就不粘贴了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    40 fac = lambda n : 1 if n==0 else n*fac(n-1)

    这同样是一个阶乘算法,与26-30表示的是同一个函数。此即lambda表达式,可以更加方便地创建函数。

    41 a

    比较运算符可以像普通运算符一样链式使用,但其运算顺序类似于“并行”,对于类似a的代码,其结果等价于(a

    >>> 1<2<3<4>3>2
    True
    >>> 3<5<4
    False
    
    • 1
    • 2
    • 3
    • 4

    42 max(set(lst),key=lst.count)

    通过调用list中的count方法,来统计元素个数,然后找到个数最多的元素所对应的值,用这种方法得到的是众数。

    >>> lst = [1,2,3,4,5,2,3,2,1,2,4]
    >>> max(set(lst),key=lst.count)
    2
    
    • 1
    • 2
    • 3

    43 迭代器 next(gen)

    一般来说,初学python很快就会接触推导式,但对其内涵并不理解,实际上for ... in语句生成的是一个迭代器,可通过next来逐个调用迭代器中的值

    >>> gen = (x**2 for x in range(10))
    >>> next(gen)
    0
    >>> next(gen)
    1
    >>> next(gen)
    4
    >>> list(gen)
    [9, 16, 25, 36, 49, 64, 81]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    44 list(set(A).difference(B))

    集合set提供了类似做差集的操作difference。对于集合AB而言,若想实现A-B的功能,可采取A.difference(B)的方案。

    对于其他数据结构,可先转为set再行做差。且difference会自动将输入变量转为set

    >>> A = [1,2,3]
    >>> B = [1,2,4]
    >>> list(set(A).difference(B))
    [3]
    
    • 1
    • 2
    • 3
    • 4

    45 list(filter(lambda x : x%2==0, [1,2,3,4,5]))

    filter是python的内置函数,用于过滤不符合条件的数据。list(filter(func,myList))等价于[e for e in myList if func(e)==1]

    >>> list(filter(lambda x : x%2==0, [1,2,3,4,5]))
    [2, 4]
    
    • 1
    • 2

    46 "python".title()

    title()是字符串内置方法,可以将每个词进行首字母大写。

    "python".title()
    # 'Python'
    
    • 1
    • 2

    此外,字符串还支持.lower.upper函数,可以进行大小写转换。

    47 '-'.join(['2023','7','1'])

    可以通过字符串的内置函数join将一个字符串加入到列表中的多个符串中

    >>> '-'.join(['2023','7','1'])
    '2023-7-1'
    
    • 1
    • 2

    48 len(string.encode('utf-8'))

    将字符串转化为utf-8编码后,其长度即为字节数,据此可判断文本文件大小

    len("我爱你".encode('utf-8'))
    # 9
    
    • 1
    • 2

    49 sys.getsizeof(v)

    sys中的getsizeof函数可以检查变量所占内存大小。

    import sys
    v = 30
    print(sys.getsizeof(v)) # 24
    
    • 1
    • 2
    • 3

    50 id(v)

    id函数可以返回变量在内存中的位置。

    >>> v = "abcde"
    >>> id(v)
    1803924712368
    
    • 1
    • 2
    • 3

    高手之路

    51 itertools.list(chain(*[[1,2],[3],[4,5,6]]))

    chain也是itertools中的一个函数,可以拼接列表。

    from itertools import chain
    >>> list(chain(*[[1,2],[3],[4,5,6]]))
    [1, 2, 3, 4, 5, 6]
    
    • 1
    • 2
    • 3

    52 op = {"add":lambda a,b:a+b, "minus":lambda a,b:a-b}

    op["add"]表示调用函数lambda a,b:a+b,即加法;op["minus"]表示调用函数lambda a,b:a-b,即减法。

    正因lambda表达式并不需要命名,所以也称匿名函数。

    >>> op = {"add":lambda a,b:a+b, "minus":lambda a,b:a-b}
    >>> op["add"](3,4)
    7
    >>> op["minus"](3,4)
    -1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    53-54 while...else

    while a<5:a+=1
    else: print(f"a={a}")
    
    • 1
    • 2

    while循环大家都十分了解,即当a<5时执行a+=1的程序。else表示当a<5不满足时执行的代码。

    >>> while a<5:
    ...     a+=1
    ... else: 
    ...     print(f"a={a}")
    ...
    a=5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    55-57 for...else

    xs = []
    for x in range(10): xs.append(x)
    else : print(xs)
    
    • 1
    • 2
    • 3

    while..else相似,for也有和else的组合,其语义也很雷同,表示当执行完for循环之后执行else的语句。

    >>> xs = []
    >>> for x in range(10): xs.append(x)
    ... else : print(xs)
    ...
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    58-60

    from matplotlib import pyplot as plt
    plt.plot(np.random.rand(10))
    plt.show()
    
    • 1
    • 2
    • 3

    from...import表示从matplotlib中导入pyplotmatplotlib是python中最常用的画图包,功能非常强大。

    plt.plot是最常用的绘图函数。python在执行绘图函数之后,会将图片放入内存,当使用plt.show()之后,才会将其显示到屏幕上。

    >>> from matplotlib import pyplot as plt
    >>> plt.plot(np.random.rand(10))
    [<matplotlib.lines.Line2D object at 0x00000232FA511B10>]
    >>> plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    61-68

    class person:
        def __init__(self,name): 
            self.name = name
        def selfIntro(self): 
            print(f"my Name is {self.name}")
        @staticmethod
        def say(string): 
            print(string)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    尽管主打函数式,但在python中,一切皆对象。

    class则可以声明一个类,在类中,__init__为python内置的初始化函数,在类实例化之后,会首先运行这个函数。

    self用来声明类成员。

    @staticmethod为静态类标识,静态类可以不经实例化而调用。

    >>> person.say("hello")
    hello
    >>> person.selfIntro()
    Traceback (most recent call last):
      File "", line 1, in <module>
    TypeError: person.selfIntro() missing 1 required positional argument: 'self'
    >>> Li = person("Li")
    >>> Li.selfIntro()
    my Name is Li
    >>>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    69 xs=[i for i in range(10) if i%2==0]

    通过推导式来快速筛选内容,并创建列表。

    >>> xs=[i for i in range(10) if i%2==0]
    >>> xs
    [0, 2, 4, 6, 8]
    
    • 1
    • 2
    • 3

    70 d = dict([[1,2],[4,5],[6,7]])

    dict可将列表转为字典,前提是列表中的元素必须为二元组。

    >>> d = dict([[1,2],[4,5],[6,7]])
    >>> d
    {1: 2, 4: 5, 6: 7}
    
    • 1
    • 2
    • 3

    内置包库

    71 time.time()

    当然前提是要导入import time,这其实是个很常用的函数,以时间戳的形式返回当前的时间。

    >>> import time
    >>> time.time()
    1634558595.5172253
    
    • 1
    • 2
    • 3

    72 calendar.prmonth(2021,10)

    可打印日历。。。

    >>> import calendar
    >>> calendar.prmonth(2021,10)
        October 2021
    Mo Tu We Th Fr Sa Su
                 1  2  3
     4  5  6  7  8  9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31
    >>>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    73 os.listdir(r"c:\Windows")

    返回文件列表,其中r字符串中的\不用于转义。

    >>> import os
    >>> os.listdir(r"c:\Windows")
    ['addins``appcompat``apppatch``AppReadiness``assembly``bcastdvr``bfsvc.exe', ...
    
    • 1
    • 2
    • 3

    74 os.path.join(r"C:\Windows", "python.exe")

    合并路径,自动处理\\

    >>> os.path.join(r"C:\Windows", "python.exe")
    'C:\\Windows\\python.exe'
    
    • 1
    • 2

    75 glob.glob(r"c:\Windows\*.ini")

    可通过通配符返回文件列表。

    >>> import glob
    >>> glob.glob(r"c:\Windows\*.exe")
    ['c:\\Windows\\bfsvc.exe``c:\\Windows\\explorer.exe``c:\\Windows\\HelpPane.exe``c:\\Windows\\hh.exe``c:\\Windows\\notepad.exe``c:\\Windows\\py.exe``c:\\Windows\\pyw.exe``c:\\Windows\\regedit.exe``c:\\Windows\\splwow64.exe``c:\\Windows\\Wiainst64.exe``c:\\Windows\\winhlp32.exe``c:\\Windows\\write.exe']
    >>>
    
    • 1
    • 2
    • 3
    • 4

    76-77 urllib

    res = urllib.request.urlopen('https://blog.csdn.net/')
    html = res.read()
    
    • 1
    • 2

    urllib是python内置的http解析请求库,是大多数爬虫学习者接触的第一个工具。

    其中,read()用于读取网页数据,当然,得到的网页数据是未解码数据。

    import urllib.request
    res = urllib.request.urlopen('https://blog.csdn.net/')
    html = res.read()
    
    • 1
    • 2
    • 3

    78-79 正则表达式re

    content = html.decode('utf-8')
    cn = re.findall(r"[\u4e00-\u9fa5]+", content)
    
    • 1
    • 2

    此为正则表达式的简单应用,re.findall表示从字符串content中筛选出符合r"[\u4e00-\u9fa5]+"要求的值。所以第一步,是通过utf-8对content进行解码。

    而在utf-8中,汉字的序号为\u4e00-\u9fa5;在正则表达式中,[]表示符合条件的集合,+表示出现任意多个符合条件的字符。

    >>> import re
    >>> content = html.decode('utf-8')
    >>> cn = re.findall(r"[\u4e00-\u9fa5]+", content)
    >>> cn[:20]
    ['博客``专业``技术发表平台``博客为中国软件开发者``从业人员``初学者打造交流的专业``技术发表平台``全心致力于帮助开发者通过互联网分享知识``让更多开发者从中受益``一同和``开发者用代码改变未来``头部``广告``频道首页右侧``打底``头部``广告``题目征集``你出我答``做']
    >>>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    80 Thread(target=print,args=["hello thread"]).start()

    这是最简单的多线程程序,target是将要调用的函数,args为参数列表。

    >>> from threading import Thread
    >>> Thread(target=print,args=["hello thread"]).start()
    hello thread
    
    • 1
    • 2
    • 3

    81-83 线程池

    concurrent.futures中的ThreadPoolExecutor可快速创建线程池,其输入参数为线程池中可以容纳的最多线程数,

    from concurrent.futures import ThreadPoolExecutor
    with ThreadPoolExecutor(max_workers=5) as ex:
        for i in range(15):
            ex.submit(print,f"{i}")
    
    • 1
    • 2
    • 3
    • 4

    84-86 进程池

    创建单个进程的方法和创建多个线程的方法并无区别,只是把threading中的Thread换成multiprocessing中的Process而已,故不赘言了。

    from multiprocessing import Pool
    
    def over(cb):
        print("完成")
    
    # 下面3行为创建进程池的函数
    p = Pool(5)
    for i in range(15):
        p.apply_async(func=print, 
            args = [f"{i}"], callback=over)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    87-88 创建窗口程序tkinter

    frame = tkinter.Tk()
    frame.mainloop()
    
    • 1
    • 2

    其中frame即为tkinter创建的窗口,而mainloop表示进入窗口的消息循环。

    >>> import tkinter
    >>> frame = tkinter.Tk()
    >>> frame.mainloop()
    
    • 1
    • 2
    • 3

    89-90 通过ctypes调用C语言动态链接库

    >>> import ctypes
    >>> libc = ctypes.CDLL("msvcrt")
    >>> libc.printf("%s".encode(), "Hello ctypes\n".encode())
    Hello ctypes
    13      #此为该函数的返回值
    
    • 1
    • 2
    • 3
    • 4
    • 5

    奇技淫巧

    91 judge = lambda a,b,f1,f2 : (f1 if a>b else f2)(a,b)

    表示,如果a>b则执行f1(a,b),否则执行f2(a,b)

    92 eval('[a,b,c]')

    eval函数会把字符串转为可执行的表达式,由于这个功能不太安全,所以尽管使用门槛很低,但不建议初学者使用。

    93 list(zip(*lst))

    zip可以像拉链一样将数组中对应的值缝合起来,以元组的形式重新存储。根据这个特性,可完成列表的"转置"。

    >>> lst = [[1,2], [3,4], [5,6]]
    >>> list(zip(*lst))
    [(1, 3, 5), (2, 4, 6)]
    
    • 1
    • 2
    • 3

    94 max(set(lst),key=lst.count)

    其中lst为列表,count(i)是列表的内置函数,表示统计i出现的个数。set表示将lst转为集合,从而剩排除重复值。

    max(set(lst),key=lst.count)表示通过lst.count这个指标来得到set(lst)中出现次数最多的那个值——即求众数。

    95 dict(zip(myDict.values(),myDict.keys()))

    通过zip实现字典的键值对互换操作。

    96 map(lambda x: lst[x * size:x * size + size], list(range(0, ceil(len(lst) / size)))))

    这种丧心病狂的单行表达式十分魔性,感兴趣的朋友可以试着读读看。

    97 ['未成年', '成年'][age > 18]

    这种写法等价于'成年' if age > 18 else '未成年',其实很容易理解,age>18若为True,则对应1,返回列表中的第1项;否则返回第0项。

    98 sum([[1,2,3],[4,5,6]],[])

    之前已经讲了用chain拼接列表的方法,sum也可以实现这一功能,而且更加优雅简洁。其原理是,用[]依次加上[1,2,3][4,5,6]。一般来说,[]这一项默认为0,故而适用于数值加法。

    但目前来说,并不支持sum(['a','b'],''),但字符串列表有join,所以并不需要这个。

    99-100 sys.ps1, sys.ps2=":", "-"

    sysps1ps2分别表示,在交互模式下的两个提示符,这个不知道如何描述,但做一下示例就明白了

    >>> sys.ps1 = ":"
    :sys.ps2 = "-"
    :def test():
    -  print("Hello")
    -
    :test()
    Hello
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    从 MySQL 到 ClickHouse 实时数据同步 —— Debezium + Kafka 表引擎
    「BUAA OO Pre」 Pre 2总结回顾概览
    正则表达式
    Hi3559av100 u-boot bootargs bootcmd
    计算机毕业设计django基于协同过滤的旅游推荐系统(源码+系统+mysql数据库+Lw文档)
    CentOS 编译安装TinyXml2
    Java里面int、Integer、String相互转换
    SC7A20(士兰微-加速度传感器)示例
    枚举类根据name获取value
    int和Interger区别
  • 原文地址:https://blog.csdn.net/m0_37816922/article/details/133969143