Python从入门到入土的90行代码

基础入门

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...10

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

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

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

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

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

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

>>> x[0]
1

10 y = set(x)\

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

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

菜鸟提升

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)。

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

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

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

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 "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

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]

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

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

基础晋级

21 a = 1,2,3

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

>>> a = 1,2,3
>>> a
(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

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

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

>>> print(f"a={a}")
a=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

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]

26-30 函数封装

def fac(n):
    if n == 0:
        return 1
    else:
        return n*fac(n-1) 

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

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

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): print(i,x)
enumerate可以逐次返回由列表索引和元素构成的元组,效果如下

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

35 [*a,*b]

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

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

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

>>> addd = lambda a,b,c : a+b+c
>>> addd(*a)
6

36 {**a,**b}

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

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

同样,双星号索引的用途也不止于此

>>> addd(3,**a)
6

37 s == s[::-1]

在python中,对列表或者字符串采用:进行索引,例如a:b指的是从a到b的数据;当采用双引号::时,引号间的值的意义就发生了变化,例如a:b:c表示从a到b,间隔为c的数据。

据此,可以得到::-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

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

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

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

效果为

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

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

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

高手之路

41 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]

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

Python中没有switch..case表达式,而字典+lambda表达式可以弥补这一点。上式中,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

43-44

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

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

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

45-47

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

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]

48-50

from matplotlib import pyplot as plt
plt.plot(np.random.rand(10))
plt.show()

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()

51-58

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)

尽管python主打函数式,但在python中,一切皆对象。而class则可以声明一个类。

在类中,通过self来声明类成员,类似有些语言中的this.

__init__为python内置的初始化函数,在类实例化之后,会首先运行这个函数。

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

>>> person.say("hello")
hello
>>> person.selfIntro()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: person.selfIntro() missing 1 required positional argument: 'self'
>>> Li = person("Li")
>>> Li.selfIntro()
my Name is Li
>>>

59 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]

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

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

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

内置包库

61 time.time()

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

>>> import time
>>> time.time()
1634558595.5172253

62 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
>>>

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

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

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

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

合并路径,自动处理\\

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

65 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']
>>>

66-67 urllib

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

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

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

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

68-69 正则表达式re

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

此为正则表达式的简单应用,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]
['博客``专业``技术发表平台``博客为中国软件开发者``从业人员``初学者打造交流的专业``技术发表平台``全心致力于帮助开发者通过互联网分享知识``让更多开发者从中受益``一同和``开发者用代码改变未来``头部``广告``频道首页右侧``打底``头部``广告``题目征集``你出我答``做']
>>>

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

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

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

71-73 线程池

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}")

74-76 进程池

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

from multiprocessing import Pool

def over(cb):
    print("完成")

p = Pool(5)
for i in range(15):
    p.apply_async(func=print, 
        args = [f"{i}"], callback=over)

77-78 创建窗口程序tkinter

frame = tkinter.Tk()
frame.mainloop()

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

>>> import tkinter
>>> frame = tkinter.Tk()
>>> frame.mainloop()

79-80 通过ctypes调用C语言动态链接库

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

奇技淫巧

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

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

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

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

83 list(zip(*lst))

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

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

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

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

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

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

通过zip实现字典的字符串互换操作。

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

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

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

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

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

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

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

89-90 sys.ps1, sys.ps2=":", "-"

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

>>> sys.ps1 = ":"
:sys.ps2 = "-"
:def test():
-  print("Hello")
-
:test()
Hello

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年3月2日 下午10:01
下一篇 2023年3月2日 下午10:03

相关推荐