1. python基础语法
a.注释:
# 单行注释:
''' : 三引号之间的内容就是多行注释
'''
b.变量:
即拿即用:不用提前声明
apple = 100
banana = 200
c.标识符与关键字
标识符:程序员定义的符号
关键字:
import keyword
print(keyword.kwlist)
d. 数据类型:
int: float
bool: True false
str:
定义: 引号之间的内容就是字符串
访问: 下标 (-1) while for [初始位置:终止位置:步长]
for i in str:
print(i)
字符串的函数操作:
i. find rfind index rindex count
ii.split splitline partition
iii.startswith endswith isspace isalpha isalnum isdigit
iv:upper lower title
v.center ljust rjust strip lstrip rstrip
vi. join
list:
定义: [int, str, bool, list, tuple, dict]
访问: 下标 while for [初始位置:终止位置:步长]
增删改查:
i. append insert extend
ii. del pop remove
iii. dic[下标] = new_value
iv: in not in index count
排序:
sort(reverse=True, key)
tuple:
定义:(int, str, bool ,list, tuple, dict)
访问: 下标 while for [初始位置: 终止位置: 步长]
查找: in not in index count
dict:
定义:{key: value}
访问:
dic[key]
dic.get()
增删改查:
增加:dic[new_key] = value
删除: clear() del dic[key]
修改: dic[key] = value
查找:
len(dic)
keys()
values()
items()
in not in index count
set:
定义: {int, bool, str}
特点: 唯一 无序
交集与并集: & |
e. 输入输出:
输入: input: 无论输入何种类型: 到程序中都以字符串显示
输出: print: %s %d format()
f.类型转换:
数据类型(变量名)
g.运算符:
/
//
**
=
and
or
not
2.逻辑语句
a. if
i.
if 条件:
pass
ii.
if 条件:
pass
else:
pass
iii.
if 条件:
pass
elif 条件:
pass
else:
pass
b. while
while 条件:
pass
while True:
pass
c. for
for i in 可迭代对象:
pass
d. break continue
3. 函数:
a. 定义与调用:
def 函数名(形式参数):
pass
函数名(实际参数)
b. 定义函数文档
def 函数名(形式参数):
'''
函数文档书写处
'''
pass
c. 缺省参数与不定长参数
def 函数名(age = 5)
pass
def 函数名(age,*args, **kwargs)
pass
d. 返回值:
return a, b, c, d
e. 递归回归函数:
函数自己调用字节
f. 列表推导式
[i for i in range(101)]
g.匿名函数:
lambda x, y: pass
4. 文件io
with open("", '') as f
f.read()
f.write()
f.seek()
os模块:
os.rename
os.remove
os.mkdir
os.rmdir
os.getcwd()
os.chdir
os.listdir()
5. 面向对象:
类:
a.组成:
类名
类属性:
类方法:
b.定义类:
class Cat(object):
pass
对象:
对象 = 类名()