1、使用索引反转字符串
str="hello"
print(str[::-1])
2、zip函数获取可迭代对象,将它们聚合到一个元组中,然后返回结果。语法是zip(*iterables)
numbers = [1, 2, 3]
string = ['one', 'two', 'three'] 
result = zip(numbers,string)
print(set(result))
>>>{(3, 'three'), (2, 'two'), (1, 'one')}
3、装饰器(Decorator)用于向现有代码添加功能。这也称为元编程,因为程序的一部分在编译时会尝试修改程序的另一部分。
def addition(func):
    def inner(a, b):
        print("numbers are", a, "and", b)
        return func(a, b)
    return inner

@addition
def add(a, b):
    print(a + b)

add(5, 6)
>>>
numbers are 5 and 6
11 
4、python调用shell脚本

(1)使用 os.system 来运行
(2)使用 subprocess.run 来运行
(3)使用 subprocess.Popen 来运行

5、python py中调用sh
fromsh.sh中:
#!/bin/bash
echo "from sh file"
callsh.py中:

#!/usr/bin/python3
import os

print ("start call sh file")
os.system('./fromsh.sh')
print ("end call sh file")
6、sh中调用py
frompy.py中:
#!/usr/bin/python3
print ("from python")
callpy.sh中:
#!/bin/bash3
echo 'start call py'
./frompy.py
echo 'end call py'
7、冒泡排序,比较相邻元素
def x_sort(lists):
    n = len(lists)
    for i in range(n):
        for j in range(0, n - 1):
            if lists[j] > lists[j + 1]:
                lists[j], lists[j + 1] = lists[j + 1], lists[j]
    print(lists)
x_sort([3, 1, 6, 9, 8, 7, 5])
8、选择排序,用一个元素和其他元素相比
def y_sort(lists):
    n = len(lists)
    for i in range(n):
        for j in range(i + 1, n):
            if lists[i] > lists[j]:
                lists[i], lists[j] = lists[j], lists[i]
    print(lists)
y_sort([10, 6, 8, 2])
9、os模块

(1)os.system() 执行程序或命令
(2)os.path.abspath(文件) 获取文件绝对
(3)os.path.repath(文件) 获取文件相对路径
(4)os.getcwd() 返回当前路径
(5)os.getlogin() 当前系统登录用户名
(6)os.cpu_count() 当前系统CPU数量
(7)os.rename(原名,新名) 重命名
(8)os.mkdir(目录名) 创建目录

10、json模块

(1)json.loads() json格式的字符串转为python字典格式
(2)json.dumps() python字典格式转为json格式

11、time模块

(1)time.time() 获取时间戳
(2)time.sleep() 休眠几秒

12、datetime模块

(1)datetime.datetime.now() 获取当前日期和时间
(2)datetime.date.today() 获取当前的日期

13、random模块

(1)random.randint(1,9) 获取随机数

14、re模块:执行正则,进行过滤

。。。

15、eval函数:将字符串当成有效的表达式来执行并返回结果
a = eval('1+1')
print(a)
>>>2
b = (eval('[1,2,3]'))
print(b)
print(type(b))
>>>[1,2,3]
<class 'list'>
16、密码不可见
import getpass
pwd = getpass.getpass("密码:")
print(pwd)
>>>在terminal中运行
F:\code\Pythoncode\StaffingSystem_django>python api/utils/other.py
密码:
123
17、sys模块

负责程序与 Python 解释器的交互,并提供了一系列的属性和方法,用于操控 Python 运行时的环境。
(1)获取系统标识符

import sys
print(sys.platform)
>>>win32

(2)获取python解释器版本信息

import sys
print(sys.version)
>>>3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]
18、md5加密
import hashlib
def md5(salt):
    """md5加密"""
    # salt可以加点盐
    obj = hashlib.md5()
    obj.update(salt.encode('utf-8'))
    print(obj.hexdigest())
md5("123")
19、高阶函数:一个函数的参数使用了另一个参数

(1)将无序列表排序:sorted、max、min

list_value = [('tom', 21), ('a', 19), ('b', 33)]
# 最大值
sort_list = max(list_value, key=lambda n: n[1])
print(sort_list)
# 最小值
sort_list = min(list_value, key=lambda n: n[1])
print(sort_list)
# 排序,reverse=True倒序
sort_list = sorted(list_value, key=lambda n: n[1], reverse=True)
print(sort_list)

(2)filter类,过滤一个列表中符合规定的元素,结果为迭代器
(3)map类,将列表中的每项数据执行相同操作,结果为迭代器
(4)reduce函数,对一个序列进行压缩运算,得到一个值,在functools模块