Python随心记

dir(__builtins__) ---查看内置函数

hlep(函数) ----帮助

str=r'sadada' -----r自动转义

type() -- 查看数据类型

isinstance ----验证是什么类型数据

small = x if x < y else y --三元运算符

assert ---断言

assert 3>4 如果assert后面的判断为false的时候程序奔溃会抛出AssertionError的异常

for i in member ----for循环

break ----跳出循环

continue ----终止本轮开始下一轮

len(member) ---统计列表的长度

member.append('萌萌') ----向列表追加元素 每次只能追加一个

member.extend(['阿纯','萌萌']) -向列表追加元素 每次只能追加多个

member.insert(1,'阿纯') ----在第二位插入一个元素(从0开始)

member.remove('萌萌') ---从列表中移除萌萌

del.member[1] ----删除

list=[]----创建空列表

member.pop() ----出栈

member[1:3] ------列表分片

list.count(127) ----统计127在列表出现的次数 列表的方法都是:列表.方法名

list.index(123,3,7) ---从第三个开始到第七

list.reverse() ----反转列表

list.sort(func,key,reverse=true) ---从小到大排序 reverse=true 倒序

元组(定义后不可改变)

* ----重复操作符

temp=() 创建空元组

temp=(1,) ---创建一个参数的元组
temp=1,2,3 --创建一个参数的元组

tuplea= (1,2,3,4) ---定义元组

temp=temp[:2]+('一静',) + temp[2:] ------往元组中添加数据

del temp ----删除元组

str.capitalize() ---字符串首字母大写

list() ---列表

列表 元组

def MyFirstFunction() ----定义函数

Local Variable ---局部变量(在函数内部定义的)

Global Variable --- 全局变量(在函数外定义的)

MyFirstFunction() --调用函数

lambde --- 匿名函数

fileter() ---过滤方法

list(filter(lambda x : x % 2,range(10))) ---过滤偶数 filter()

list(map(lambda x : x * 2,range(10))) ---过滤偶数 map()

def recursion()
return recursion() -----递归(死循环)

字典
创建字典
dict = {'a':'adad','b':'ada'}

dict.formkeys((1,2,3))

dict.keys() 打印健

dict.values() 打印值

dict.get('21','米有')

dict.clear() ---请空字典

集合
set()

a in num 判断是否在集合中

a not in num 判断是否在集合中

文件

os.getcwd() ---得到工作目录
os.chdir() 改变工作目录
os.mkdir() 创建目录
os.mkdirs() 递归创建

永久存储
import pickle
pickle_file = open('list.pkl','wd')
pickle.dump(list,lick_file)
pick_file.close

猜你喜欢

转载自www.cnblogs.com/Essaycode/p/10040939.html