数据类型
- 布尔型:
bool
- 数值:
int
- 字符串:
str
- 元组:
tuple
- 列表:
list
- 字典:
dict
- 集合:
set
- 类类型
- None
- 深浅
copy
- 值范围:{True, False}
True
bool(1)
bool(0b01))
bool(-1.2)
bool('s')
bool([1])
Flase
bool(0b0)
bool(0.0)
bool('')
bool("")
bool(None)
bool([])
bool(())
bool({})
- 整型
- 整型 int
- 长整型 long
- Python 3 里都是整型
- 浮点型
- 10进制:{0,9}
- 2进制:{0,1}
- 表示法:0b10
- 8进制:{0,7}
- 表示法:0o10
- 16进制:{0,f}
- 表示法:0xff
- 转换2进制:
- 10->2 : bin(10)
- 8->2 : bin(0o10)
- 16->2 : bin(oxff)
- 转换10进制:
- 2->10 : int(0b111)
- 8->10 : int(0o10)
- 16->10: int(oxff)
- 转换8进制:
- 2->8 : oct(0b111)
- 8->8 : oct(0o10)
- 16->8: oct(oxff)
- 转换16进制:
- 2->16 : hex(0b111)
- 8->16: hex(0o10)
- 16->16: hex(oxff)
bit_length()
当前数字占用二进制最少位数bin(18) => 0b10001 => 5 bit
__abs__()
绝对值abs()
内部创建数组对象并调用abs()数字方法__add__(y)
加号运算, 等于加号+
__divmod__(y)
返回元祖,分页场景使用__float__()
类型转换__floordiv__()
floor 想下取整__ge__()
大于等于__le__()
小于等于help(10), help(int)
帮助函数203020930239
j表示复数(5+4j)
3.141592
sys.float_info
复数:
3+6e
- 单引号
- 双引号
- 三引号
str(1)+"string"
- 字符串连接
str=str1+str2
str="===%s==="%(str1+str2)
r'C:\now\fishc\ca'
len(str)
i=str[index]
i=str[-lastIndex]
str=str[0:2] # strarIndex:endIndx, [:2]
str=str[1:3] # startIndex:endIndex,不包括endIndex元素`
str=str[2:] # 第三个元素到最后一个
str=str[-2:] # 最后两个元素
str=str[1:10:2] # 2:step
str=str[-1:0:-1] # 反向取值,不包括第一个0元素值
str=str[-1::-1] # 反向取值,所有的
str=str[::-1] # 反向取值,所有的,以步长为顺序决定开始索引和结束索引
str = "hello world hello hi ho are you"
-1 | index str.find(search [, start=0, end=len(search)])
str.find("llo") # 2
str.find("good") # -1
-1 | index str.rfind("llo") # 14
error | index str.index(search [, start=0, end=len(search)])
str.index("e") # 1
str.rindex("llo") # 14
str.index("good") # ValueError: substring not found
0 | 字符串中子串个数 str.count(search [, start=0, end=len(search)])
0 | 字符串中子串个数 str.count("llo") # 2
newstr str.replace(old, new [,str.count(old)])
newstr str.replace("llo","good") # hegood world hegood hi ho are you
对原字符串没有影响
count 替换次数
str.split(seperator[, count])
str.split(" ") # ['hello', 'world', 'hello', 'hi', 'ho', 'are', 'you']
str.split(" ",2)# ['hello', 'world', 'hello hi ho are you']
以特殊字符作为分隔符作为单词边界
str.title() # Hello World Hello Hi Ho Are You
str.startswith("hello") # True
str.startswith("ehello") # False
str.endswith("you") # True
- 上传文件
- 检查后缀名
- 检查内容
str.capitalize()
str.lower() # hello world hello hi ho are you
str.upper() # HELLO WORLD HELLO HI HO ARE YOU
'hello\t word'.expandtabs(tabsize=20)
'hello\t world'.expandtabs(20)
'hello\t world'.expandtabs(20)
s="hi"
左对齐:str.ljust(5) # "hi "
右对齐:str.rjust(5) # " hi"
中对齐:str.center(5) # " hi "
'hello world How are you?'.center(50,'*')
'*************hello world How are you?*************'
s=" hi "
左修剪:str.lstrip()
右修剪:str.rstrip()
修剪:str.strip()
s="hello world title how title are you"
s.partition("title") # ('hello world ', 'title', ' how title are you')
s.rpartition("title") # ('hello world title how ', 'title', ' are you')
s.partition("titles") # ('hello world title how title are you', '', '')
s.rpartition("titles") # ('', '', 'hello world title how title are you')
s="hello world\n how areyou\nhi"
s.splitlines() # ['hello world', ' how areyou', 'hi']
s="123"
s1="abc 1"
s2="abc"
s.isalpha() # False
s1.isalpha() # False
s2.isalpah() # True
s="123"
s1="12 a"
s2="abc"
s.isdigit()` # True
s1.isdigit()` # False
s2.isdigit()` # False
s="="
li=["a","b","c"]
s.join(li)` # a=b=c
只读列表
t=(3,) # 注意:单个元素元组最后必须加逗号
t=(11,22)
a,b=t # a=11,b=22
t[index]
t.index(ele)
t.count(ele)
li = list([1,2,3])
li = [1,2,3]
li.append(ele) # 压栈,没有返回值[null],整体添加
li.insert(index,ele) # 数据类型
li.extend(li2) == li+li2 # 整体分割成元素分别添加
区别:li 变为 li+li2, 而 li+li2 的 li 值不变
li.pop() # 弹栈,delete last element
li.remove(ele) # 删除查找元素的第一个
del li[index] # 根据下标删除
li.clear() # 清空 []
li[index] = value
li[start:end]
li.count(ele)
error|int li.index(ele)
boolean "元素" [not] in LIST
max([1,2,3,4]) # 4
min([1,2,3,4]) # 1
max("abcdefA") # A (ASCII 排序)
ord('A') # 65
ord('a') # 97
ord(' ') # 32
name_list.reverse()
name_list.sort() # 升序
ASCII码顺序:数字,特殊字符,字母
sorted(li)
函数name_list.sort(reverse=True)
存在就覆盖,否则就增加
di={'k1':'v1','k2':'v2'}
di["k1"]="value"
di["new"]="new value"
error|bool del di["k1"]
d.pop('key') # 返回值
d.popitem() # 删除随机元素
di["key"]
**di.get("key")** # 没有找到返回空,None
len(di)
返回没有索引的键列表
di.keys()
返回没有索引的值列表
di.values()
返回键值对
di.items()
[('k1','v1'),('k2','v2')]
ele in d.keys()
ele in d.values()
k,v in d.items()
info=[{'name':'banzhang','age':10},{'name':'fubanzhang','age':9},{'name':'xiaoming',age':20}]
info.sort(key=lambda x:x['name'])
> x是元素,即字典
> x['name'],字典的name属性
> ASCII码比较
d.setdefault("age",34)
d.setdefault("age",34)
dict.fromkeys([A,B,C],D)
int(变量)
float(变量)
str(变量)
bool(变量)
list(变量)
tuple(变量)
dic(变量)
set(变量)
type(变量)
集合元素不能有重复值
集合:可变的数据类型,元素必须是不可变的数据类型,无序,不重复
s = set([1,2,33,33,2,1,4,11,11,11])
li = list(s)
# 告诉我列表拥有的所有方法
print(dir([]))
# 列表的所有方法
print(dir({}))
# 字符串的所有方法
print(dir(''))
# 列表拥有的方法
print(dir(range(10)))
# 双下的方法
print([1].__add__([2]))
print([1] + [2])
# 可迭代的对象都有`__iter__`
ret = set(dir([])) & set(dir({})) & set(dir('')) & set(dir(range(10)))
print(ret) # iterable
print('__iter__' in dir(int)) # False
print('__iter__' in dir(bool)) # False
print('__iter__' in dir(list)) # True
print('__iter__' in dir(dict)) # True
print('__iter__' in dir(set)) # True
print('__iter__' in dir(tuple)) # True
print('__iter__' in dir(enumerate([]))) # True
print('__iter__' in dir(range(1))) # True
# 能被for循环的数据类型就一定拥有__iter__方法
# 列表执行了__iter__()之后的返回值就是一个迭代器
print([].__iter__())
创建一个包含tuple 所有功能以及其他功能的类型
Python 内置的一个线程安全的双向队列
- 单向队列
- 双向队列
- 对 于数字和字符串类型,深浅拷贝都是同一个内存地址
a=[1,2,3]
b={a:"test"}
根据key找到对应的value,是因为通过获得的哈希值(哈希表中有对应的内存地址)找到key对应的值。同一个字典中不能相同的key,因为不能有相同的哈希值。
Last modified 3yr ago