整数:int
浮点数:float
布尔类型:bool
字符串:str --不可变
列表:list[ a,b,c]--可变
嵌套列表(二维列表):[[1,2],[true,false],['hello','world']]
元组:tuple(1,2,3) 里面哪怕只有一个元素也要加逗号 --不可变
集合:set {1,2,3} 无序的,去重 无法执行切片操作;空集合 set()
字典:dict eg:{key1:value1,key2:values2,key3:values3} 不能有相同的key
type(2/2):float type(2//2):int
/:除法会自动的转成浮点型
//:整除 只会保留整数部分
进制:十进制(1,2,3...10)、二进制(0,1,10,11,110)、八进制(1,2...,7,10)、十六进制(0,1,2,...9,A,B,C)
转换方法:0b+二进制 0o+八进制 0x+十六进制
函数方法:bin()转二进制 int()转十进制 hex()转成十六进制 oct()八进制
bool()转换成布尔类型,任何空值都是false 例如bool([]),bool("")
转义字符:\ eg:'let\'s go' \' 单引号 \t 指标符号(tab)
想输出一个路径print(r'c:\northwind\northwest')
r+路径 输出原始字符串
换行符号:\n
多行输入:'''或者"""
字符串的运算
合并字符串方法:
+
eg:"hello"+"world" => "helloworld"
重复字符串:* eg:"hello"*3 => "hellohellohello"
切片操作
输出字符串字母:[n] | [-n] eg:'hello world[0]' => 'h' 'hello world'[-1] => d
输出字符[n:m] 左闭右开,截取的结果从n到m-1 eg:"hello world"[0:5] => 'hello' "hello world"[0:-1] => 'hello worl'
截取完整字符[n:]从n截取到结尾:"hello world"[0:11] => ''hello world' 或者 "hello world"[0:] => "hello world"
开头截取:[:n]
三字符的切片操作
eg:"hello world"[0:8:2] 最后一个参数是步长的意思,例子中的意思是取0-8的字符,隔两个取一个即取:0,2,4,6,8 =>hlwr
列表获取
索引获取 eg:["新月打击","苍白之瀑","月之降临"][0] => '新月打击'
范围获取[n:m] 左闭右开
倒数获取[n:-m] 从后面数,由-1开始
列表合并:+
元组获取可以参考列表列表,为什么python中会存在元组这种类型呢?
元组是不可变序列,list是可变序列
判断是否存在 :in eg:3 in [1,2,3,4]
判断是否不存在:not in eg:3 not in [1,2,3]
求长度:len("hello world") => 11
最大值,最小值:max([1,2,3,4]) => 4 min([1,2,3,4]) => 1
scii转换:ord()
集合剔除:- eg:{1,2,3,4} - {1,2} =>{3,4}
集合求交集:& eg:{1,2,3,4,5} & {3,4} =>{3,4}
集合合并:| eg:{1,2,3,4} | {3,4,7} =>{1,2,3,4,7}
字典正确访问方式,通过key来访问:eg:{'Q':'新月打击','w':'苍白之瀑'}['Q']