python个人学习笔记

阅读量: searchstar 2020-03-22 00:16:52
Categories: Tags:

语法

缩进应该使用4个空格而不是tab:https://peps.python.org/pep-0008/#tabs-or-spaces

for循环

0到9循环:

for i in range(10) :
	Statements

在交互式环境中(如命令行形式)后面要多打一个回车才开始运行

from dataclasses import dataclass
@dataclass
class A:
    a: int
    b: str
    c: float
li: list[A] = []
li.append(A(1, "2", 3.3))
li.append(A(2, "3", 4.4))
print(li)
# [<class '__main__.A'>, A(a=1, b='2', c=3.3), A(a=2, b='3', c=4.4)]
item = li[1]
print(item.c)
# 3.3
sorted(li, key = lambda item: item.c, reverse=True)

参考:https://stackoverflow.com/questions/35988/c-like-structures-in-python

完整教程:Python 面向对象

内置数据结构

输入

输出

完整版:Python3 print 函数用法总结

输出为科学计数法

参考:https://blog.csdn.net/qq_45434742/article/details/102094577?fps=1&locationNum=2

print("%e" %111)
x = 111
print("%e" %x)
print("%e" %(111 + 111))
print("%e %e" %(x, 111 + 111))

不打印换行符

print('hello', end='')

打印到stderr

print('xxx', file=sys.stderr)

来源:https://stackoverflow.com/questions/5574702/how-do-i-print-to-stderr-in-python

指定分隔符打印数组

>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5

来源:https://stackoverflow.com/questions/22556449/print-a-list-of-space-separated-elements

文件

python文件管理

python基础之写文件操作

切换到脚本所在目录

import sys
import os

abspath = os.path.abspath(sys.argv[0])
dname = os.path.dirname(abspath)
os.chdir(dname)

来源:https://stackoverflow.com/questions/1432924/python-change-the-scripts-working-directory-to-the-scripts-own-directory

标准库

时间

https://docs.python.org/3/library/datetime.html

datetime.datetime.fromtimestamp(1516332287)

也可以用time包:Python中时间与时间戳之间的转换

math

阶乘

10!

from math import *
factorial(10)

正则

https://docs.python.org/3/library/re.html

执行命令

简单执行命令

import subprocess
subprocess.call(['./executable', arg1, arg2])

来源:https://stackoverflow.com/questions/5788891/execute-a-file-with-arguments-in-python-shell

获取命令输出

python的popen函数

排序

sorted

对字典的key排序

x = {1: 2, 4: 3, 2: 1}
sorted(x)
[1, 2, 4]

对字典的值排序

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted(x.items(), key=lambda item: item[1])
[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

来源:https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

shlex

可以用解析命令行参数的方式解析字符串。文档:https://docs.python.org/3/library/shlex.html

例子:

# https://stackoverflow.com/a/899314
import shlex
shlex.split('-o 1 --long "Some long string"')
['-o', '1', '--long', 'Some long string']

其他

三方库

dateutil

https://dateutil.readthedocs.io/en/stable/

pip3 install python-dateutil

可以实现时间减去月数等功能。

来源:https://thispointer.com/subtract-months-from-a-date-in-python/

pandas

官方文档:https://pandas.pydata.org/docs/

pandas数据框常用操作

numpy

官方文档:https://numpy.org/doc/stable/

矩阵操作

等差数列

文档:https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)[source]

其他

json.dumps(obj, indent='\t')可以使用tab进行缩进。

pip

安装指定版本:https://www.marsja.se/pip-install-specific-version-of-python-package/

requirements.txt: https://note.nkmk.me/en/python-pip-install-requirements/

有关

已知的问题

调用另一个文件里的函数不太方便: https://www.geeksforgeeks.org/python-import-module-from-different-directory/