Python常用关键字
关键字
1. if/elif/
else
2. for
3. while
4. pass
5. break
6. continue
7. def
8. return
9. del
10. yeild
11. globle
12. raise
13. import
14. from
15. try/except/
finally
16. asserta
17. print a,b,
18. raw_input() input()
逻辑运算
and ornot is in
特殊赋值
a,b=1,2a=1b=2a,b=b,a ##交换两个变量的内容
数字类型
import math
math.pi
math.emath.sin()abs()max()min()int()str()float()list()
字符串定义
a=
'hello'
a=
"hello"
a=
"""
hello
"""
转义 \n 切片(可用于list的分页) a[1:2:2] 索引 a[1]
字符串方法
sort()
join()
split()
strip()
rstrip()
lstrip()
replase()
endwith()
字符串格式化
a+b“%s%s”%(a,b)基于字典的格式化“%(a)s %(b)s”%(‘a’:’sasa’,’b’:’dad’)
集成开发环境:pycharm
Shift+F6 ##修改变量名 Ctrl+Alt+L ##规范化变量
列表解析
a=[1,2,3] b=[_ for _ in a if >1] ##是一个变量,结果为2,3
区别和比较 “is” 和 “==”
“==”比的是值 “is” 比的是变量指向的地址
json模块
import json
d=
"""
[
{
"id":1,
"key":1
},
{
"key":"value",
"id":2
}
]
"""
d1={
"name":
"kang",
"id":
1,
"age":
20}
print json.dumps(d1,indent=
4)
print json.loads(d)
cmd模块
import cmd
class MainCLI(cmd.Cmd):
prompt =
">>>"
def do_add(self, line):
line = line.split()
print float(line[
0]) + float(line[
1])
def do_jian(self, line):
line = line.split()
print float(line[
0]) - float(line[
1])
def do_mul(self, line):
line = line.split()
print float(line[
0]) * float(line[
1])
def do_dev(self, line):
line = line.split()
print float(line[
0]) / float(line[
1])
def do_exit(self, line=None):
""" ##help查看的文档
退出
"""
return True
MainCLI().cmdloop()