简单HTTP服务器
python -m SimpleHTTPServer 8080
Python中字符与数字转换
ASCII值与字符的转换:
>>> chr(65), ord('A') ('A', 65)
不同进制的字符串
>>> bin(15), oct(15), hex(15) ('0b1111', '017', '0xf') >>> int('1111',2), int('17',8), int('f',16) (15, 15, 15)
python时间
import time start = time.monotonic() time.sleep(0.2) end = time.monotonic() print("start: {:>9.2f}".format(start)) print("end: {:>9.2f}".format(end)) print("span: {:>9.2f}".format(end - start)) start = time.perf_counter() i = 0 while i < 100000: i = i + 1 elapsed_time = time.perf_counter() - start print("Elapsed time: {}".format(elapsed_time))
Python调用C模块
C源代码:
#include <stdio.h> int sum(int a, int b) { return a + b; }
编译C源代码为so库文件:
gcc -fPIC -shared sample.c -o sample.so
Python代码中调用ctype
模块加载so
文件:
import sys from ctypes import * # load ctype module c_object = CDLL('./sample.so') print(c_object.sum(3, 40))
测试执行效果:
python3 sample.py # show: 43