python基础操作笔记

python基础操作笔记

第二章 变量和简单的数据类型

#2.1输出数据hello world
print(’--------------------------------------------------------------------------------------------’)
print(‘hello world!!!’)

#2.2 变量
print(’--------------------------------------------------------------------------------------------’)
message=“这是**杰的第一个python”
print(message)

#2.2.1变量的命名和使用
#只能包含字母,数字,下划线 ,不能包含空格,不可用Python关键字函数命名,简短又具有描述性,慎用小写I和O

#2.3字符串
print(’--------------------------------------------------------------------------------------------’)

“wo shi zhao huajie”
‘wo shi zhao huajie’
print(“wo shi zhao huajie”)
print(‘wo shi zhao huajie’)

#2.3.1使用方法修改字符串的大小写
print(’--------------------------------------------------------------------------------------------’)
name=‘wo shi zhao huajie ,我是杰’
print(name.title()) #title()首字母变成大写
print(name.upper()) #upper()字母变成大写
name1='WO SHI ZHAO HUAJIE ,我是
杰-----------’
print(name1.lower()) #lower()字母变成小写
print(name1.title()) #title()首字母变成大写

#2.3.2 合并拼接
print(’--------------------------------------------------------------------------------------------’)
#python用+来拼接
frist_name=“zhao”
last_name=‘huajie’
full_name =frist_name+last_name
print(full_name)
print(“zhao”+‘huajie’.title())

#2.3.3使用制表符和换行符来添加空白
print(’--------------------------------------------------------------------------------------------’)
#空白泛指 非打印字符,如空格 ,制表符,换行符
#\t制表符 \n 换行符
print("\tzhao\thuajie\tzhao\tzhao\thuayi")
print("\nzhao\nhuajie\nzhao\nzhao\nhuayi")

#2.3.4删除空白
print(’--------------------------------------------------------------------------------------------’)
name="* 华杰 "
print(name.rstrip()) #rstrip()去掉末尾空白
name=" * 华杰 "
print(name.lstrip()) #lstrip()去掉首部空白

#2.3.5避免错误的语法
print(’--------------------------------------------------------------------------------------------’)
#message=‘zhao ’ huajie’
#print(message) #撇号应当位于双引号中间
message1=“zhao’huajie”
print(message1)

#2.4数字
print(’--------------------------------------------------------------------------------------------’)
#2.4.1整数 数字可以直接加减乘除,混合运算
2+3
3-2
4/2
2*3

#2.4.2浮点数 带小数的数 计算结果保存的小数不确定的
print(’--------------------------------------------------------------------------------------------’)
3.000+3
3.000-2
3.000/2
3.000*3

#2.4.3使用str()函数避免类型错误
print(’--------------------------------------------------------------------------------------------’)
age=23
#message="zhao huajie "+age+ ‘的年龄’
#print(message)
message="zhao huajie "+str(age)+ ‘的年龄’
print(message)

#2.5如何编写注释
#python中用‘#’来注释

#第三章 列表

#3.1列表是什么
print(’--------------------------------------------------------------------------------------------’)
#列表是由一组特定顺序的元素组成 ,用[]来表示
name=[‘zhao’,‘hua’,‘jie’]
print(name)
print([‘zhao’,‘hua’,‘jie’])

#3.1.1访问元素
print(’--------------------------------------------------------------------------------------------’)
name=[‘zhao’,‘hua’,‘jie’,“zheng”,“zai”,“zuo”,“shen”,“me”]
print(name[0]+name[1]+name[2]+name[3]+name[4])
print(name[0]+name[1]+name[2]+name[3]+name[4].title()+’============’)

#3.1.2 索引从0开始而不是1开始
print(’--------------------------------------------------------------------------------------------’)
name=[‘zhao’,‘hua’,‘jie’,“zheng”,“zai”,“zuo”,“shen”,“me”]
print(name[0]+name[1]+name[2]+name[-2]+name[-1].upper()+’-----------’) #python提供-1回到最后一个元素,-1…-n 指的是倒着取数

#3.2修改,删除,添加元素
print(’--------------------------------------------------------------------------------------------’)
message=[‘zhao’,‘huajie’,‘zuo’,‘shenme’,‘xianzai’]
print(message)
message[0]=‘ZHAO’ #修改元素
print(message)
message.append(‘zuoshmmene?’)#append()方法是把元素添加到列表的最后面,不影响其他的元素
print(message)
message.insert(0,‘WEIDADE’)#insert(0,‘WEIDADE’)方法是把元素插入到列表的指定的位置,其他元素后移
print(message)
message=[‘zhao’,‘huajie’,‘zuo’,‘shenme’,’=============’]
print(message)
del message[-1] #del语句是删除对应的数据
print(message)

print(’\t’)
print(’--------------------------------------------------------------------------------------------’)
flag=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag)
print(flag.pop())#po()方法是删除列表最后一个元素,并且让你继续获取到它。
print(flag)

flag1=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag1)
#print(flag1[0].pop()+‘pop删除指定的元素’)此语法错误
print(flag1.pop(-1)+’------pop删除指定列表的元素’)
print(flag1)

flag2=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag2)
flag2.remove(‘小汽车’)#根据具体的列表的值删除
print(flag2)

flag2=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag2)
flag2.remove(‘小汽车’)#根据具体的列表的值删除,并且只删除第一个匹配到的值
print(flag2)
print(’--------------------------------------------------------------------------------------------’)

#3.3组织列表

#3.3.1使用方法sort()对列表永久排序
print(’--------------------------------------------------------------------------------------------’)

many=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(many)
many.sort()#按照字母的顺序进行永久的 正序 排序
print(str(many)+‘正序’)
many=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
many.sort(reverse=True)#按照字母的顺序进行永久的 相反 排序
print(str(many)+‘相反’)

#3.3.2使用方法sorted()对列表永临时的排序
print(’--------------------------------------------------------------------------------------------’)
many1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(many1)
sorted(many1)#按照字母的顺序进行永久的 临时正序 排序
print(str(sorted(many1))+‘临时正序’)
many1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
sorted(many1,reverse=True)#按照字母的顺序进行永久的 临时相反 排序
print(str(sorted(many1,reverse=True))+‘临时相反’)
print(many1)

#3.3.3倒着打印列表
print(’--------------------------------------------------------------------------------------------’)
manySo=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(manySo)
print(manySo.reverse())#reverse()倒着打印列表

#3.3.4确定列表的长度
print(’--------------------------------------------------------------------------------------------’)
manySo=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(manySo)
print(len(manySo))#len()确定列表的长度

#3.4使用列表避免索引错误
#索引从0开始

#第四章 操作列表

#4.1遍历整个列表
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls) #记得要空格print前面,否则报错。

#4.1.1深入研究循环
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls) #记得要空格print前面,否则报错。

#4.1.2循环中执行更多的操作
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。

#4.1.3循环结束执行一些操作
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。
print(“循环数据结束完了!!!!”) #这句话不在循环内部,print()前不需要加空格;;;;

#4.2避免缩进错误
print(’--------------------------------------------------------------------------------------------’)
#4.2.1忘记缩进

#4.2.2忘记缩进额外的代码行

#4.2.3没必要的缩进
ls=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
#print(ls)#没必要的缩进

#4.2.4循环后没必要的缩进
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。

print(“没必要的缩进!!!!”) #这句话不在循环内部,print()前不需要加空格;;;;缩进后会添加到上面的循环

#4.2.5遗漏了冒号

#4.3创建数字列表

#4.3.1使用range函数
print(’--------------------------------------------------------------------------------------------’)
for ls in range(1,10): #range(n,m)开始数字n,结束数字m ,包含n,不包含m
print(ls)

#4.3.2使用range函数创建数字列表
print(’--------------------------------------------------------------------------------------------’)
num=list(range(1,10))#list()函数将其转换为列表
print(num)

num=list(range(1,10,2))#list()函数将其转换为列表,range(n,m,o) o为步长
print(num)

#4.3.3对列表的数字进行简单的计算
print(’--------------------------------------------------------------------------------------------’)
numNm=list(range(1,15,3))
print(min(numNm))#最小值
print(max(numNm))#最大值
print(sum(numNm))#求和

#4.3.4列表解析
print(’--------------------------------------------------------------------------------------------’)
squares=[val**2 for val in range(1,10)] #获取数据的平方值得列表,注意这里没有冒号“:”
print(squares)
print([val for val in range(1,10)])
print(list(range(1,10))) #print(list(range(1,10)))和上面print([val for val in range(1,10)])结果一样

#4.4使用列表的一部分(切片)

#4.4.1切片
print(’--------------------------------------------------------------------------------------------’)
listM=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listM)
print(listM[0:3]) #获取列表前三个元素 列表名[n:m]n开始位置包含,m结束位置不包含
print(listM[:3]) #列表名[:m] 没有指定第一个则从头开始到m
print(listM[0:]) #列表名[n:]没有指定尾,则从n到最后
print(listM[-3:]) #列表名[n:]没有指定尾,则从n到最后,倒着数

#4.4.2遍历切片
print(’--------------------------------------------------------------------------------------------’)
listM1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listM1)
for a in listM1[:3]:
print(a)

#4.4.2复制列表
print(’--------------------------------------------------------------------------------------------’)
listM1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(str(listM1)+’---------listM1’)
listM2=listM1 #此种复制方法是列表名字改了,集体的列表物理地址没变化.
listM3=listM1[:] #此种切片复制列表的方法,是建立了新的列表,物理地址发生变化
listM1.append(‘王八初始化’)
listM2.append(‘王八羔子’)
listM3.append(‘王八犊子’)
print(str(listM1)+’---------listM11’)
print(str(listM2)+’---------listM2’)
print(str(listM3)+’---------listM3’)

#4.5元组

#4.5.1元组定义
print(’--------------------------------------------------------------------------------------------’)
#元组犹如列表,但是不是使用方括号,而是使用圆括号,定义元组可以用索引来访问,就像列表一样。
data=(100,100,200,4000) #定义元组
print(data[0]) #使用元组
print(data[1])
#data[1]=300 #报错,python不能给元组的元素赋值;

#4.5.2遍历元组的元素
print(’--------------------------------------------------------------------------------------------’)
data1=(100,100,200,4000) #定义元组
for data in data1: #遍历元组
print(data) #循环打印数据

#4.5.3修改元组变量
print(’--------------------------------------------------------------------------------------------’)
data2=(100,100,200,4000) #定义元组
for d in data2: #遍历元组
print(d) #循环打印数据
print(‘没赋值元组变量的值’)

data2=(1000,1000,2000,40000) #定义元组
for d in data2: #遍历元组
print(d) #循环打印数据
print(‘赋值元组变量后的值’)

#4.6设置代码格式
print(’--------------------------------------------------------------------------------------------’)
#4.6.1格式指南
print(’--------------------------------------------------------------------------------------------’)
#4.6.2缩进
print(’--------------------------------------------------------------------------------------------’)
#4.6.3行长
print(’--------------------------------------------------------------------------------------------’)
#在大多数的计算机中,终端的行长为79个字符,
#4.6.4空行
print(’--------------------------------------------------------------------------------------------’)
#程序不同部分用空行隔开,尽量用一个空行隔开,不然影响可读性。
#4.6.5其他格式设置指南
print(’--------------------------------------------------------------------------------------------’)
#pep8参考

#第五章 if语句

#5.1一个简单例子
print(’--------------------------------------------------------------------------------------------’)
listIf=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’,‘if’]
for lf in listIf:
if lf==‘if’: #这是if的判断的写法
print(‘这是第一个if’)
else:
print(lf+‘这是其他的元素!!!’)

#5.2条件测试
print(’--------------------------------------------------------------------------------------------’)
#5.2.1检查是否相等
car=‘123’
car==‘123’
print(car==‘123’)#检查俩个元素是否相等用‘’;
car
’345’
print(car==‘133’)

#5.2.2检查是否相等考虑大小写
print(’--------------------------------------------------------------------------------------------’)
car=‘qwe’
car==‘QWE’
print(car==‘QWE’)#检查俩个元素是否相等用‘’,且考虑大小写;
car
’345’
print(car==‘133’)
print(’----转换为小写----’)
print(car.lower()‘QWE’.lower())#方法lower(),upper()进行转换;
print(’----转换为大写----’)
print(car.upper()
‘QWE’.upper())

#5.2.3检查是否不相等
print(’--------------------------------------------------------------------------------------------’)
car=‘qwe’
car==‘QWE’
print(car==‘QWE’)#检查俩个元素是否不相等用‘!=’,且考虑大小写;
car!=‘345’
print(car!=‘133’)
print(’----转换为小写----’)
print(car.lower()!=‘QWE’.lower())#方法lower(),upper()进行转换;
print(’----转换为大写----’)
print(car.upper()!=‘QWE’.upper())

#5.2.4比较数字
print(’--------------------------------------------------------------------------------------------’)
print(’—比较数字----’)
age=18
age==18
print(age>12)
print(age>=12)
print(age<19)
print(age<=19)
print(age>19)
print(age>=19)

#5.2.5检查多个条件
print(’--------------------------------------------------------------------------------------------’)
print(’—检查多个条件----’)
age=18
sex=‘男’
print(age18 and sex’男’) #使用and检查多个条件
print(age18 or sex’女’) #使用or检查多个条件

#5.2.6/7检查特定值是否包含/不包含在列表中
print(’--------------------------------------------------------------------------------------------’)
print(’—检查特定值是否包含在列表中----’)
listIn=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(‘人’ in listIn) #检查特定值是否包含在列表中
print(‘人’ not in listIn) #检查特定值是否包含在列表中

#5.2.8布尔表达式
print(’--------------------------------------------------------------------------------------------’)
print(’—布尔表达式----’)
testTF=True #T大写
testTF=False #F大写

#5.3if语句

#5.3.1简单的if语句
print(’--------------------------------------------------------------------------------------------’)
print(’—简单的if语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)

#5.3.2if-else语句
print(’--------------------------------------------------------------------------------------------’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
else:
print(‘这是ok的else测试!!!’)

#5.3.2if-elif-else语句
print(’--------------------------------------------------------------------------------------------’)
print(’—if-elif-else语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
elif age==19:
print(‘这是ok的elif测试!!!’)
else:
print(‘这是ok的else测试!!!’)

#5.3.3/4/5使用elif语句
print(’--------------------------------------------------------------------------------------------’)
print(’—if-elif-else语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
elif age19:
print(‘这是ok的elif测试!!!’)
elif age
20:
print(‘这是ok的elif2222测试!!!’)
else:#else可以省略不写
print(‘这是ok的else测试!!!’)

#5.4使用if语句处理列表
print(’--------------------------------------------------------------------------------------------’)
print(’—使用if语句处理列表----’)

#5.4.1检查特殊元素
print(’--------------------------------------------------------------------------------------------’)

#5.4.2确认列表是否为空
print(’--------------------------------------------------------------------------------------------’)
testNull=[]
if testNull:#判读列表是否为空
print(‘列表不是空的,可以执行下面的语句’)
else:
print(‘列表为空的,可以执行下面的语句’)

#5.4.3使用多个列表
print(’--------------------------------------------------------------------------------------------’)
testNum1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
testNum2=[5,6,7,8,9,10,11,12,13,14,15,20]
for Num2 in testNum2:
if Num2 in testNum1:
print(‘数字--------------’+str(Num2))
else:#注意缩进,否则报错IndentationError: unindent does not match any outer indentation level
print(‘数字不在--------------’+str(Num2))

#5.5设置if的格式
print(’--------------------------------------------------------------------------------------------’)

#第六章 字典

#6.1一个简单的例子
print(’--------------------------------------------------------------------------------------------’)
print(’-------------------字典---------------------------’)
#字典是由大括号括起来的键值对,
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example[‘本人’])
print(str(example[‘人口总数’])+‘人’)

#6.2使用字典
#6.2。1使用字典
print(’--------------------------------------------------------------------------------------------’)
print(example[‘laopo’])

#6.2.2添加键值对
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)

#6.2.3先创建一个新字典
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)

#6.2.3修改字典的值
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人’]=’*更新1’
example[‘小舅子’]=’***1’
print(example)

#6.2.5删除字典的键值对
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人1’]=’*更新1’
example[‘小舅子1’]=’***1’
print(example)
del example[‘丈人1’]#删除字典的键值对,删除的键值对永远的消失了
print(example)

#6.2.6删除字典的键值对
print(’--------------------------------------------------------------------------------------------’)
example={
‘本人’:‘杰’,
‘laopo’:’
*’
}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人1’]=’*更新1’
example[‘小舅子1’]=’***1’
print(example)
del example[‘丈人1’]#删除字典的键值对,删除的键值对永远的消失了
print(example)

#6.3遍历字典

#6.3.1遍历所有的键值对
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key,value in example.items(): #使用items()方法
print(“key:”+str(key)+"---------value:"+str(value))

#6.3.2遍历所有的键
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key in example.keys():#使用keys()方法
print(“key:”+str(key)+"---------这是key.")

#6.3.3按顺序遍历所有的键
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key in sorted(example.keys()):#使用sorted()方法排序
print(“key:”+str(key)+"---------这是key.")

#6.3.4按顺序遍历所有的值
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example)
del example[‘人口总数’]#类型不一样无法遍历,报错
example[‘本人1’]=’**杰’#添加键不同,但值相同的元素
example[‘本人’]=’**杰’#修改键的值
print(example)
for value in set(sorted(example.values())): #set()方法是 去掉键值对的值相同的重复数据
print(“value:”+str(value)+"---------value.")

#6.4嵌套
#6.4.1字典列表
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:'’}
example1={‘兄长’:'毅’,‘嫂子’:‘青’}
example2={'父
’:'
’,'母
’:’***’}
example3={‘长侄’:’***瑞’,‘侄女’:’***嘉’}
example4={‘大犬’:’***琳’,‘人口总数’:9}
print(example0,example1,example2,example3,example4)
#列表
exampleSome=[example0,example1,example2,example3,example4]
print(exampleSome)

#6.4.2 字典存储列表
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:[’*’,’***1’,’***2’]}
print(example0)
#列表
listName0=[‘1’,‘2’,‘3’]
listName1=[‘1’,‘2’,‘3’]
listName2=[‘1’,‘2’,‘3’]
#字典存储列表
exampleName={1:listName0,2:listName1,3:listName2}
print(exampleName)

#6.4.2 字典存字典
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:[’*’,’***1’,’***2’]}
print(example0)
#字典
listName0={1:‘1’,2:‘2’,3:‘3’}
listName1={1:‘1’,2:‘2’,3:‘3’}
listName2={1:‘1’,2:‘2’,3:‘3’}
#字典存储字典
exampleName={1:listName0,2:listName1,3:listName2}
print(exampleName)

#第七章 用户输入和Wile循环

#7.1函数input的工作原理
print(’--------------------------------------------------------------------------------------------’)
#message=[input(“请输入一个值:::::”)]#没办法在这里执行,使用input()则可以从键盘获取值。
#print(message)

#7.1.1/2函数int()来获取数值输入
print(’--------------------------------------------------------------------------------------------’)
#age=[input(“请输入一个值:::::”)]#没办法在这里执行,使用input()则可以从键盘获取值。
age=int(age)#用int()函数来转换为int类型
#print(age)

#7.1.3求模运算
print(’--------------------------------------------------------------------------------------------’)
4%3
print(4%3)

#7.2while循环

#7.2.1使用while循环
print(’--------------------------------------------------------------------------------------------’)
num=1
while num<=5:#while循环 冒号注意
print(num)#缩进注意
num+=1 #循环累加

#7.2.2让用户选择何时退出
print(’--------------------------------------------------------------------------------------------’)
ptmopt="\n 输入数字"
ptmopt+="\t 否则’quit’ 程序!!!"
message=""
#while message!=‘quit’: #while循环 冒号注意
#message=input(ptmopt) #输入信息
#print(message)#缩进注意

#7.2.3使用标志
print(’--------------------------------------------------------------------------------------------’)
#message=True
#while message: #while循环 冒号注意
#message=input(ptmopt) #输入信息
#print(message)#缩进注意

#7.2.4使用break退出循环
print(’--------------------------------------------------------------------------------------------’)
num=1
while num<=5:#while循环 冒号注意
print(num)#缩进注意
if num==2:
break #根据条件中断循环
num+=1 #循环累加

#7.2.5使用continue退出循环
print(’--------------------------------------------------------------------------------------------’)
#num=1
#while num<=5:#while循环 冒号注意
#print(num)#缩进注意
#num+=1 #循环累加
#if num==2:
#ontinue #根据条件加速循环

#7.2.6避免死循环
print(’--------------------------------------------------------------------------------------------’)

#7.3使用while循环处理列表和字典

#7.3.1 在列表中移动元素
print(’--------------------------------------------------------------------------------------------’)

#创建用户列表
#创建存储列表
oldlist = [‘1’,‘2’,‘3’,‘4’]
newlist = []

while oldlist: #while循环遍历列表的时候自动判断遍历结束,语法为此。
oldlistone=oldlist.pop() #
print(‘这是取出的元素-----’+oldlistone)
newlist.append(oldlistone)
print(‘验证循环结束!!!’)

#显示已验证的用户列表
for newls in newlist:
print(‘已经验证的用户信息----’+newls.title())
print(‘查询已经验证循环结束!!!’)

#查看被遍历的用户列表
for oldls in oldlist:#列表为空
print(‘被验证的用户信息----’+oldls.title())
print(‘查询被验证循环结束!!!’)

#7.3.2 删除包含特定值得所有列表元素
print(’--------------------------------------------------------------------------------------------’)

pets = [‘cat’,‘cat’,‘dog’,‘fish’,‘rabbit’,‘snake’]
print(pets)
while ‘cat’ in pets: #判断此值是否在列表中
pets.remove(‘cat’) #删除列表特定的值;
print(pets)

#7.3.3 使用用户输入填充字典
print(’--------------------------------------------------------------------------------------------’)

python的多行注释用“’’’”

‘’’
#创建字典
petsnew = {}
print(petsnew)
#循环条件表示
flag=True
#给字典加入元素
while flag: #让循环执行
name=input(’\n输入一个名字’)
respones=input(’\n回答的问题’)
petsnew[name]=respones
repeat=input(’\n是否有人需要调查??????’)
if repeat==‘no’:
flag=False
print(’\n----------调查结束------’)

#显示调查的结果
for key,value in petsnew.items():
print(‘这是调查的结果元素-------key:’+key+’-------values:’+value)
print(‘程序结束!!!’)

print(petsnew)
‘’’

#第八章 函数

#8.1定义函数
print(’--------------------------------------------------------------------------------------------’)
def greet_people():#创建函数 def 函数名字():
“”“显示简单的问候语!!!!!!!”""
print(‘hello world!!!’)

greet_people()#调用函数

#8.1.1向函数传递信息
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username):#创建函数 def 函数名字(参数):
“”“显示简单的问候语!!!!!!!”""
print(username+’---------------------hello world!!!’)

greet_people(‘zhaohuajie’)#调用函数

#8.1.2实参和形参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username):#创建函数 def 函数名字(参数):username为形参
“”“显示简单的问候语!!!!!!!”""
print(username+’---------------------hello world!!!’)

greet_people(‘zhaohuajie’)#调用函数, 'zhaohuajie’为实参

#8.2传递实参

#8.2.1位置实参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets):#创建函数 def 函数名字(参数): , username,pets为位置形参
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

greet_people(‘zhaohuajie’,‘cat’)#调用函数, ‘zhaohuajie’,'cat’为实参
greet_people(‘zhaohuajie1’,‘cat1’)#调用函数, ‘zhaohuajie’,'cat’为实参

#8.2.2关键字实参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets):#创建函数 def 函数名字(参数): , username,pets为位置形参
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样;
greet_people(username=‘zhaohuajie’,pets=‘cat’)#调用函数,
greet_people(pets=‘cat’,username=‘zhaohuajie’)#调用函数,

#8.2.3 默认值
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets=‘cat1’):#创建函数 def 函数名字(参数): , username,pets为位置形参,pets=‘cat1’给参数默认值
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样;
greet_people(username=‘zhaohuajie’)#调用函数, pets默认值为cat1
greet_people(pets=‘cat’,username=‘zhaohuajie’)#调用函数, 给pets重新赋值

#8.2.4 等效的函数调用
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets=‘cat1’):#创建函数 def 函数名字(参数): , username,pets为位置形参,pets=‘cat1’给参数默认值
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样,为等效的函数调用
greet_people(username=‘zhaohuajie111111’)#调用函数, pets默认值为cat1
greet_people(pets=‘cat2222222’,username=‘zhaohuajie11111’)#调用函数, 给pets重新赋值

#8.2.5 避免错误
print(’--------------------------------------------------------------------------------------------’)

#8.3 返回值

#8.3.1 返回简单值
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last):
full_name=first+" "+last
return full_name.title() #设定返回值

full_name=fomatted_name(‘zhao’,‘huajie’)
print(full_name)

#8.3.2 让实参变成可选的
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last,middle=’’):#注意默认的字符串middle必须在末尾
if middle:#判断是否为空
full_name=first+middle+last
else:#else后的冒号不可少
full_name=first+’’+last
return full_name.title() #设定返回值

full_name=fomatted_name(‘zhao’,’—’,‘huajie’)
full_name1=fomatted_name(‘zhao’,‘huajie’)
print(full_name1)
print(full_name1)

##8.3.3 返回字典
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last,middle=’’):#注意默认的字符串middle必须在末尾
full_name={‘first’:first,‘last’:last} #设置返回值为字典
return full_name #设定返回值
full_name=fomatted_name(‘zhao’,’—’,‘huajie’)
full_name1=fomatted_name(‘zhao’,‘huajie’)
print(full_name)
print(full_name1)

#8.3.3 结合函数和while循环
print(’--------------------------------------------------------------------------------------------’)

#8.4 传递列表
print(’--------------------------------------------------------------------------------------------’)

def fomatted_name(first,last,listone):#传递列表
full_name=first+" "+last+’********’+listone
return full_name.title() #设定返回值

listone=[‘1’,‘2’,‘3’,‘4’] #列表
full_name=fomatted_name(‘zhao’,‘huajie’,str(listone))
print(full_name)

#8.4.1 在函数中修改列表
print(’--------------------------------------------------------------------------------------------’)

#8.4.2 禁止在函数中修改列表
print(’--------------------------------------------------------------------------------------------’)
full_name=fomatted_name(‘zhao’,‘huajie’,str(listone[:]))#切片方法是将列表的副本传递给函数,对原来函数无影响
print(full_name)

#8.5 传递任意数量的实参
print(’--------------------------------------------------------------------------------------------’)
def pizza(top): #传递任意数量的实参用 ‘’ 号表示,*只能在top前面
print(top)

pizza(‘1’)
pizza(‘1’,‘2’,‘3’,‘4’,‘5’)

#8.5.1 位置实参和任意数量的实参
print(’--------------------------------------------------------------------------------------------’)
def pizza2(name,top): #传递任意数量的实参用 ‘’ 号表示,*只能在top前面,name为位置实参
print(name,top)
pizza2(‘zhaohuajie’,‘1’)
pizza2(‘zhaohuajie’,‘1’,‘2’,‘3’,‘4’,‘5’)

#8.5.2 位置实参和任意数量的关键字实参
print(’--------------------------------------------------------------------------------------------’)
def pizza3(name,user_info): #传递任意数量的实参用 ‘’ 号表示,只能在top前面,name为位置实参,为俩个
print(name,user_info)
pizza3(‘zhaohuajie’,user_info1=‘这是一’,user_info2=‘这是二’)

#下面代码相当于上面的代码
def pizza3(name,user_info): #传递任意数量的实参用 ‘**’ 号表示,只能在top前面,name为位置实参,为俩个
print(name,user_info)
user_info={‘user_info1’:‘这是一121’,‘user_info2’:‘这是二1323’}
pizza3(‘zhaohuajie’,user_info)

#8.6将函数存储在模块中

#8.6.1 导入整个模块
print(’------------------------------------------------------------------------------------------------’)
import testFuncthion #导入别的文件
print(’--------------------调用函数pizza22-------------------------------------------------------------’)
testFuncthion.pizza22(‘zhaohuajie’)#调用别的文件的函数

print(‘测试调用函数从别的文件里::::’,str(testFuncthion.pizza22(‘zhaohuajie’)))

#8.6.2 导入特定的函数
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import pizza22, pizza22 #导入别的文件里的特定的几个函数
print(’--------------------导入别的文件里的特定的函数--------------------------------------------------’)
pizza22(‘zhaohuajie’)#调用别的文件的函数,不需要在写文件名字了

print(‘测试调用函数从别的文件里,不需要再写文件名字::::’,str(pizza22(‘zhaohuajie’)))

#8.6.3 使用as给函数指定别名
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import pizza22 as mp #导入别的文件里的特定的函数,给函数取别名
print(‘测试调用函数mp从别的文件里,不需要再写文件名字::::’,str(mp(‘zhaohuajie’))) #调用别名函数

#8.6.4 使用as给模块定别名
print(’------------------------------------------------------------------------------------------------’)
import testFuncthion as tf #导入模块并使用别名
print(‘测试用模块别名获取函数::::’,str(tf.pizza22(‘zhaohuajie’))) #使用别名获取函数

#8.6.5 导入模块中的所有函数
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import * #导入模块中的所有函数
print(‘测试用*代替函数::::’,str(pizza22(‘zhaohuajie’))) #调用其中一个函数

#第九章 类

#9.1 创建和使用类
print(’----------------------------------------类--------------------------------------------------’)
#面向对象编程是最有效的编程之一

#9.1.1 创建dog类
print(’--------------------------------------------------------------------------------------------’)
class Dog():
#模拟小狗的类
def init(self,name,age): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.age=age

 def sit(self): #类中的方法
 		#模拟小狗下蹲
 		print(self.name.title()+'is no down!!!')
 	
 def roll_over(self):
 		#模拟小狗被命令时打滚
 		print(self.name.title()+'is no roll_over!!!')

#9.1.2 根据类创建实例
print(’--------------------------------------------------------------------------------------------’)
print(’----------根据类创建实例-----------’)
my_dog=Dog(‘huahua’,90)#创建类的实例
print(‘我的小狗—’+my_dog.name.title()+‘跑的快!!!它’+str(my_dog.age)+‘岁。’) #调用类里的属性
my_dog.sit() #调用类的方法
my_dog.roll_over() #调用类的方法

my_dog1=Dog(‘huahua’,90)#创建类的多个实例
print(‘我的小狗1—’+my_dog1.name.title()+‘跑的快!!!它’+str(my_dog1.age)+‘岁。’) #调用类里的属性
my_dog1.sit() #调用类的方法

my_dog1.roll_over() #调用类的方法

#9.2使用类和实例

#9.2.1 Car类
print(’--------------------------------------------------------------------------------------------’)
class Car():
#模拟车的类
def init(self,name,make,model,year): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=year

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()

my_car=Car(‘宝骏’,‘MA’,‘A4’,1989)
print(‘在调用自定义方法’)
print(’**杰自定义的函数返回值:::::’,my_car.get_des_name())

#9.2.2 给属性指定符默认值
print(’--------------------------------------------------------------------------------------------’)
class Car1():
#模拟车的类
def init(self,name,make,model): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=2000 #给属性指定赋默认值————————————————————————————————————————————————————————————

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()

my_car1=Car1(‘宝骏’,‘MA’,‘A4’)
print(‘在调用自定义方法’)
print(‘给属性指定赋默认值,自定义的函数返回值:::::’,my_car1.get_des_name())

#9.2.2 修改属性的值
print(’--------------------------------------------------------------------------------------------’)

#直接修改属性值
my_car2=Car1(‘宝骏’,‘MA’,‘A4’)
print(‘未修改属性值前-------’,my_car2.get_des_name())
my_car2.name=‘宝马’ #修改其中的某个属性;*****************************************
print(‘修改属性值后---------’,my_car2.get_des_name())

#提过方法修改属性值
class Car3():
#模拟车的类
def init(self,name,make,model): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=2000 #给属性指定赋默认值————————————————————————————————————————————————————————————

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()
 def edit(self,name,make,model): #类中的方法
 		#方法修改属性的值
 		self.name=name #把name值给self的name
 		self.make=make
 		self.model=model #把name值给self的name
 		self.year=2000  #给属性指定赋默认值————————————————————————————————————————————————————————————

my_car3=Car3(‘宝骏’,‘MA’,‘A4’)
print(‘方法未修改属性值前-------’,my_car3.get_des_name())
my_car3.edit(‘宝驴’,‘MAa’,‘A5’) #调用方法修改其中的某个属性;*****************************************
print(‘方法修改属性值后测试---------’,my_car3.get_des_name())

#通过方法对属性的值进行递增
“”" #段落注释前面不可以有空格
my_car4=Car3(‘宝骏’,‘MA’,‘A4’)
print(‘方法未修改属性值前-------’,my_car4.get_des_name1())
my_car4.newNAME=1000
print(‘测试循环增加------------’)
my_car4.edit_Updata(3000) #调用方法修改其中的某个属性;*****************************************
print(‘方法修改属性值后111111---------’,my_car4.get_des_name1())

“”"

#9.3 继承
#9.3.1/2/3/4 继承
print(’--------------------------------------------------------------------------------------------’)
class newCar(Car):
#电动汽车的独特处
def init(self,name,make,model,year): #init()方法,类中的函数称为方法,self方法参数不可少
super().init(name,make,model,year) #继承父类的属性和方法
self.flag=0 #给子类定义属性
def allP(self):#给子类定义方法
return self.flag
def get_des_name(self): #类中的方法
#模拟描述返回信息
long_name=self.name+’—’+self.make+’—’+self.model+’—’+str(self.year)+’—’+str(self.flag)
return long_name.title()
my_NewCar=newCar(‘皮皮虾’,‘吃的’,‘好玩’,1990)
print(‘调用子类的方法-------’,my_NewCar.allP())
print(‘调用子类重写父类的方法-------’,my_NewCar.get_des_name())

#9.3.5 将实例用作属性
#self.name=Car() 属性值为Car类

#9.4导入类
#9.4.1导入单个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car #把python文件的Car类导入到此文件夹

#9.4.2 在一个模块中存储多个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car #把python文件的Car类导入到此文件夹

#9.4.3 C从一个模块中导入多个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car,Dog #把python文件的Car,Dog类导入到此文件夹

#9.4.4 导入整个模块
print(’--------------------------------------------------------------------------------------------’)
#import python #把python文件导入

#9.4.5 导入整个模块
print(’--------------------------------------------------------------------------------------------’)
#from python import * #导入python模块中的所有类

#9.4.6 在一个模块导入另外一个模块
print(’--------------------------------------------------------------------------------------------’)

#9.4.7 自定义工作流
print(’--------------------------------------------------------------------------------------------’)

#9.5 Python标准库
print(’--------------------------------------------------------------------------------------------’)

#9.6 类编码风格
print(’--------------------------------------------------------------------------------------------’)
#驼峰命名法,首字母大写,而不是用下划线
#实例名或者模块名采用小写,单词之间用下划线分割

第十章 文件和议程

#10.1 从文件读取数据
print(’--------------------------------------------------------------------------------------------’)
#从文件读取数据,首先把文件读取到内存中。

#10.1.1/2 读取整个文件/文件路径
print(’--------------------------------------------------------------------------------------------’)
print(’------读取整个文件-------’)
url1=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
url2=‘E:\gongzuo_hc\UE_bak_file\read_test.txt’
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.1.3 逐行读取
print(’--------------------------------------------------------------------------------------------’)
print(’------逐行读取-------’)
url1=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
url2=‘E:\gongzuo_hc\UE_bak_file\read_test.txt’
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
for line in first_object:#for循环逐行读取
print(line.rstrip(),‘测试是否空行—’) #删除尾部空行
print(line,‘测试是否空行—’) #删除尾部空行

#10.1.4 创建包含文件各行的列表
print(’--------------------------------------------------------------------------------------------’)
print(’------创建包含文件各行的列表-------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()
listnew=[] #定义列表
for line in lines:#for循环逐行读取
print(line.rstrip()) #rstrip()删除尾部空行
listnew.append(line.rstrip())#添加元素到列表
print(listnew)#打印列表

#10.1.5 使用文件内容
print(’--------------------------------------------------------------------------------------------’)
#拼接,存入列表 等

#10.1.4 包含一百万位的大文件
print(’--------------------------------------------------------------------------------------------’)
print(’------创建包含文件各行的列表-------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test_million.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()#读取一行数据
pi_string=’’ #定义字符串
for line in lines:#for循环逐行读取
pi_string+=line.rstrip()#读取每行拼接在一起
print(pi_string[:52]+’…’)#截取字符串的前52位 pi_string[:52]

#10.1.5圆周率包含你的生日吗?
print(’--------------------------------------------------------------------------------------------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test_million.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()#读取一行数据
pi_string=’’ #定义字符串
for line in lines:#for循环逐行读取
pi_string+=line.strip()#读取每行拼接在一起
#print(pi_string)
if ‘19890704’ in pi_string:
print(‘19890704…’)#文件是否包含某个字符串
else:
print(‘不存在…’)

#10.2 写入文件

#10.2.1写入空文件
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘w’) as write_file: #注意写入的时候传参 w 需要加单引号。
write_file.write(“我爱***************!!!!!!!!!!!!!!!”)
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.2.2写入多行
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘w’) as write_file: #注意写入的时候传参 w 需要加单引号。
write_file.write(“我爱***************!!!!!!!!!!!!!!!”)
write_file.write(“可爱的小孩子!!!!!!!!!!!!!!!\n")#上面俩句为追加
write_file.write("我爱
*************!!!!!!!!!!!!!!!\n”)
write_file.write("**可爱的小孩子!!!!!!!!!!!!!!!\n")#上面俩句为换行追加
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.2.2附加到文件
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘a’) as write_file: #注意附加到文件传参 a 需要加单引号。
write_file.write(“附加我爱!!!!!!!!!!!!!!!")
write_file.write(“附加*******************可爱的小孩子!!!!!!!!!!!!!!!\n”)#上面俩句为追加
write_file.write("附加
我爱!!!!!!!!!!!!!!!\n”)
write_file.write(“附加*******************可爱的小孩子!!!!!!!!!!!!!!!\n”)#上面俩句为换行追加
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.3 异常
#10.3.1处理ZeroDivisionError异常
print(’--------------------------------------------------------------------------------------------’)
#print(9/0)

#10.3.2使用try-except代码块
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/0)
except:
print(‘0 不可以做除数-----------’)

#10.3.3使用异常避免奔溃
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/0)
except:
print(‘0 不可以做除数-----------’)

#10.3.4 else代码块
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/1)
except:
print(‘0 不可以做除数-----------’)
else:
print(‘输出数据------------------’)

#10.3.5/6 处理FileNotFoundError异常/分析文本
print(’--------------------------------------------------------------------------------------------’)
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘文件不可以找到*FileNotFoundError异常’)
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)

#10.3.7 使用多个文本
print(’--------------------------------------------------------------------------------------------’)
‘’’
def ff():
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘文件不可以找到*FileNotFoundError异常’)
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)
‘’’
#10.3.7 失败时一声不吭
print(’--------------------------------------------------------------------------------------------’)
‘’’
def ff():
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘pass’) #在此处失败时一声不吭
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)
‘’’

#10.3.8 决定报告哪些错误
print(’--------------------------------------------------------------------------------------------’)

#10.4存储数据
#10.4.1/2使用json.dump()和json.load()/保存用户读写的数据
print(’--------------------------------------------------------------------------------------------’)
#json.dump()存储数据,包含俩个参数,存储的数据,存储到的文件。
import json as js
numbers=[1,2,3,4,5,6]
filename1=‘E:/gongzuo_hc/UE_bak_file/jsonfile.json’
with open(filename1,‘w’) as j_f:
#dump(numbers,j_f)第一个参数为写入内容,第二个要写的文件
js.dump(numbers,j_f)#这个地方写反了,找了一个小时,真的很草他妈,吃屎的。。。。
print(‘写入结束!!!!!!!!!!dump(numbers,j_f)第一个参数为写入内容,第二个要写的文件!!!!!!!!!’)
with open(filename1,‘r’) as j_f_r:
numbers=js.load(j_f_r) #读取json文件的信息;
print(numbers)

#10.4.3 重构
print(’--------------------------------------------------------------------------------------------’)
#就是使用函数包装代码

#第十一章 测试代码
#11.1 测试函数
print(’--------------------------------------------------------------------------------------------’)

#11.1.1 单元测试和测试用例
print(’--------------------------------------------------------------------------------------------’)

#11.1.2 可通过的测试
print(’--------------------------------------------------------------------------------------------’)

#11.1.3 不能可通过的测试
print(’--------------------------------------------------------------------------------------------’)

#11.1.4 不能通过怎么办
print(’--------------------------------------------------------------------------------------------’)

#11.1.5 添加新的测试
print(’--------------------------------------------------------------------------------------------’)

#11.2测试类
print(’--------------------------------------------------------------------------------------------’)

#11.2.1 各种断言方法
print(’--------------------------------------------------------------------------------------------’)

#11.2.2 一个要测试的类
print(’--------------------------------------------------------------------------------------------’)

#11.2.3 测试类
print(’--------------------------------------------------------------------------------------------’)

#11.2.4 测试方法
print(’--------------------------------------------------------------------------------------------’)

#第十二章 武装飞船

#12.1规划项目
print(’--------------------------------------------------------------------------------------------’)

#12.2 安装Pygame
print(’--------------------------------------------------------------------------------------------’)

#12.2.1 使用pip安装python包
print(’--------------------------------------------------------------------------------------------’)
#1.在linux中检查pip

$pip --version

#2.在windows 检查pip
#3.安装pip
#4.在linux安装pip包

sudo python get—pip.py

#5.在windows安装pip
#python get-pip.py

#12.2.2 linux安装pagame
print(’--------------------------------------------------------------------------------------------’)

#12.2.3 osx安装pagame
print(’--------------------------------------------------------------------------------------------’)

#12.2.4 windows安装pagame
print(’--------------------------------------------------------------------------------------------’)

#12.3开始游戏项目
print(’--------------------------------------------------------------------------------------------’)

第二章 变量和简单的数据类型

#2.1输出数据hello world
print(’--------------------------------------------------------------------------------------------’)
print(‘hello world!!!’)

#2.2 变量
print(’--------------------------------------------------------------------------------------------’)
message=“这是**杰的第一个python”
print(message)

#2.2.1变量的命名和使用
#只能包含字母,数字,下划线 ,不能包含空格,不可用Python关键字函数命名,简短又具有描述性,慎用小写I和O

#2.3字符串
print(’--------------------------------------------------------------------------------------------’)

“wo shi zhao huajie”
‘wo shi zhao huajie’
print(“wo shi zhao huajie”)
print(‘wo shi zhao huajie’)

#2.3.1使用方法修改字符串的大小写
print(’--------------------------------------------------------------------------------------------’)
name=‘wo shi zhao huajie ,我是杰’
print(name.title()) #title()首字母变成大写
print(name.upper()) #upper()字母变成大写
name1='WO SHI ZHAO HUAJIE ,我是
杰-----------’
print(name1.lower()) #lower()字母变成小写
print(name1.title()) #title()首字母变成大写

#2.3.2 合并拼接
print(’--------------------------------------------------------------------------------------------’)
#python用+来拼接
frist_name=“zhao”
last_name=‘huajie’
full_name =frist_name+last_name
print(full_name)
print(“zhao”+‘huajie’.title())

#2.3.3使用制表符和换行符来添加空白
print(’--------------------------------------------------------------------------------------------’)
#空白泛指 非打印字符,如空格 ,制表符,换行符
#\t制表符 \n 换行符
print("\tzhao\thuajie\tzhao\tzhao\thuayi")
print("\nzhao\nhuajie\nzhao\nzhao\nhuayi")

#2.3.4删除空白
print(’--------------------------------------------------------------------------------------------’)
name="* 华杰 "
print(name.rstrip()) #rstrip()去掉末尾空白
name=" * 华杰 "
print(name.lstrip()) #lstrip()去掉首部空白

#2.3.5避免错误的语法
print(’--------------------------------------------------------------------------------------------’)
#message=‘zhao ’ huajie’
#print(message) #撇号应当位于双引号中间
message1=“zhao’huajie”
print(message1)

#2.4数字
print(’--------------------------------------------------------------------------------------------’)
#2.4.1整数 数字可以直接加减乘除,混合运算
2+3
3-2
4/2
2*3

#2.4.2浮点数 带小数的数 计算结果保存的小数不确定的
print(’--------------------------------------------------------------------------------------------’)
3.000+3
3.000-2
3.000/2
3.000*3

#2.4.3使用str()函数避免类型错误
print(’--------------------------------------------------------------------------------------------’)
age=23
#message="zhao huajie "+age+ ‘的年龄’
#print(message)
message="zhao huajie "+str(age)+ ‘的年龄’
print(message)

#2.5如何编写注释
#python中用‘#’来注释

#第三章 列表

#3.1列表是什么
print(’--------------------------------------------------------------------------------------------’)
#列表是由一组特定顺序的元素组成 ,用[]来表示
name=[‘zhao’,‘hua’,‘jie’]
print(name)
print([‘zhao’,‘hua’,‘jie’])

#3.1.1访问元素
print(’--------------------------------------------------------------------------------------------’)
name=[‘zhao’,‘hua’,‘jie’,“zheng”,“zai”,“zuo”,“shen”,“me”]
print(name[0]+name[1]+name[2]+name[3]+name[4])
print(name[0]+name[1]+name[2]+name[3]+name[4].title()+’============’)

#3.1.2 索引从0开始而不是1开始
print(’--------------------------------------------------------------------------------------------’)
name=[‘zhao’,‘hua’,‘jie’,“zheng”,“zai”,“zuo”,“shen”,“me”]
print(name[0]+name[1]+name[2]+name[-2]+name[-1].upper()+’-----------’) #python提供-1回到最后一个元素,-1…-n 指的是倒着取数

#3.2修改,删除,添加元素
print(’--------------------------------------------------------------------------------------------’)
message=[‘zhao’,‘huajie’,‘zuo’,‘shenme’,‘xianzai’]
print(message)
message[0]=‘ZHAO’ #修改元素
print(message)
message.append(‘zuoshmmene?’)#append()方法是把元素添加到列表的最后面,不影响其他的元素
print(message)
message.insert(0,‘WEIDADE’)#insert(0,‘WEIDADE’)方法是把元素插入到列表的指定的位置,其他元素后移
print(message)
message=[‘zhao’,‘huajie’,‘zuo’,‘shenme’,’=============’]
print(message)
del message[-1] #del语句是删除对应的数据
print(message)

print(’\t’)
print(’--------------------------------------------------------------------------------------------’)
flag=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag)
print(flag.pop())#po()方法是删除列表最后一个元素,并且让你继续获取到它。
print(flag)

flag1=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag1)
#print(flag1[0].pop()+‘pop删除指定的元素’)此语法错误
print(flag1.pop(-1)+’------pop删除指定列表的元素’)
print(flag1)

flag2=[‘摩托车’,‘小汽车’,‘客车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag2)
flag2.remove(‘小汽车’)#根据具体的列表的值删除
print(flag2)

flag2=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(flag2)
flag2.remove(‘小汽车’)#根据具体的列表的值删除,并且只删除第一个匹配到的值
print(flag2)
print(’--------------------------------------------------------------------------------------------’)

#3.3组织列表

#3.3.1使用方法sort()对列表永久排序
print(’--------------------------------------------------------------------------------------------’)

many=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(many)
many.sort()#按照字母的顺序进行永久的 正序 排序
print(str(many)+‘正序’)
many=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
many.sort(reverse=True)#按照字母的顺序进行永久的 相反 排序
print(str(many)+‘相反’)

#3.3.2使用方法sorted()对列表永临时的排序
print(’--------------------------------------------------------------------------------------------’)
many1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(many1)
sorted(many1)#按照字母的顺序进行永久的 临时正序 排序
print(str(sorted(many1))+‘临时正序’)
many1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
sorted(many1,reverse=True)#按照字母的顺序进行永久的 临时相反 排序
print(str(sorted(many1,reverse=True))+‘临时相反’)
print(many1)

#3.3.3倒着打印列表
print(’--------------------------------------------------------------------------------------------’)
manySo=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(manySo)
print(manySo.reverse())#reverse()倒着打印列表

#3.3.4确定列表的长度
print(’--------------------------------------------------------------------------------------------’)
manySo=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(manySo)
print(len(manySo))#len()确定列表的长度

#3.4使用列表避免索引错误
#索引从0开始

#第四章 操作列表

#4.1遍历整个列表
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls) #记得要空格print前面,否则报错。

#4.1.1深入研究循环
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls) #记得要空格print前面,否则报错。

#4.1.2循环中执行更多的操作
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。

#4.1.3循环结束执行一些操作
print(’--------------------------------------------------------------------------------------------’)
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。
print(“循环数据结束完了!!!!”) #这句话不在循环内部,print()前不需要加空格;;;;

#4.2避免缩进错误
print(’--------------------------------------------------------------------------------------------’)
#4.2.1忘记缩进

#4.2.2忘记缩进额外的代码行

#4.2.3没必要的缩进
ls=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
#print(ls)#没必要的缩进

#4.2.4循环后没必要的缩进
listFor=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listFor)
for ls in listFor: #for循环打印列表, ls为定义变量 ,listFor为列表名字,:为必须要
print(ls+‘他是数据的返回!!!!’) #记得要空格print前面,否则报错。
print(ls+‘他是数据的返回!!!!.\n’) #记得要空格print前面,否则报错。

print(“没必要的缩进!!!!”) #这句话不在循环内部,print()前不需要加空格;;;;缩进后会添加到上面的循环

#4.2.5遗漏了冒号

#4.3创建数字列表

#4.3.1使用range函数
print(’--------------------------------------------------------------------------------------------’)
for ls in range(1,10): #range(n,m)开始数字n,结束数字m ,包含n,不包含m
print(ls)

#4.3.2使用range函数创建数字列表
print(’--------------------------------------------------------------------------------------------’)
num=list(range(1,10))#list()函数将其转换为列表
print(num)

num=list(range(1,10,2))#list()函数将其转换为列表,range(n,m,o) o为步长
print(num)

#4.3.3对列表的数字进行简单的计算
print(’--------------------------------------------------------------------------------------------’)
numNm=list(range(1,15,3))
print(min(numNm))#最小值
print(max(numNm))#最大值
print(sum(numNm))#求和

#4.3.4列表解析
print(’--------------------------------------------------------------------------------------------’)
squares=[val**2 for val in range(1,10)] #获取数据的平方值得列表,注意这里没有冒号“:”
print(squares)
print([val for val in range(1,10)])
print(list(range(1,10))) #print(list(range(1,10)))和上面print([val for val in range(1,10)])结果一样

#4.4使用列表的一部分(切片)

#4.4.1切片
print(’--------------------------------------------------------------------------------------------’)
listM=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listM)
print(listM[0:3]) #获取列表前三个元素 列表名[n:m]n开始位置包含,m结束位置不包含
print(listM[:3]) #列表名[:m] 没有指定第一个则从头开始到m
print(listM[0:]) #列表名[n:]没有指定尾,则从n到最后
print(listM[-3:]) #列表名[n:]没有指定尾,则从n到最后,倒着数

#4.4.2遍历切片
print(’--------------------------------------------------------------------------------------------’)
listM1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(listM1)
for a in listM1[:3]:
print(a)

#4.4.2复制列表
print(’--------------------------------------------------------------------------------------------’)
listM1=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(str(listM1)+’---------listM1’)
listM2=listM1 #此种复制方法是列表名字改了,集体的列表物理地址没变化.
listM3=listM1[:] #此种切片复制列表的方法,是建立了新的列表,物理地址发生变化
listM1.append(‘王八初始化’)
listM2.append(‘王八羔子’)
listM3.append(‘王八犊子’)
print(str(listM1)+’---------listM11’)
print(str(listM2)+’---------listM2’)
print(str(listM3)+’---------listM3’)

#4.5元组

#4.5.1元组定义
print(’--------------------------------------------------------------------------------------------’)
#元组犹如列表,但是不是使用方括号,而是使用圆括号,定义元组可以用索引来访问,就像列表一样。
data=(100,100,200,4000) #定义元组
print(data[0]) #使用元组
print(data[1])
#data[1]=300 #报错,python不能给元组的元素赋值;

#4.5.2遍历元组的元素
print(’--------------------------------------------------------------------------------------------’)
data1=(100,100,200,4000) #定义元组
for data in data1: #遍历元组
print(data) #循环打印数据

#4.5.3修改元组变量
print(’--------------------------------------------------------------------------------------------’)
data2=(100,100,200,4000) #定义元组
for d in data2: #遍历元组
print(d) #循环打印数据
print(‘没赋值元组变量的值’)

data2=(1000,1000,2000,40000) #定义元组
for d in data2: #遍历元组
print(d) #循环打印数据
print(‘赋值元组变量后的值’)

#4.6设置代码格式
print(’--------------------------------------------------------------------------------------------’)
#4.6.1格式指南
print(’--------------------------------------------------------------------------------------------’)
#4.6.2缩进
print(’--------------------------------------------------------------------------------------------’)
#4.6.3行长
print(’--------------------------------------------------------------------------------------------’)
#在大多数的计算机中,终端的行长为79个字符,
#4.6.4空行
print(’--------------------------------------------------------------------------------------------’)
#程序不同部分用空行隔开,尽量用一个空行隔开,不然影响可读性。
#4.6.5其他格式设置指南
print(’--------------------------------------------------------------------------------------------’)
#pep8参考

#第五章 if语句

#5.1一个简单例子
print(’--------------------------------------------------------------------------------------------’)
listIf=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’,‘if’]
for lf in listIf:
if lf==‘if’: #这是if的判断的写法
print(‘这是第一个if’)
else:
print(lf+‘这是其他的元素!!!’)

#5.2条件测试
print(’--------------------------------------------------------------------------------------------’)
#5.2.1检查是否相等
car=‘123’
car==‘123’
print(car==‘123’)#检查俩个元素是否相等用‘’;
car
’345’
print(car==‘133’)

#5.2.2检查是否相等考虑大小写
print(’--------------------------------------------------------------------------------------------’)
car=‘qwe’
car==‘QWE’
print(car==‘QWE’)#检查俩个元素是否相等用‘’,且考虑大小写;
car
’345’
print(car==‘133’)
print(’----转换为小写----’)
print(car.lower()‘QWE’.lower())#方法lower(),upper()进行转换;
print(’----转换为大写----’)
print(car.upper()
‘QWE’.upper())

#5.2.3检查是否不相等
print(’--------------------------------------------------------------------------------------------’)
car=‘qwe’
car==‘QWE’
print(car==‘QWE’)#检查俩个元素是否不相等用‘!=’,且考虑大小写;
car!=‘345’
print(car!=‘133’)
print(’----转换为小写----’)
print(car.lower()!=‘QWE’.lower())#方法lower(),upper()进行转换;
print(’----转换为大写----’)
print(car.upper()!=‘QWE’.upper())

#5.2.4比较数字
print(’--------------------------------------------------------------------------------------------’)
print(’—比较数字----’)
age=18
age==18
print(age>12)
print(age>=12)
print(age<19)
print(age<=19)
print(age>19)
print(age>=19)

#5.2.5检查多个条件
print(’--------------------------------------------------------------------------------------------’)
print(’—检查多个条件----’)
age=18
sex=‘男’
print(age18 and sex’男’) #使用and检查多个条件
print(age18 or sex’女’) #使用or检查多个条件

#5.2.6/7检查特定值是否包含/不包含在列表中
print(’--------------------------------------------------------------------------------------------’)
print(’—检查特定值是否包含在列表中----’)
listIn=[‘摩托车’,‘小汽车’,‘客车’,‘小汽车’,‘人’,‘猪八戒’,‘妖怪’]
print(‘人’ in listIn) #检查特定值是否包含在列表中
print(‘人’ not in listIn) #检查特定值是否包含在列表中

#5.2.8布尔表达式
print(’--------------------------------------------------------------------------------------------’)
print(’—布尔表达式----’)
testTF=True #T大写
testTF=False #F大写

#5.3if语句

#5.3.1简单的if语句
print(’--------------------------------------------------------------------------------------------’)
print(’—简单的if语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)

#5.3.2if-else语句
print(’--------------------------------------------------------------------------------------------’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
else:
print(‘这是ok的else测试!!!’)

#5.3.2if-elif-else语句
print(’--------------------------------------------------------------------------------------------’)
print(’—if-elif-else语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
elif age==19:
print(‘这是ok的elif测试!!!’)
else:
print(‘这是ok的else测试!!!’)

#5.3.3/4/5使用elif语句
print(’--------------------------------------------------------------------------------------------’)
print(’—if-elif-else语句----’)
age=19
if age>=20:
print(‘这是ok的测试!!!’)
elif age19:
print(‘这是ok的elif测试!!!’)
elif age
20:
print(‘这是ok的elif2222测试!!!’)
else:#else可以省略不写
print(‘这是ok的else测试!!!’)

#5.4使用if语句处理列表
print(’--------------------------------------------------------------------------------------------’)
print(’—使用if语句处理列表----’)

#5.4.1检查特殊元素
print(’--------------------------------------------------------------------------------------------’)

#5.4.2确认列表是否为空
print(’--------------------------------------------------------------------------------------------’)
testNull=[]
if testNull:#判读列表是否为空
print(‘列表不是空的,可以执行下面的语句’)
else:
print(‘列表为空的,可以执行下面的语句’)

#5.4.3使用多个列表
print(’--------------------------------------------------------------------------------------------’)
testNum1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
testNum2=[5,6,7,8,9,10,11,12,13,14,15,20]
for Num2 in testNum2:
if Num2 in testNum1:
print(‘数字--------------’+str(Num2))
else:#注意缩进,否则报错IndentationError: unindent does not match any outer indentation level
print(‘数字不在--------------’+str(Num2))

#5.5设置if的格式
print(’--------------------------------------------------------------------------------------------’)

#第六章 字典

#6.1一个简单的例子
print(’--------------------------------------------------------------------------------------------’)
print(’-------------------字典---------------------------’)
#字典是由大括号括起来的键值对,
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example[‘本人’])
print(str(example[‘人口总数’])+‘人’)

#6.2使用字典
#6.2。1使用字典
print(’--------------------------------------------------------------------------------------------’)
print(example[‘laopo’])

#6.2.2添加键值对
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)

#6.2.3先创建一个新字典
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)

#6.2.3修改字典的值
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人’]=’*更新1’
example[‘小舅子’]=’***1’
print(example)

#6.2.5删除字典的键值对
print(’--------------------------------------------------------------------------------------------’)
example={}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人1’]=’*更新1’
example[‘小舅子1’]=’***1’
print(example)
del example[‘丈人1’]#删除字典的键值对,删除的键值对永远的消失了
print(example)

#6.2.6删除字典的键值对
print(’--------------------------------------------------------------------------------------------’)
example={
‘本人’:‘杰’,
‘laopo’:’
*’
}
print(example)
example[‘丈人’]=‘更新’
example[‘小舅子’]=’
**’
print(example)
example[‘丈人1’]=’*更新1’
example[‘小舅子1’]=’***1’
print(example)
del example[‘丈人1’]#删除字典的键值对,删除的键值对永远的消失了
print(example)

#6.3遍历字典

#6.3.1遍历所有的键值对
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key,value in example.items(): #使用items()方法
print(“key:”+str(key)+"---------value:"+str(value))

#6.3.2遍历所有的键
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key in example.keys():#使用keys()方法
print(“key:”+str(key)+"---------这是key.")

#6.3.3按顺序遍历所有的键
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
for key in sorted(example.keys()):#使用sorted()方法排序
print(“key:”+str(key)+"---------这是key.")

#6.3.4按顺序遍历所有的值
print(’--------------------------------------------------------------------------------------------’)
example={‘本人’:‘杰’,‘laopo’:'’,‘兄长’:'毅’,‘嫂子’:‘青’,'父’:'’,'母’:’***’,‘长侄’:’***瑞’,‘侄女’:’***嘉’,‘大犬’:’***琳’,‘人口总数’:9}
print(example)
del example[‘人口总数’]#类型不一样无法遍历,报错
example[‘本人1’]=’**杰’#添加键不同,但值相同的元素
example[‘本人’]=’**杰’#修改键的值
print(example)
for value in set(sorted(example.values())): #set()方法是 去掉键值对的值相同的重复数据
print(“value:”+str(value)+"---------value.")

#6.4嵌套
#6.4.1字典列表
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:'’}
example1={‘兄长’:'毅’,‘嫂子’:‘青’}
example2={'父
’:'
’,'母
’:’***’}
example3={‘长侄’:’***瑞’,‘侄女’:’***嘉’}
example4={‘大犬’:’***琳’,‘人口总数’:9}
print(example0,example1,example2,example3,example4)
#列表
exampleSome=[example0,example1,example2,example3,example4]
print(exampleSome)

#6.4.2 字典存储列表
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:[’*’,’***1’,’***2’]}
print(example0)
#列表
listName0=[‘1’,‘2’,‘3’]
listName1=[‘1’,‘2’,‘3’]
listName2=[‘1’,‘2’,‘3’]
#字典存储列表
exampleName={1:listName0,2:listName1,3:listName2}
print(exampleName)

#6.4.2 字典存字典
print(’--------------------------------------------------------------------------------------------’)
#字典
example0={‘本人’:‘杰’,‘laopo’:[’*’,’***1’,’***2’]}
print(example0)
#字典
listName0={1:‘1’,2:‘2’,3:‘3’}
listName1={1:‘1’,2:‘2’,3:‘3’}
listName2={1:‘1’,2:‘2’,3:‘3’}
#字典存储字典
exampleName={1:listName0,2:listName1,3:listName2}
print(exampleName)

#第七章 用户输入和Wile循环

#7.1函数input的工作原理
print(’--------------------------------------------------------------------------------------------’)
#message=[input(“请输入一个值:::::”)]#没办法在这里执行,使用input()则可以从键盘获取值。
#print(message)

#7.1.1/2函数int()来获取数值输入
print(’--------------------------------------------------------------------------------------------’)
#age=[input(“请输入一个值:::::”)]#没办法在这里执行,使用input()则可以从键盘获取值。
age=int(age)#用int()函数来转换为int类型
#print(age)

#7.1.3求模运算
print(’--------------------------------------------------------------------------------------------’)
4%3
print(4%3)

#7.2while循环

#7.2.1使用while循环
print(’--------------------------------------------------------------------------------------------’)
num=1
while num<=5:#while循环 冒号注意
print(num)#缩进注意
num+=1 #循环累加

#7.2.2让用户选择何时退出
print(’--------------------------------------------------------------------------------------------’)
ptmopt="\n 输入数字"
ptmopt+="\t 否则’quit’ 程序!!!"
message=""
#while message!=‘quit’: #while循环 冒号注意
#message=input(ptmopt) #输入信息
#print(message)#缩进注意

#7.2.3使用标志
print(’--------------------------------------------------------------------------------------------’)
#message=True
#while message: #while循环 冒号注意
#message=input(ptmopt) #输入信息
#print(message)#缩进注意

#7.2.4使用break退出循环
print(’--------------------------------------------------------------------------------------------’)
num=1
while num<=5:#while循环 冒号注意
print(num)#缩进注意
if num==2:
break #根据条件中断循环
num+=1 #循环累加

#7.2.5使用continue退出循环
print(’--------------------------------------------------------------------------------------------’)
#num=1
#while num<=5:#while循环 冒号注意
#print(num)#缩进注意
#num+=1 #循环累加
#if num==2:
#ontinue #根据条件加速循环

#7.2.6避免死循环
print(’--------------------------------------------------------------------------------------------’)

#7.3使用while循环处理列表和字典

#7.3.1 在列表中移动元素
print(’--------------------------------------------------------------------------------------------’)

#创建用户列表
#创建存储列表
oldlist = [‘1’,‘2’,‘3’,‘4’]
newlist = []

while oldlist: #while循环遍历列表的时候自动判断遍历结束,语法为此。
oldlistone=oldlist.pop() #
print(‘这是取出的元素-----’+oldlistone)
newlist.append(oldlistone)
print(‘验证循环结束!!!’)

#显示已验证的用户列表
for newls in newlist:
print(‘已经验证的用户信息----’+newls.title())
print(‘查询已经验证循环结束!!!’)

#查看被遍历的用户列表
for oldls in oldlist:#列表为空
print(‘被验证的用户信息----’+oldls.title())
print(‘查询被验证循环结束!!!’)

#7.3.2 删除包含特定值得所有列表元素
print(’--------------------------------------------------------------------------------------------’)

pets = [‘cat’,‘cat’,‘dog’,‘fish’,‘rabbit’,‘snake’]
print(pets)
while ‘cat’ in pets: #判断此值是否在列表中
pets.remove(‘cat’) #删除列表特定的值;
print(pets)

#7.3.3 使用用户输入填充字典
print(’--------------------------------------------------------------------------------------------’)

python的多行注释用“’’’”

‘’’
#创建字典
petsnew = {}
print(petsnew)
#循环条件表示
flag=True
#给字典加入元素
while flag: #让循环执行
name=input(’\n输入一个名字’)
respones=input(’\n回答的问题’)
petsnew[name]=respones
repeat=input(’\n是否有人需要调查??????’)
if repeat==‘no’:
flag=False
print(’\n----------调查结束------’)

#显示调查的结果
for key,value in petsnew.items():
print(‘这是调查的结果元素-------key:’+key+’-------values:’+value)
print(‘程序结束!!!’)

print(petsnew)
‘’’

#第八章 函数

#8.1定义函数
print(’--------------------------------------------------------------------------------------------’)
def greet_people():#创建函数 def 函数名字():
“”“显示简单的问候语!!!!!!!”""
print(‘hello world!!!’)

greet_people()#调用函数

#8.1.1向函数传递信息
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username):#创建函数 def 函数名字(参数):
“”“显示简单的问候语!!!!!!!”""
print(username+’---------------------hello world!!!’)

greet_people(‘zhaohuajie’)#调用函数

#8.1.2实参和形参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username):#创建函数 def 函数名字(参数):username为形参
“”“显示简单的问候语!!!!!!!”""
print(username+’---------------------hello world!!!’)

greet_people(‘zhaohuajie’)#调用函数, 'zhaohuajie’为实参

#8.2传递实参

#8.2.1位置实参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets):#创建函数 def 函数名字(参数): , username,pets为位置形参
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

greet_people(‘zhaohuajie’,‘cat’)#调用函数, ‘zhaohuajie’,'cat’为实参
greet_people(‘zhaohuajie1’,‘cat1’)#调用函数, ‘zhaohuajie’,'cat’为实参

#8.2.2关键字实参
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets):#创建函数 def 函数名字(参数): , username,pets为位置形参
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样;
greet_people(username=‘zhaohuajie’,pets=‘cat’)#调用函数,
greet_people(pets=‘cat’,username=‘zhaohuajie’)#调用函数,

#8.2.3 默认值
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets=‘cat1’):#创建函数 def 函数名字(参数): , username,pets为位置形参,pets=‘cat1’给参数默认值
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样;
greet_people(username=‘zhaohuajie’)#调用函数, pets默认值为cat1
greet_people(pets=‘cat’,username=‘zhaohuajie’)#调用函数, 给pets重新赋值

#8.2.4 等效的函数调用
print(’--------------------------------------------------------------------------------------------’)
def greet_people(username,pets=‘cat1’):#创建函数 def 函数名字(参数): , username,pets为位置形参,pets=‘cat1’给参数默认值
“”“问候pets!!!!!!!”""
print(username+’---------------------hello world!!!’+pets.title())

#关键字实参下面的俩种顺序结果一样,为等效的函数调用
greet_people(username=‘zhaohuajie111111’)#调用函数, pets默认值为cat1
greet_people(pets=‘cat2222222’,username=‘zhaohuajie11111’)#调用函数, 给pets重新赋值

#8.2.5 避免错误
print(’--------------------------------------------------------------------------------------------’)

#8.3 返回值

#8.3.1 返回简单值
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last):
full_name=first+" "+last
return full_name.title() #设定返回值

full_name=fomatted_name(‘zhao’,‘huajie’)
print(full_name)

#8.3.2 让实参变成可选的
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last,middle=’’):#注意默认的字符串middle必须在末尾
if middle:#判断是否为空
full_name=first+middle+last
else:#else后的冒号不可少
full_name=first+’’+last
return full_name.title() #设定返回值

full_name=fomatted_name(‘zhao’,’—’,‘huajie’)
full_name1=fomatted_name(‘zhao’,‘huajie’)
print(full_name1)
print(full_name1)

##8.3.3 返回字典
print(’--------------------------------------------------------------------------------------------’)
def fomatted_name(first,last,middle=’’):#注意默认的字符串middle必须在末尾
full_name={‘first’:first,‘last’:last} #设置返回值为字典
return full_name #设定返回值
full_name=fomatted_name(‘zhao’,’—’,‘huajie’)
full_name1=fomatted_name(‘zhao’,‘huajie’)
print(full_name)
print(full_name1)

#8.3.3 结合函数和while循环
print(’--------------------------------------------------------------------------------------------’)

#8.4 传递列表
print(’--------------------------------------------------------------------------------------------’)

def fomatted_name(first,last,listone):#传递列表
full_name=first+" "+last+’********’+listone
return full_name.title() #设定返回值

listone=[‘1’,‘2’,‘3’,‘4’] #列表
full_name=fomatted_name(‘zhao’,‘huajie’,str(listone))
print(full_name)

#8.4.1 在函数中修改列表
print(’--------------------------------------------------------------------------------------------’)

#8.4.2 禁止在函数中修改列表
print(’--------------------------------------------------------------------------------------------’)
full_name=fomatted_name(‘zhao’,‘huajie’,str(listone[:]))#切片方法是将列表的副本传递给函数,对原来函数无影响
print(full_name)

#8.5 传递任意数量的实参
print(’--------------------------------------------------------------------------------------------’)
def pizza(top): #传递任意数量的实参用 ‘’ 号表示,*只能在top前面
print(top)

pizza(‘1’)
pizza(‘1’,‘2’,‘3’,‘4’,‘5’)

#8.5.1 位置实参和任意数量的实参
print(’--------------------------------------------------------------------------------------------’)
def pizza2(name,top): #传递任意数量的实参用 ‘’ 号表示,*只能在top前面,name为位置实参
print(name,top)
pizza2(‘zhaohuajie’,‘1’)
pizza2(‘zhaohuajie’,‘1’,‘2’,‘3’,‘4’,‘5’)

#8.5.2 位置实参和任意数量的关键字实参
print(’--------------------------------------------------------------------------------------------’)
def pizza3(name,user_info): #传递任意数量的实参用 ‘’ 号表示,只能在top前面,name为位置实参,为俩个
print(name,user_info)
pizza3(‘zhaohuajie’,user_info1=‘这是一’,user_info2=‘这是二’)

#下面代码相当于上面的代码
def pizza3(name,user_info): #传递任意数量的实参用 ‘**’ 号表示,只能在top前面,name为位置实参,为俩个
print(name,user_info)
user_info={‘user_info1’:‘这是一121’,‘user_info2’:‘这是二1323’}
pizza3(‘zhaohuajie’,user_info)

#8.6将函数存储在模块中

#8.6.1 导入整个模块
print(’------------------------------------------------------------------------------------------------’)
import testFuncthion #导入别的文件
print(’--------------------调用函数pizza22-------------------------------------------------------------’)
testFuncthion.pizza22(‘zhaohuajie’)#调用别的文件的函数

print(‘测试调用函数从别的文件里::::’,str(testFuncthion.pizza22(‘zhaohuajie’)))

#8.6.2 导入特定的函数
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import pizza22, pizza22 #导入别的文件里的特定的几个函数
print(’--------------------导入别的文件里的特定的函数--------------------------------------------------’)
pizza22(‘zhaohuajie’)#调用别的文件的函数,不需要在写文件名字了

print(‘测试调用函数从别的文件里,不需要再写文件名字::::’,str(pizza22(‘zhaohuajie’)))

#8.6.3 使用as给函数指定别名
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import pizza22 as mp #导入别的文件里的特定的函数,给函数取别名
print(‘测试调用函数mp从别的文件里,不需要再写文件名字::::’,str(mp(‘zhaohuajie’))) #调用别名函数

#8.6.4 使用as给模块定别名
print(’------------------------------------------------------------------------------------------------’)
import testFuncthion as tf #导入模块并使用别名
print(‘测试用模块别名获取函数::::’,str(tf.pizza22(‘zhaohuajie’))) #使用别名获取函数

#8.6.5 导入模块中的所有函数
print(’------------------------------------------------------------------------------------------------’)
from testFuncthion import * #导入模块中的所有函数
print(‘测试用*代替函数::::’,str(pizza22(‘zhaohuajie’))) #调用其中一个函数

#第九章 类

#9.1 创建和使用类
print(’----------------------------------------类--------------------------------------------------’)
#面向对象编程是最有效的编程之一

#9.1.1 创建dog类
print(’--------------------------------------------------------------------------------------------’)
class Dog():
#模拟小狗的类
def init(self,name,age): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.age=age

 def sit(self): #类中的方法
 		#模拟小狗下蹲
 		print(self.name.title()+'is no down!!!')
 	
 def roll_over(self):
 		#模拟小狗被命令时打滚
 		print(self.name.title()+'is no roll_over!!!')

#9.1.2 根据类创建实例
print(’--------------------------------------------------------------------------------------------’)
print(’----------根据类创建实例-----------’)
my_dog=Dog(‘huahua’,90)#创建类的实例
print(‘我的小狗—’+my_dog.name.title()+‘跑的快!!!它’+str(my_dog.age)+‘岁。’) #调用类里的属性
my_dog.sit() #调用类的方法
my_dog.roll_over() #调用类的方法

my_dog1=Dog(‘huahua’,90)#创建类的多个实例
print(‘我的小狗1—’+my_dog1.name.title()+‘跑的快!!!它’+str(my_dog1.age)+‘岁。’) #调用类里的属性
my_dog1.sit() #调用类的方法

my_dog1.roll_over() #调用类的方法

#9.2使用类和实例

#9.2.1 Car类
print(’--------------------------------------------------------------------------------------------’)
class Car():
#模拟车的类
def init(self,name,make,model,year): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=year

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()

my_car=Car(‘宝骏’,‘MA’,‘A4’,1989)
print(‘在调用自定义方法’)
print(’**杰自定义的函数返回值:::::’,my_car.get_des_name())

#9.2.2 给属性指定符默认值
print(’--------------------------------------------------------------------------------------------’)
class Car1():
#模拟车的类
def init(self,name,make,model): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=2000 #给属性指定赋默认值————————————————————————————————————————————————————————————

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()

my_car1=Car1(‘宝骏’,‘MA’,‘A4’)
print(‘在调用自定义方法’)
print(‘给属性指定赋默认值,自定义的函数返回值:::::’,my_car1.get_des_name())

#9.2.2 修改属性的值
print(’--------------------------------------------------------------------------------------------’)

#直接修改属性值
my_car2=Car1(‘宝骏’,‘MA’,‘A4’)
print(‘未修改属性值前-------’,my_car2.get_des_name())
my_car2.name=‘宝马’ #修改其中的某个属性;*****************************************
print(‘修改属性值后---------’,my_car2.get_des_name())

#提过方法修改属性值
class Car3():
#模拟车的类
def init(self,name,make,model): #init()方法,类中的函数称为方法,self方法参数不可少
self.name=name #把name值给self的name
self.make=make
self.model=model #把name值给self的name
self.year=2000 #给属性指定赋默认值————————————————————————————————————————————————————————————

 def get_des_name(self): #类中的方法
 		#模拟描述返回信息
 		long_name=self.name+'---'+self.make+'---'+self.model+'---'+str(self.year)
 		return long_name.title()
 def edit(self,name,make,model): #类中的方法
 		#方法修改属性的值
 		self.name=name #把name值给self的name
 		self.make=make
 		self.model=model #把name值给self的name
 		self.year=2000  #给属性指定赋默认值————————————————————————————————————————————————————————————

my_car3=Car3(‘宝骏’,‘MA’,‘A4’)
print(‘方法未修改属性值前-------’,my_car3.get_des_name())
my_car3.edit(‘宝驴’,‘MAa’,‘A5’) #调用方法修改其中的某个属性;*****************************************
print(‘方法修改属性值后测试---------’,my_car3.get_des_name())

#通过方法对属性的值进行递增
“”" #段落注释前面不可以有空格
my_car4=Car3(‘宝骏’,‘MA’,‘A4’)
print(‘方法未修改属性值前-------’,my_car4.get_des_name1())
my_car4.newNAME=1000
print(‘测试循环增加------------’)
my_car4.edit_Updata(3000) #调用方法修改其中的某个属性;*****************************************
print(‘方法修改属性值后111111---------’,my_car4.get_des_name1())

“”"

#9.3 继承
#9.3.1/2/3/4 继承
print(’--------------------------------------------------------------------------------------------’)
class newCar(Car):
#电动汽车的独特处
def init(self,name,make,model,year): #init()方法,类中的函数称为方法,self方法参数不可少
super().init(name,make,model,year) #继承父类的属性和方法
self.flag=0 #给子类定义属性
def allP(self):#给子类定义方法
return self.flag
def get_des_name(self): #类中的方法
#模拟描述返回信息
long_name=self.name+’—’+self.make+’—’+self.model+’—’+str(self.year)+’—’+str(self.flag)
return long_name.title()
my_NewCar=newCar(‘皮皮虾’,‘吃的’,‘好玩’,1990)
print(‘调用子类的方法-------’,my_NewCar.allP())
print(‘调用子类重写父类的方法-------’,my_NewCar.get_des_name())

#9.3.5 将实例用作属性
#self.name=Car() 属性值为Car类

#9.4导入类
#9.4.1导入单个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car #把python文件的Car类导入到此文件夹

#9.4.2 在一个模块中存储多个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car #把python文件的Car类导入到此文件夹

#9.4.3 C从一个模块中导入多个类
print(’--------------------------------------------------------------------------------------------’)
#from python import Car,Dog #把python文件的Car,Dog类导入到此文件夹

#9.4.4 导入整个模块
print(’--------------------------------------------------------------------------------------------’)
#import python #把python文件导入

#9.4.5 导入整个模块
print(’--------------------------------------------------------------------------------------------’)
#from python import * #导入python模块中的所有类

#9.4.6 在一个模块导入另外一个模块
print(’--------------------------------------------------------------------------------------------’)

#9.4.7 自定义工作流
print(’--------------------------------------------------------------------------------------------’)

#9.5 Python标准库
print(’--------------------------------------------------------------------------------------------’)

#9.6 类编码风格
print(’--------------------------------------------------------------------------------------------’)
#驼峰命名法,首字母大写,而不是用下划线
#实例名或者模块名采用小写,单词之间用下划线分割

第十章 文件和议程

#10.1 从文件读取数据
print(’--------------------------------------------------------------------------------------------’)
#从文件读取数据,首先把文件读取到内存中。

#10.1.1/2 读取整个文件/文件路径
print(’--------------------------------------------------------------------------------------------’)
print(’------读取整个文件-------’)
url1=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
url2=‘E:\gongzuo_hc\UE_bak_file\read_test.txt’
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.1.3 逐行读取
print(’--------------------------------------------------------------------------------------------’)
print(’------逐行读取-------’)
url1=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
url2=‘E:\gongzuo_hc\UE_bak_file\read_test.txt’
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
for line in first_object:#for循环逐行读取
print(line.rstrip(),‘测试是否空行—’) #删除尾部空行
print(line,‘测试是否空行—’) #删除尾部空行

#10.1.4 创建包含文件各行的列表
print(’--------------------------------------------------------------------------------------------’)
print(’------创建包含文件各行的列表-------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()
listnew=[] #定义列表
for line in lines:#for循环逐行读取
print(line.rstrip()) #rstrip()删除尾部空行
listnew.append(line.rstrip())#添加元素到列表
print(listnew)#打印列表

#10.1.5 使用文件内容
print(’--------------------------------------------------------------------------------------------’)
#拼接,存入列表 等

#10.1.4 包含一百万位的大文件
print(’--------------------------------------------------------------------------------------------’)
print(’------创建包含文件各行的列表-------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test_million.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()#读取一行数据
pi_string=’’ #定义字符串
for line in lines:#for循环逐行读取
pi_string+=line.rstrip()#读取每行拼接在一起
print(pi_string[:52]+’…’)#截取字符串的前52位 pi_string[:52]

#10.1.5圆周率包含你的生日吗?
print(’--------------------------------------------------------------------------------------------’)
with open(‘E:/gongzuo_hc/UE_bak_file/read_test_million.txt’) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
lines=first_object.readlines()#读取一行数据
pi_string=’’ #定义字符串
for line in lines:#for循环逐行读取
pi_string+=line.strip()#读取每行拼接在一起
#print(pi_string)
if ‘19890704’ in pi_string:
print(‘19890704…’)#文件是否包含某个字符串
else:
print(‘不存在…’)

#10.2 写入文件

#10.2.1写入空文件
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘w’) as write_file: #注意写入的时候传参 w 需要加单引号。
write_file.write(“我爱***************!!!!!!!!!!!!!!!”)
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.2.2写入多行
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘w’) as write_file: #注意写入的时候传参 w 需要加单引号。
write_file.write(“我爱***************!!!!!!!!!!!!!!!”)
write_file.write(“可爱的小孩子!!!!!!!!!!!!!!!\n")#上面俩句为追加
write_file.write("我爱
*************!!!!!!!!!!!!!!!\n”)
write_file.write("**可爱的小孩子!!!!!!!!!!!!!!!\n")#上面俩句为换行追加
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.2.2附加到文件
print(’--------------------------------------------------------------------------------------------’)
filename=‘E:/gongzuo_hc/UE_bak_file/zhaohuajie_read.txt’
with open(filename,‘a’) as write_file: #注意附加到文件传参 a 需要加单引号。
write_file.write(“附加我爱!!!!!!!!!!!!!!!")
write_file.write(“附加*******************可爱的小孩子!!!!!!!!!!!!!!!\n”)#上面俩句为追加
write_file.write("附加
我爱!!!!!!!!!!!!!!!\n”)
write_file.write(“附加*******************可爱的小孩子!!!!!!!!!!!!!!!\n”)#上面俩句为换行追加
print(‘写入结束!!!!!!!!!!!!!!!!!!!’)
with open(filename,‘r’) as read_file: #注意写入的时候传参 r 需要加单引号,可省略。
contents=read_file.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行

#10.3 异常
#10.3.1处理ZeroDivisionError异常
print(’--------------------------------------------------------------------------------------------’)
#print(9/0)

#10.3.2使用try-except代码块
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/0)
except:
print(‘0 不可以做除数-----------’)

#10.3.3使用异常避免奔溃
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/0)
except:
print(‘0 不可以做除数-----------’)

#10.3.4 else代码块
print(’--------------------------------------------------------------------------------------------’)
try: #使用try-except代码块,注意后面的冒号
print(8/1)
except:
print(‘0 不可以做除数-----------’)
else:
print(‘输出数据------------------’)

#10.3.5/6 处理FileNotFoundError异常/分析文本
print(’--------------------------------------------------------------------------------------------’)
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘文件不可以找到*FileNotFoundError异常’)
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)

#10.3.7 使用多个文本
print(’--------------------------------------------------------------------------------------------’)
‘’’
def ff():
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘文件不可以找到*FileNotFoundError异常’)
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)
‘’’
#10.3.7 失败时一声不吭
print(’--------------------------------------------------------------------------------------------’)
‘’’
def ff():
url2=‘E:/gongzuo_hc/UE_bak_file/read_test.txt’
try: #使用try-except代码块,注意后面的冒号
with open(url) as first_object: #打开指定的文件,用单引号引起来,注意被读文件的编码格式。
contents=first_object.read()#读取打开的文件,read()读取整个文件
print(contents.rstrip()) #删除尾部空行
except:
print(‘pass’) #在此处失败时一声不吭
else:
print(‘程序结束!---------注意缩进很麻烦的哦…’)
‘’’

#10.3.8 决定报告哪些错误
print(’--------------------------------------------------------------------------------------------’)

#10.4存储数据
#10.4.1/2使用json.dump()和json.load()/保存用户读写的数据
print(’--------------------------------------------------------------------------------------------’)
#json.dump()存储数据,包含俩个参数,存储的数据,存储到的文件。
import json as js
numbers=[1,2,3,4,5,6]
filename1=‘E:/gongzuo_hc/UE_bak_file/jsonfile.json’
with open(filename1,‘w’) as j_f:
#dump(numbers,j_f)第一个参数为写入内容,第二个要写的文件
js.dump(numbers,j_f)#这个地方写反了,找了一个小时,真的很草他妈,吃屎的。。。。
print(‘写入结束!!!!!!!!!!dump(numbers,j_f)第一个参数为写入内容,第二个要写的文件!!!!!!!!!’)
with open(filename1,‘r’) as j_f_r:
numbers=js.load(j_f_r) #读取json文件的信息;
print(numbers)

#10.4.3 重构
print(’--------------------------------------------------------------------------------------------’)
#就是使用函数包装代码

#第十一章 测试代码
#11.1 测试函数
print(’--------------------------------------------------------------------------------------------’)

#11.1.1 单元测试和测试用例
print(’--------------------------------------------------------------------------------------------’)

#11.1.2 可通过的测试
print(’--------------------------------------------------------------------------------------------’)

#11.1.3 不能可通过的测试
print(’--------------------------------------------------------------------------------------------’)

#11.1.4 不能通过怎么办
print(’--------------------------------------------------------------------------------------------’)

#11.1.5 添加新的测试
print(’--------------------------------------------------------------------------------------------’)

#11.2测试类
print(’--------------------------------------------------------------------------------------------’)

#11.2.1 各种断言方法
print(’--------------------------------------------------------------------------------------------’)

#11.2.2 一个要测试的类
print(’--------------------------------------------------------------------------------------------’)

#11.2.3 测试类
print(’--------------------------------------------------------------------------------------------’)

#11.2.4 测试方法
print(’--------------------------------------------------------------------------------------------’)

#第十二章 武装飞船

#12.1规划项目
print(’--------------------------------------------------------------------------------------------’)

#12.2 安装Pygame
print(’--------------------------------------------------------------------------------------------’)

#12.2.1 使用pip安装python包
print(’--------------------------------------------------------------------------------------------’)
#1.在linux中检查pip

$pip --version

#2.在windows 检查pip
#3.安装pip
#https://bootstrap.pypa.io/get-pip.py 然后运行get-pip.py 文件
#https://pip.pypa.io/ 单击INSTALLATION 在单击get-pip.py链接。
#4.在linux安装pip包

sudo python get—pip.py

#5.在windows安装pip
#python get-pip.py

#12.2.2 linux安装pagame
print(’--------------------------------------------------------------------------------------------’)

#12.2.3 osx安装pagame
print(’--------------------------------------------------------------------------------------------’)

#12.2.4 windows安装pagame
print(’--------------------------------------------------------------------------------------------’)
#https://bitbucket.org/pygame/downloads/
#或者 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
#python -m pip install --user pygame-1.9.6-cp37-cp37m-win32.whl --执行在本机

#12.3开始游戏项目
print(’--------------------------------------------------------------------------------------------’)

‘’’

游戏项目

print(’-----------------------------------------------游戏项目---------------------------------------------’)
import sys
import pygame
from settings import Settings
from ship import Ship

def run_game():
pygame.init()
#设置参数
#ai_settings=Settings()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption(“Alien Invasion”)
#创建一艘飞船
ship=Ship(screen)

#开始游戏主循环
while True:
	
	#监视键盘和鼠标
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			sys.exit()
		elif event.type==pygame.KEYDOWN:
			if event.key==pygame.K_RIGHT:
				ship.rect.centerx+=20
			if event.key==pygame.K_LEFT:
				ship.rect.centerx-=20
	#每次循环时候都重绘屏幕
	screen.fill((230,230,230))  #让颜色填充屏幕
	#调用绘制飞船
	ship.blitme()
	#让最近绘制的屏幕可见
	pygame.display.flip()

run_game()

class Settings():
def init(self):
self.screen.width=1200
self.screen.height=800
self.screen.bg_color=(230,230,230)
#设置子弹
self.bullet_speed_factor=1
self.bullet_width=3
self.bullet_height=15
self.bullet_colorr=60,60,60

import pygame
class Ship():
def init(self,screen):
“”“设置具体参数”""
self.screen=screen

	#读取地址信息
	self.image=pygame.image.load('E:/gongzuo_hc/UE_bak_file/image/alien.bmp')
	self.rect=self.image.get_rect()
	self.screen_rect=screen.get_rect()
	#\u5C06\u6BCF\u8258\u98DE\u8239\u653E\u5230\u5C4F\u5E55\u5E95\u4E2D\u592E\u90E8
	self.rect.centerx=self.screen_rect.centerx
	self.rect.bottom=self.screen_rect.bottom

def blitme(self):
	#调用参数
	self.screen.blit(self.image,self.rect)

‘’’

#第十五章 生成数据
#15.1安装matplotlib

#15.1.1Linux系统中安装matplotlib
print(’--------------------------------------------------------------------------------------------’)

$ sudo apt-get install python3-matplotlib

#15.1.2 os x系统中安装matplotlib
print(’--------------------------------------------------------------------------------------------’)

$ pip install --user matplotlib

#15.1.3 windows系统中安装matplotlib
print(’--------------------------------------------------------------------------------------------’)

$ pip install --user matplotlib

#先下载 Visual Studio 下载地址:https://dev.windows.com/ 单击downloads 再 Visual Studio Community

https://pypi.python.org/pypi/matplotlib/ 下载对应的matplotlib文件

#安装 python -m pip install --user matplotlib-xxx

#第十六章 下载数据
#16.1 CSV文件格式
print(’---------------------------------------csv---------------------------------------------------’)
import csv
filename=‘E:/gongzuo_hc/UE_bak_file/csv.csv’
with open(filename ,‘r’,encoding=‘gbk’) as f:
reader=csv.reader(f)
head_row=next(reader)
print(’*************************’,head_row)

发布了4 篇原创文章 · 获赞 7 · 访问量 1255

猜你喜欢

转载自blog.csdn.net/zhaohj123456/article/details/101013478