1,
if (3 < 5): #加不加括号都可以
print("我在里面")
print("我也在里面")
print("我在外面")
if "小甲鱼" == "小姐姐": #字符串直接判断
print("小甲鱼是小姐姐")
else:
print("小甲鱼不是小姐姐")
2, """ if、elif、while实现成绩分等级 """ #全靠缩进
temp = input("请输入成绩:")
score = int(temp)
while (score != -1) and (score != -2): #加不加括号都可以
if 0 <= score < 60:
print("D")
elif 60 <= score < 80:
print("C")
elif 80 <= score < 90:
print("B")
elif 90 <= score <= 100:
print("A")
else:
print("请输入0~100之间的数字") #用elif比用四个并列的if效率高
temp = input("请输入成绩:")
score = int(temp)
print("操作结束!")
3,"""条件表达式""" #把if else语句写在一行表示,方便嵌入
a = 3
b = 5
if a < b:
small = a
else:
small = b
print(small)
#用条件表达式实现
small = a if a < b else b
print(small)
4,score = 66
level = ('D' if 0 <= score <60 else
'C' if 60 <= score < 80 else
'B' if 80 <= score < 90 else
'A' if 90 <= score <= 100 else
"请输入1~100之间的分支") #多行语句可以用括号,知道就好,还是并列的if elif 实用
print(level)
5,"""条件表达式""" #把if else语句写在一行表示,方便嵌入
a = 3
b = 5
if a < b:
small = a
else:
small = b
print(small)
6,"""用条件表达式实现"""
small = a if a < b else b #不能写成:small = a if a < b else small = b
print(small)
temp = input("请输入成绩:")
score = int(temp)
while(score != -1) and (score != -2):
level = ('D' if 0 <= score <60 else
'C' if 60 <= score < 80 else
'B' if 80 <= score < 90 else
'A' if 90 <= score <= 100 else
"请输入1~100之间的分支") #多行语句可以用括号,知道就好,还是并列的if elif 实用
print(level)
temp = input("请输入成绩:")
score = int(temp)
print("操作结束")
7,""" 分支结构的嵌套 """
age = 18
ismale = True
if age < 18:
print("抱歉,未满18岁禁止入内")
else:
if ismale:
print("任君选购")
else:
print("抱歉,本店不适合小公举哦!")
8,"""循环结构:while , for,continue,break"""
"""while"""
love = "yes"
while love == "yes":
love = input("今天你还爱我吗")
i = 1
sum = 0
while i <= 1000000:
sum += i
i += 1
print(sum)
"""break与continue"""
while True:
answer = input("主人,我可以推出循环了吗?")
if answer == "可以":
break #直接跳出while循环
print("继续执行")
print("谢谢主人")
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue #continue 继续下一轮循环判断,不执行print(i)
print(i)
"""while,break后不会执行后面的else"""
day = 1
while day <= 7:
answer = input("今天打卡了吗?")
if answer != "是":
break
day += 1
else: #while相当于if,当while day <= 7条件成立时,
#不会执行else,只有到day > 7时,条件不成立,会执行else.
print("您已经打卡七天了")
"""循环的嵌套打印乘法表"""
i = 1
while i <= 9:
j = 1
while(j <= i):
print(j, "*", i, "=", i * j,end = " ")#设置一下end = " "表示不同式子之间用空格隔开
j += 1
print() #表示打印一个换行
i += 1
"""打印乘法表方法2"""
for i in range(1,10):
for j in range(1,i+1):
print(j, "*", i ,"=",i * j,end = ' ')
print()
"""break,continue 只能作用于一层循环"""
i = 1
j = 1
while i <= 7:
while j <= 7 :
j += 1
print("打印一次")
if(j > 1):
break
i += 1
"""for循环""" #格式:for A in B: A为迭代变量用于保存读出的值,B要迭代或遍历的对象
for each in "FishC": #each 为迭代变量,FishC为要迭代的对象
print(each)
i = 0
while i < len("FishC"):
print("FishC"[i])
i += 1
sum = 0
for i in 10000: #错误代码:TypeError: 'int' object is not iterable
#因为10000数字不可以做为"迭代对象"。可以是任何有序的“序列”对象:列表,元组,集合,字典和字符串
sum += i
print(sum)
for i in range(1,10000,1): #正确代码:range(起始数字start,结束数字end,步长step)
range三种用法:range(end),range(start,end),range(start,end,step)
注意:(1),不包括end所指的值即range(3,8)得到的值是3~7,range(10)=0~9
(2),start 默认从0开始.
sum += i
print(sum)
for i in range(2,10,2):
print(i)
for i in range(10,0,-1):
print(i)
9,"""判断一个数是不是素数:除了2和他本身不能被其他数整除"""
for n in range(2,10):
for i in range(2,n):
if n % i == 0:
print(n,"=",i,"*",n / i)
print(n,"不是一个素数")
break
else: #注意:此else与内层的for循环对应,for 也相当于if条件
print(n,"是一个素数")
08-03
08-03
08-03