1、在选择和循环结构中,条件表达式情况如下
值为false
值为true
2、条件表达式中不能有赋值符“=”,出现即报错
tips:
多条语句缩进必须对齐一致
描述语法格式时,中括号[]通常表示可选非必选
单分支
双分支
三元条件运算符
tips:
多分支结构,几个分支之间有逻辑关系,不能随意颠倒顺序
注意控制好不同级别代码块的缩进,缩进量决定代码的从属关系
if 条件表达式 1 :
语句 1/语句块 1
elif 条件表达式 2:
语句 2/语句块 2 .
.
.
elif 条件表达式 n :
语句 n/语句块 n
[else:
语句 n+1/语句块 n+1
]
if 表达式 1:
语句块 1
if 表达式 2:
语句块 2
else:
语句块 3
else:
if 表达式 4:
语句块 4
tips:
1、循环结构用来重复执行一条或多条语句
2、判断条件表达式,true则重复执行循环体里的语句,执行后再次回到此过程
3、循环体中应有使循环体趋于结束的语句,否则便是死循环
while
while 条件表达式:
循环体语句
for
for 变量 in 可迭代对象:
循环体语句
可迭代对象
序列:字符串、列表、元组
字典
迭代器对象
文件对象
break语句
continue语句
else语句
while 条件表达式:
循环体
else: 语句块
或者:
for 变量 in 可迭代对象:
循环体
else:
语句块
zip()
列表推导式
字典推导式
集合推导式
生成器推导式(生成元组)
#列表推导式
print([x for x in range(1,5)])
print([m*2 for m in range(1,20) if m%2==0])
print([a for a in 'abcdefg'])
ab = [(a,b) for a in range(1,5) for b in range(1,5)]#2个循环嵌套
for abs in ab:
print(abs,end=',')
print("-----------------------------------------------")
#字典推导式
my_text = 'xiaohu,xiaoshou,xiaoshui'
char_count = {
c:my_text.count(c) for c in my_text}
print(char_count)
print("-----------------------------------------------")
#集合推导式
print({
x for x in range(1,10) if x%2==0})
print("-----------------------------------------------")
#生成器推导式
d = (x for x in range(1,100) if x%5==0)
print(d) #结果是一个生成器对象,没有具体数值,只能使用一次
score = int(input("请输入你的分数:"))
grade = '' #创建空字符接收
if score <60:
grade = '不及格'
elif score <80:
grade = '及格'
elif score <90:
grade = '良好'
elif score <=100:
grade = '优秀'
print("分数是{0},等级是{1}".format(score,grade))
x = int(input("请输入x坐标\n"))
y = int(input("请输入y坐标\n"))
if(x==0 and y==0):
print("原点")
elif(x==0):
print("y轴")
elif(y==0):
print("x轴")
elif(x>0 and y >0):print("第一象限")
elif(x<0 and y >0):print("第二象限")
elif(x<0 and y <0):print("第三象限")
else:print("第四象限")
score = int(input("请输入你的分数:"))
grade = ''
if (score>100 or score <0):
score = int(input("false!请重新输入数字:"))
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("请输入数字:\n"))
degree = 'ABCDE'
if score>100 or score <0:
score = int(input("输入错误,重新输入数字:\n"))
else:
num = score//10
if num<6:num = 5
print("你的分数是{0},等级是{1}".format(score,degree[9-num]))
num2 = 0
sum_all = 0
sum_ou = 0
sum_ji = 0
while num2 <= 100:
sum_all += num2
if num2 % 2 == 0:
sum_ou += num2
else:
sum_ji += num2
num2 += 1 # 添加条件使while语句趋向于结束
print("0-100累计和是:", sum_all)
print("0-100奇数和是:", sum_ji)
print("0-100偶数和是:", sum_ou)
sum_all = 0
sum_ou = 0
sum_ji = 0
for i in range(101):
sum_all += i
if i%2==0:
sum_ou+= i
else:
sum_ji+=i
print("0-100的累加和是{0},奇数和是{1},偶数和是{2}".format(sum_all,sum_ji,sum_ou))
for i in range(5):
for i in range(5):
print(i,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='xiaohu',age=20,salary=30000,city='PeiKing')
r2 = dict(name='xiaoshou',age=21,salary=20000,city='Tibet')
r3 = dict(name='xiaoshui',age=22,salary=10000,city='Boston')
tb = [r1,r2,r3]
for i in tb:
if i.get('salary')>15000:
print(i)
empSum = 0
salarySum = 0
salary = []
while True:
s = input("输入员工工资:\n")
if s.upper()=='Q' or s.upper() =='q':
print("over,out")
break
if float(s) < 0:
continue
empSum +=1
salary.append(float(s))
salarySum += float(s)
print("员工数{0}个".format(empSum))
print("员工薪资",salary)
print("员工平均薪资{0}".format(salarySum/empSum))
salarySum = 0
salary = []
empSum = 0
for i in range(4):
s = input("请输入薪资:\n")
if s.upper()=='Q' or s.upper()=='q':
print("over,out")
break
if float(s) <0:
continue
empSum += 1
salarySum += float(s)
salary.append(float(s))
else:
print("录入结束")
print("员工数{0}".format(empSum))
print("员工薪资",salary)
print("员工平均薪资{0}".format(salarySum/empSum))
import turtle
#定义变量接收画笔信息
t = turtle.Pen()
#定义形状颜色速度
t.width(5)
t.speed(0) #0最快,1最慢
colors = ('red','blue','purple','yellow','green','black')
#定义绘制办法
for i in range(20): #圆圈数量
t.penup() #找到位置
t.goto(0,-i*10) #坐标向下,下降比例一致
t.pendown() #开始画
t.color(colors[i%len(colors)])
t.circle(10+i*10) #圆圈一层比一层大
turtle.done()
import turtle
t = turtle.Pen()
width = 30
num = 18
x1 = [(-250,250),(-250+width*num,250)]
y1 = [(-250,250),(-250,250-width*num)]
t.speed(0)
for i in range(0,19):
t.penup()
t.goto(x1[0][0],x1[0][1]-30*i)
t.pendown()
t.goto(x1[1][0],x1[1][1]-30*i)
for i in range(0,19):
t.penup()
t.goto(y1[0][0]+30*i,y1[0][1])
t.pendown()
t.goto(y1[0][0]+30*i,y1[1][1])
turtle.done()