Skip to content

入门书籍推荐:

《https://docs.python.org/zh-cn/3/tutorial/index.html》

《Python编程:从入门到实践 第3版》

《Python语言及其应用 第2版》

《Python编程快速上手:让繁琐工作自动化 第2版》

《Python基础教程 第3版》

数据类型 7条 ​

数字 整数int/浮点float/复数complex ​

字符串str ​

增:

【字符串相加】
st = python
st2 = java
print(st + st2) >>pythonjava

【字符串连接】
st_list = ['python', 'java']
print(','.join(st_list)) >>'python,java'

删除:

【去除字符串首尾空白符】
st = ' python '
print(st.strip()) >>python

【去除字符串左侧空白符】
st = ' python '
print(st.lstrip()) >>'python '

【去除字符串右侧空白符】
st = ' python '
print(st.rstrip()) >>' python'

查:

【字符串计数】
st = 'hello, world, world'
print(st.count('world')) >>2

【查找字符串返回第一次出现的索引】
st = 'hello python'
print(st.find('python')) >>6
print(st.find('world')) >>-1 #如没有返回-1

【从右向左查字符串返回第一次出现的索引】
st = 'hello python python'
print(st.rfind('python')) >>13
print(st.rfind('world')) >>-1 #如没有返回-1

【查找字符串第一次出现的索引】
st = 'hello python'
print(st.index('python')) >>6
print(st.index('world')) >>ValueError #未出现抛出ValueError异常

【查找字符串第一次出现的索引】
st = 'hello python python'
print(st.rindex('python')) >>13
print(st.rindex('world')) >>ValueError #未出现抛出ValueError异常

更改:

【转换成字符串】
st = 1234 >>1234 int
print(str(st)) >>'1234' str

【全变大写】  
st = python
print(st.upper()) >>PYTHON

【全变小写】
st = PYTHON
print(st.lower()) >>python

【首字母变大】
st = python
print(st.title()) >>Python

【大写变小写,小写变大写】
st = Python
print(s.swapcase()) >>pYTHON

【字符串处理成列表】
st = 'hello,python'
print(st.split(',')) >>['hello', 'python']

【字符串替换】
st = 'hello, python'
print(st.replace('python', 'world')) >>'hello, world'

列表list [] ​

元组tuple () ​

字典dict {} ​

集合set () ​

布尔类型bool True/False ​

控制语句 4条 ​

if语句 ​

if-else语句 ​

if-elif-else语句 ​

嵌套if ​

循环语句 4条 ​

for循环 ​

while循环 ​

终止循环break ​

跳出当前循环continue ​

函数 ​

创建函数 ​

具有返回值的函数 ​

默认值参数 ​

关键字参数 ​

嵌套函数 ​

创建对象 ​

模块 ​