1Number类型
1类型定义
Integer,Long integer,Boolean,Double-precision float,Complex number
2操作符
比特操作符
~A 按二进制取反操作
A&B 并操作
A|B 或操作
A^B 异或操作
A>>B 按比特位右移
A<<B 按比特位左移
算数操作符 :略
3内置函数
1通用函数
cmp(A,B)比较AB大小,前者小返回-1,前者大返回1,两者相同返回0
str(A)将数字转换为可显示的字符串
type(A)返回参数的类型对象
bool(A)将参数转换为布尔类型
int(A)将参数转换为整数类型,以十进制表示
long(A)将参数转换为长整形,以十进制表示
float(A)将参数转换为浮点类型
complex(A)将参数转换为复数类型
2数值类型特定函数
abs(A)取绝对值
coerce(A,B)将AB转化为同一个类型,并生成一个元组
divmod(A,B)除模操作:生成一个元组,形式为(A/B,A%B)
pow(A,B)幂操作:A的B次方
round(A) 返回参数的四舍五入结果
hex(A) 将A转换为用十六进制的字符串
oct(A) 将A转换为用八进制表示的字符串
chr(A)将A转化为ASCII字符,要求0<=A<=255
ord(A) chr(A)的反函数
2String类型
1基本使用
python中字符串分两种:一种是普通字符串,另一种是Unicode字符串。用引号声明的字符串是普通字符串;而在因号之前加上字母u时,python会将其解释为Unicode字符串。
2字符串格式化
char = 'abcd' char = u'abcd'
3内置函数
详细参考:http://blog.luoyuanhang.com/2016/03/02/Python-%E5%AD%97%E7%AC%A6%E4%B8%B2-String-%E5%86%85%E5%BB%BA%E5%87%BD%E6%95%B0%E5%A4%A7%E5%85%A8-1/
3Tuple类型
tuple用圆括号表示,不同元素之间用逗号隔开,在初始化之后不能修改。
4List类型
内置函数
append(obj)在列表尾部添加一个对象
count(obj)计算对象在列表中出现的次数
extend(seq)将序列seq的内容添加到列表中
index(obj,i=0,j=len(list))计算对象obj在列表中的位置
insert(index,obj)把对象插入到列表index指定的位置
pop(index=-1)读取并删除index位置的对象,默认为最后一个对象
remove(obj)从列表中删除对象obj
reserve()获取反向列表
list.sort(func=None,key=None,reservw=False)以指定方式排序列表中的成员
5Dictionary类型
1类型定义
Dictionary是python中唯一表示映射关系的类,所以其有独特的定义和操作方式。
2内置函数
clear()清除字典中所有的键值对
copy()复制字典的一个副本
fromkeys(seq,val=None)用seq中的元素为键创建字典,所有键的值都设为val,val默认为None。
get(key,default=None)读取字典中的键key,返回该键值;如果找不到返回default设置的值。
has_key(key)判断key是否存在
captalize() 函数
功能
将一个字符串的第一个字母大写
用法
str.captalize()
参数
无
返回值
string
示例代码
1 |
str = "hello world!" |
运行结果
1
|
tr.capitalize(): Hello world!
|
center(width, fillchar) 函数
将字符串居中,居中后的长度为 width
功能
将字符串居中,居中后的长度为 width
用法
str.center(width[, fillchar])
参数
- 表示字符串总长度
- fillchar: 使字符串居中所填充的字符,默认为空格
返回值
返回填充字符后的字符串
示例代码
1 |
str = "hello world!" |
运行结果
1 |
tr.center(20): hello world! |
count(str, start=0, end=len(string)) 函数
功能
返回该字符串中出现某字符串序列(或字符)的次数
用法
str.count(sub, start=0, end=len(string))
参数
- sub: 被查找的字符串序列
- start: 开始查找的索引位置,默认为字符串开始
- end: 结束查找的索引位置,默认为字符串结束
返回值
被查找的序列在字符串的查找位置中出现的次数
示例代码
1 |
str = "hello world! hello world!" |
运行结果
1 |
str.count(sub): 4 |
decode(encoding=’UTF-8’,errors=’strict’) 函数 & encode(encoding=’UTF-8’,errors=’strict’)
功能
使用特定编码将字符串解码(decode)/编码(encode)
用法
str.decode(encoding='UTF-8',errors='strict')
str.encode(encoding='UTF-8',errors='strict')
参数
- encoding: 使用的编码格式
- errors: 设置不同的错误处理方法,其他选项有
ignore
,replace
,xmlcharrefreplace
,backslashreplace
返回值
编码/解码后的字符串
示例代码
1 |
str = "hello world!" |
运行结果
1 |
Encoded str: aGVsbG8gd29ybGQh |
endswith(suffix, start=0, end=len(string)) 函数
功能
判断字符串是否是以某字符串结尾的
用法
str.endswith(suffix, start=0, end=len(string))
参数
- suffix: 被查找的字符串
- start: 字符串查找的起始位置,默认为字符串起始位置
- end: 字符串查找的结束位置,默认为字符串结束位置
返回值
如果字符串是以 suffix
结尾的返回 True
, 否则返回 False
示例代码
1 |
str = "hello world!" |
运行结果
1 |
True |
expandstabs(tabsize=8) 函数
功能
提供自定义 tab(/t) 长度的方法,默认为8
用法
str.expandtabs(tabsize=8)
参数
- tabsize: 表示自定义 tab 的长度
返回值
string
示例代码
1 |
str = "hello world!" |
运行结果
1 |
Original str: hello world! |
find(str, start=0, end=len(string)) 函数
功能
在字符串的某指定位置查找某字符串
用法
str.find(str, start=0, end=len(string))
参数
- str: 被查找的子字符串
- start: 查找的起始位置,默认为字符串起始位置
- end: 查找的结束位置,默认为字符串结束位置
返回值
如果查找到,返回该子字符串的索引;未查找到,返回-1
示例代码
1 |
str = "hello world!" |
运行结果
1 |
6 |
index(str, start=0, end=len(string)) 函数
功能
功能上与 find() 相同,只是在未找到子字符串是抛出异常
用法
str.index(str, start=0, end=len(string))
参数
同 find()
返回值
如果查找到,返回该子字符串的索引;未查找到,抛出异常
示例代码
1 |
str = "hello world!" |
运行结果
1 |
6 |
isalnum() 函数
功能
判断该字符串是否只是字母数字组合
用法
str.isalnum()
参数
无
返回值
如果该字符串是字母数字组合,返回 True
,否则返回 False
示例代码
1 |
str = "helloworld" |
运行结果
1 |
True |
isalpha() 函数
功能
判断该字符串是否是字母组合
用法
str.isalpha()
参数
无
返回值
如果该字符串是字母组合,返回 True
,否则返回 False
示例代码
1 |
str = "helloworld" |
运行结果
1 |
True |
isdigit() 函数
功能
判断该字符串是否只包含数字
用法
str.isdigit()
参数
无
返回值
如果该字符串只包含数字,则返回 True
,否则返回 False
示例代码
1 |
str = "hello123" |
运行结果
1 |
False |
islower() 函数
功能
判断该字符串中是否只是小写字母
用法
str.islower()
参数
无
返回值
如果该字符串中只是小写字母,返回True
,否则返回False
示例代码
1 |
str = "hello wolrd!" |
运行结果
1 |
True |
isspace() 函数
功能
判断该字符串是否只包含空格
用法
str.isspace()
参数
无
返回值
如果该字符串只包含空格,返回True
,否则返回False
示例代码
1 |
str = " " |
运行结果
1 |
True |
istitle() 函数
功能
检查该字符串中的单词是否首字母都大写
用法
str.istitle()
参数
无
返回值
如果该字符串中的单词首字母都大写了,返回True
,否则返回False
示例代码
1 |
str = "Hello world!" |
运行结果
1 |
False |
isupper() 函数
功能
判断该字符串中的字母是否都是大写
用法
str.isupper()
参数
无
返回值
如果该字符串中的字母都是大写,返回True
,否则返回False
示例代码
1 |
str = "Hello world!" |
运行结果
1 |
False |
join(seq) 函数
功能
用该字符串连接某字符序列(seq)
用法
str.join(sequence)
参数
- sequence: 被连接的字符序列
返回值
返回连接之后的字符串
示例代码
1 |
str = "-" |
运行结果
1
|
hello-world-everyone-!
|
len(string) 函数
功能
得到该字符串的长度
用法
len(str)
参数
无
返回值
返回该字符串长度
示例代码
1 |
str = "Hello world!" |
运行结果
1
|
the length of str: 12
|
ljust(width, fillchar=’ ‘)函数
功能
在字符串的右边填充字符使得字符串达到指定长度
用法
str.ljust(width, fillchar=' ')
参数
- 填充后的目标长度
- fillchar: 用于填充的字符,默认为空格
返回值
返回填充后的字符串
示例代码
1 |
str = "Hello world" |
运行结果
1 |
Hello world |