案例A
效果如图
源码如下:
b = []
if not b:
print("空的列表是false")
c = "False" #非空字符串也是:True
if c:
print("c")
d = 10
if d:
print("d")
if 3<d<100:
print("3<d<100")
# if 3<c and (c=20): 条件表达式不能使用“赋值操作符”
案例B
源码如下:
s = input("请输入一个数字:")
if int(s)<10:
print("s是小于10的数字")
else:
print("s是大于等于10的数字")
#测试三元条件运算符
print( "s是小于10的数字" if int(s)<10 else "s是大于等于10的数字" )
案例C
测试成绩判定大法
源码如下:
#测试多分支选择结构
score = int(input("请输入分数:"))
grade = ""
if score<60:
grade = "不及格"
elif score<80: #60-80之间
grade = "及格"
elif score<90:
grade = "良好"
else:
grade = "优秀"
print("分数是{0},等级是{1}".format(score,grade))
print("**************************************************")
if(60<=score<80):
grade = "及格"
if(score<60):
grade = "不及格"
if(80<=score<90):
grade = "良好"
if(90<=score<=100):
grade = "优秀"
print("分数是{0},等级是{1}".format(score,grade))
案例D
#测试选择结构的嵌套–成绩复盘大法
源码如下:
#输入一个分数。分数在0-100之间。90以上是A,80以上是B,70以上是C,60以上是D。60以下是E。
score = int(input("请输入一个在0-100之间的数字:"))
grade = ""
if score>100 or score<0:
score = int(input("输入错误!请重新输入一个在0-100之间的数字:"))
else:
if score>=90:
grade = "A"
elif score>=80:
grade = 'B'
elif score>=70:
grade = 'C'
elif score>=60:
grade = 'D'
else:
grade = 'E'
print("分数为{0},等级为{1}".format(score,grade))
print("*******************************************************")
score = int(input("请输入一个在0-100之间的数字:"))
degree = "ABCDE"
num = 0
if score>100 or score<0:
score = int(input("输入错误!请重新输入一个在0-100之间的数字:"))
else:
num = score//10
if num<6:num=5
print("分数是{0},等级是{1}".format(score,degree[9-num]))
案例E
测试while循环
源码如下:
#测试while循环
num = 0
while num<=10:
print(num,end="\t")
num += 1
print("**********************************")
#计算1-100之间数字的累加和
num2 = 0
sum_all = 0
while num2<=100:
sum_all = sum_all + num2
num2 +=1
print("1-100所有书的累加和:",sum_all)
案例F
测试for循环
源码如下:
#测试for循环
for x in (10,20,30):
print(x*30)
for y in "abcdef":
print(y)
d = {"name":"高淇","age":18,"job":"程序员"}
for x in d:
print(x)
for x in d.keys():
print(x)
for x in d.values():
print(x)
for x in d.items():
print(x)
for x in range(5):
print(x)
print("########################")
sum_all = 0
sum_odd = 0 #100以内的奇数和
sum_even = 0 #100以内的偶数和
for x in range(101):
sum_all += x
if x%2==1:
sum_odd += x
else:
sum_even += x
print("1-100累加总和{0},奇数和{1},偶数和{2}".format(sum_all,sum_odd,sum_even))
案例G
测试嵌套循环
源码如下:
#测试嵌套循环
for x in range(5):
for y in range(5):
print(x,end="\t")
print() #起到换行的作用
#打印九九乘法表
for m in range(1,10):
for n in range(1,m+1):
print("{0}*{1}={2}".format(m,n,(m*n)),end="\t")
print() #换行
#使用列表和字典存储表格的数据
r1= dict(name="高小一",age=18,salary=30000,city="北京")
r2= dict(name="高小二",age=19,salary=20000,city="上海")
r3= dict(name="高小三",age=20,salary=10000,city="深圳")
tb = [r1,r2,r3]
for x in tb:
if x.get("salary")>15000:
print(x)
案例H
测试break语句
源码如下:
#测试break
while True:
a = input("请输入一个字符(输入Q或q时退出):")
if a=="q" or a=="Q":
print("循环结束,退出")
break
else:
print(a)
案例I
测试参数的类型:位置参数、默认值参数、命名参数
源码如下:
#循环代码优化测试
import time
start = time.time()
for i in range(1000):
result = []
for m in range(10000):
result.append(i*1000+m*100)
end = time.time()
print("耗时:{0}".format((end-start)))
start2 = time.time()
for i in range(1000):
result = []
c = i*1000
for m in range(10000):
result.append(c+m*100)
end2 = time.time()
print("耗时:{0}".format((end2-start2)))
案例J
测试推导式
源码如下:
#测试推导式
#列表推导式
y = [x*2 for x in range(1,50) if x%5==0]
print(y)
y = []
for x in range(1,50):
if x%5==0:y.append(x*2)
print(y)
cells = [(row,col) for row in range(1,10) for col in range(1,10)]
print(cells)
#字典推导式
my_text = "i love you, i love sxt, i love gaoqi"
char_count = { c:my_text.count(c) for c in my_text}
print(char_count)
#课下作业,使用普通的循环实现上面字典推导式实现的字符出现次数的统计
#集合推导式
b = {x for x in range(1,100) if x%9==0}
print(b)
#生成器推导式(生成元组)
gnt = (x for x in range(4))
#print(tuple(gnt))
for x in gnt: #gnt是生成器对象,生成器是可迭代的对象,只能使用一次
print(x,end=",")
print(tuple(gnt))
案例K
测试zip()并行迭代
源码如下:
# 测试zip()并行迭代
for i in [1,2,3]:
print(i)
names = ("高淇","高老二","高老三","高老四")
ages = (18,16,20,25)
jobs = ("老师","程序员","公务员")
for name,age,job in zip(names,ages,jobs):
print("{0}--{1}--{2}".format(name,age,job))
for i in range(3):
print("{0}--{1}--{2}".format(names[i],ages[i],jobs[i]))