1.if条件分支语句
python条件语句就是通过一条或者多条语句执行的结果(True/False)来决定执行的代码块。
(1)if单分支语句
if 逻辑语句:
语句(块)
demo:
print("Hello,Python")
if False:
print("向python致敬,我正在学习python")
print("Welldone.")
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
Hello,Python
Welldone.
Process finished with exit code 0
'''
(2)if双分支语句
if 逻辑语句1:
语句(块)1
else:
语句(块)2
demo:
learning="java"
if learning=="python":
print("啊真巧,我也在学python")
else:
print("小名和你一样也在学习Java")
print("nice to meet you")
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
小名和你一样也在学习Java
nice to meet you
Process finished with exit code 0
'''
(3)if多分支语句
if 逻辑语句:
语句(块)1
elif 逻辑语句2:
语句(块)2
...
elif 逻辑语句n-1:
语句(块)n-1
else:
语句(块)n
练习:根据百分制考试成绩返回等级:
90-100:A;80-90:B;70-80:C;60-70:D;0-60:E
grade=int(input("输入分数:"))
if grade>100:
print("最高分才100,多出来的分哪里来的??")
elif grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("E")
从控制台输入一个三位数,判断其是否为水仙花数
n = int(input("请输入一个三位数:"))
if n > 999 or n < 100:
print("您输入的不是三位数,请重新输入")
elif n == (n // 100) ** 3 + (n%100//10) ** 3 + (n % 10) ** 3:
print("yes")
else:
print("no")
2.while语句
循环语句
所谓循环,就是用来执行部分代码在一定条件下重复执行的代码
格式:
while 逻辑语句:
语句(块)
(1)利用while来打印1-30的数字
i=1
while i<30:
i+=1
print(i,end=' ')
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Process finished with exit code 0
'''
(2)利用while求1-100的和
i=1
n=i
while i<100:
i += 1
# print(i)【打印i的值
n += i
print("总和为:",n)
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
总和为: 5050
Process finished with exit code 0
'''
(3)利用while求1-100内所有偶数的和
i = 1
n = 0
while i < 101:
i += 1
if i % 2 == 0:
n += i
print("总和为:", n)
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
总和为: 2550
Process finished with exit code 0
'''
(4)求100-999内所有的水仙花数
i = 100
while i < 1000:
if i == (i // 100) ** 3 + (i % 100 // 10) ** 3 + (i % 10) ** 3:
print(i)
i += 1
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
153
370
371
407
Process finished with exit code 0
'''
3.for循环语句
python中,for可以用来遍历任何序列(字符串,列表,元组等)
格式:
for i in 序列:
语句(块)
(1)range()
range(end)#[0,end-1],end为整数
range(start,end)#[start,end-1],两个数均为整数
range(start,end,step)#[start,start+step,start+step*2,...,end-1]
(1)利用for来打印1-100的数字
for i in range(1,101):
print(i,end=' ')
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Process finished with exit code 0
'''
(2)利用for求1-100的和
n = 0
for i in range(1, 101):
n += i
print(n)
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
5050
Process finished with exit code 0
'''
(3)利用for求1-100内所有偶数的和
n = 0
for i in range(0, 101,2):
n += i
print(n)
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
2550
Process finished with exit code 0
'''
(4)求100-999内所有的水仙花数
for i in range(100,1000):
if i==(i//100)**3+(i%100//10)**3+(i%10)**3:
print(i)
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
153
370
371
407
Process finished with exit code 0
'''
(2)break和continue
- break用来结束一个循环
for i in range(0, 15, 2):
print(i, end=' ')
if i == 10:
break
'''
结果:
D:\Python\python.exe D:/python_01/day02.py
0 2 4 6 8 10
Process finished with exit code 0
'''
- continue终止本次循环的动作,直接进入下次循环
(3)while…else,for…else
-
while…else,for…else和else是一块的,for和else也是一块的
-
当有break或者return的时候就会跳出while。如果没有break或者return,不管while是否执行,都会执行else语句