• 已删除用户
LH
发布于 2018-01-26 / 401 阅读 / 0 评论 / 0 点赞

Python3学习杂记

收集一些Python的笔记

print 输出不换行:

print("hello world",end="")

删除字符串最后一个字符:
>>> str = "hello world"
>>> str = str[:-1]
>>> print(str)
hello worl
字符串拼接:

"%s,%s" % ("hello","world")

调用系统命令:

	import os
	os.system("ping 127.0.0.1")  #返回命令的退出状态码 
	os.popen("ping 127.0.0.1")   #返回命令的输出

模拟键盘输入:调用win32api实现,第一个参数为键位码
(键位码表:104键键盘按键码对照

	import win32api,win32con
	win32api.keybd_event(121, 0, 0, 0)    #模拟按F10
	win32api.keybd_event(121, 0, win32con.KEYEVENTF_KEYUP, 0)

列出模块可用变量名:

	import sys
	print(dir(sys))

使Python脚本可直接执行:

#!/usr/bin/env python3

终端中Python代码补全:

sudo pip3 install ipython
$ipython    发现新世界!

工厂函数:
一种可以记住自身状态的创建函数方式

def f(m):
	def g(x)
		return x**m
	return g
>>> square = f(2)
>>> square(2)
4
>>> cube = f(3)
>>> cube(2)
8

评论