交互式环境与print输出
1.print:打印/输出
2.coding:编码
3.syntax:语法
4.error:错误
5.invalid:无效
6.identifier:名称/标识符
7.character:字符
#coding=utf-8
#如果不加编码方式,在python文档中加中文注释的时候,会有报错
try:
fh = open("test.txt","w")
fh.write("这是一个测试文件")
except IOError:
print("Error:没有找到文件或读取文件失败")
else:
print("写入内容成功")
fh.close()
#SyntaxError: invalid syntax
# 符号使用不正确或代码缩进问题,导致无效语法报错
# 符号使用不正确:只写了半个括号,使用了中文符号等
#identifier
#合法的标识符:name User user_name user_age BOOK book_name book13
#不合法的标识符:1.user&book(包含非法字符) 2.4name(不能以数字开头) 3.and(关键字,不能作为标识符)
x = "Demo"
print(x.isidentifier())
character字符:https://blog.csdn.net/weixin_33749131/article/details/85943409
字符串的操作
1.user:用户
2.name:姓名/名称
3.attribute:字段/属性
4.value:值
5.key:键
重复/转换/替换/原始字符串
1.upper:上面,小写边大写
2.lower:下面,大写变小写
3.capitalize:字符串中字母首字母大写其余小写
4.title:字符串中字母每个单词的首字母大写其余小写
5.replace:替换
6.old:旧的
7.new:新的
8.count:计数,用于统计某个元素在列表中出现的次数
9.swap:互换
10.case:情形
11.path:路径
12.new:新的/新建
13.project:项目
14.test:测试
15.file:文件
16.data:数据
a = "hello"
b = "WORLD"
c = "hello"
d = "hello world"
a1 = a.upper()
b1 = b.lower()
c1 = c.capitalize()
d1 = d.title()
print(a1)
print(b1)
print(c1)
print(d1)
# 输出:
# HELLO
# world
# Hello
# Hello World
# old -- 将被替换的子字符串
# new -- 新字符串,用于替换old子字符串
# max -- 可选字符串, 替换不超过max次
str = "this is string example....wow!!! this is really string";
print(str.replace("is", "was"))
print(str.replace("is", "was", 3))
# 输出:
# thwas was string example....wow!!! thwas was really string
# thwas was string example....wow!!! thwas is really string
aList = [123, 'xyz', 'zara', 'abc', 123];
print("Count for 123 : ", aList.count(123))
print("Count for zara : ", aList.count('zara'))
# 输出:
# Count for 123 : 2
# Count for zara : 1
a = 1
b = 2
def swap(t1, t2):
return t2, t1
print(swap(a,b))
去除/查询/计数
1.strip:去除
2.index:索引,跟count的用法很像
3.find:查找
Python3中函数find()与index()的区别
find与index都是Python3中str类型的内置类型,都是用于查找子串在字符串中第一次匹配成功并返回,区别在于当匹配不成功时,前者返回-1,而后者抛出异常。
4.count:计数
5.start:开始
6.end:结束
7.chars:字符
8.sub:sub是substitute的缩写,表示替换
9.re:regular expression的所写,表示正则表达式
# str.strip() : 去除字符串两边的空格
# str.lstrip() : 去除字符串左边的空格
# str.rstrip() : 去除字符串右边的空格
a = ' abc de a 1'
print(a.strip())
print(a.lstrip())
print(a.rstrip())
# 输出:
# abc de a 1
# abc de a 1
# abc de a 1
# index参数:
# str -- 指定检索的字符串
# beg -- 开始索引,默认为0
# end -- 结束索引,默认为字符串的长度
aList = [123, 'xyz', 'runoob', 'abc']
print("xyz 索引位置: ", aList.index( 'xyz' ))
print("runoob 索引位置 : ", aList.index( 'runoob', 1, 3 ))
# 输出:
# xyz 索引位置: 1
# runoob 索引位置 : 2
# find语法:str.find(str, beg=0, end=len(string))
# find参数:
# str -- 指定检索的字符串
# beg -- 开始索引,默认为0
# end -- 结束索引,默认为字符串的长度
str1 = "this is string example....wow!!!";
str2 = "exam";
print(str1.find(str2))
print(str1.find(str2, 10))
print(str1.find(str2, 40))
#输出:
# 15
# 15
# -1
# re详解参考:https://blog.csdn.net/qq_43088815/article/details/90214217
# re.sub是个正则表达式方面的函数,用来实现通过正则表达式,实现比普通字符串的replace更加强大的替换功能
# 如果字符串简单,如inputStr = "hello 111 world 111",通过replace,可以实现替换
inputStr = "hello 111 world 111"
replacedStr = inputStr.replace("111", "222")
print(replacedStr)
# 如果字符串复杂,如inputStr = "hello 123 world 456",想把123和456,都换成222,此时,需要re.sub
# re.sub三个必选参数:pattern, repl, string,两个可选参数:count, flags
import re
inputStr = "hello 123 world 456"
replacedStr = re.sub("\d+", "222", inputStr)
print(replacedStr)
# "\d+"表示匹配1个或更多数字
# 第一个参数:pattern,表示正则表达式中的模式字符串
# 想要把整个字符串"hello crifan, nihao crifan",换成crifanli,实现如下
inputStr = "hello crifan, nihao crifan"
replacedStr = re.sub(r"hello (\w+), nihao \1", "crifanli", inputStr)
#此句中 r 表示去掉反斜杠的转移机制
# \w+表示,一个或多个匹配的字母或数字或下划线或汉字
print ("replacedStr=",replacedStr)
# 输出:replacedStr= crifanli
# Python字符串使用r开头,用来表示去掉反斜杠的转移机制
name = "test"
path=r'z:\auto\{0}\new.sh'.format(name)
print(path)
#输出:z:\auto\test\new.sh
获取输入/格式化
1.input:输入
2.prompt:提示
3.ID:身份证
4.format:格式化
5.args(argument):参数
6.kwargs:关键字参数
7.year:年
8.month:月
9.day:日
元组
1.tuple:元组
2.max:最大
3.min:最小
4.iterable:可迭代
5.key:关键字
6.function:方法/函数
7.stop:停止
8.object:对象
循环
1.for ...in...循环的使用
2.while...循环的使用
3.range:范围
4.sep(separate):分割
5.flush:冲刷
6.step:步长
7.continue:继续
8.break:突破/跳出
列表
1.list:列表
2.reverse:反向
3.true:真
4.false:假
5.append:附加
6.extend:扩展
7.insert:插入
8.pop:取出
9.remove:移除
10.del(delete):删除
11.clear:清除
12.sort:排序
运算符与随机数
1.module:模块
2.sys(system):系统
3.path:路径
4.import:导入
5.from:从...
集合
1.set:集合/设置
2.add:添加
3.update:更新
4.discard:丢弃
5.intersection:相交
6.union:联合
7.difference:差数
8.symmetric:对称
9.in:在...里面
10.not:不/不是
11.disjoint:不相交
12.subset:子集
13.superset:父集/超集
14.copy:复制
条件/跳出与结束循环
1.if:如果
2.else:否则
字典
1.dict:字典
2.key:键/关键字
3.value:值
4.item:项
5.mapping:映射
6.seq(sequence):序列
7.from:从/来自
8.get:获取
9.default:默认
10.none:没有
11.arg:可变元素
12.kwargs:可变关键字元素
嵌套函数/作用域/闭包
1.inside:内部
2.outside:外部
3.radius:半径
4.perimeter:周长
5.case:情形