1、条件语句(if-else )
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
if-else语句结构
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句;
如果 "condition_1" 为False,将判断 "condition_2";
如果"condition_2" 为 True 将执行 "statement_block_2" 块语句;
如果 "condition_2" 为False,将执行"statement_block_3"块语句。
Python中用elif代替了else if,所以if语句的关键字为:if – elif – else。
"""in-else,判断分数等级"""
score = input("请输入你的得分:")
if score.strip().isdigit():
socre = int(score.strip())
if socre >= 90:
print("非常棒,成绩优秀,得分是:{0}".format(socre))
elif socre >= 80:
print("很不错,成绩良好,得分是:{0}".format(socre))
elif socre >= 70:
print("一般般,成绩中上,得分是:{0}".format(socre))
elif socre >= 60:
print("要加油,成绩及格,得分是:{0}".format(socre))
else:
print("加把劲,成绩不及格,得分是:{0}".format(socre))
else:
print("输入的不符合要求")
if-else语句中常用的操作符
操作符描述
< 小于
<= 小于或等于
> 大于
>= 大于或等于
== 比较对象是否相等
!= 不等于
2、循环语句
Python中的循环语句有 for 和 while。
Python循环语句的控制结构图如下所示:
循环语句(while)
Python中while语句的一般形式:
while 判断条件:
statements
"""while 猜数字游戏"""
num = 5
print("猜数字游戏,范围1-10")
guess = 0
while guess != num:
temp = input("请输入你要猜想的数字:")
if temp.strip().isdigit():
guess = int(temp)
if guess > num:
print("你输入的数字大了!")
elif guess < num:
print("你输入的数字小了!")
else:
print("恭喜你,猜对了,真棒!")
else:
print("请输入的不是数字!")
while 循环是当条件符合了就会一直执行循环体,如果设置了为true 或者 1,将会是死循环;
循环语句(for)
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的一般格式如下:
for in :
else:
for 循环多用与可以迭代的对象
使用range()函数是用来生成一个序列的。
"""九九乘法表"""
for i in range(1,10):
for j in range(1,i+1):
print("{0} x {1} = {2}\t".format(j,i,j*i),end='')
print()
break、continue
在while循环和for循环中,可以使用break或continue跳出循环:
break语句可以跳出for和while的循环体。如果你从for或while循环中终止,任何对应的循环else块将不执行。
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
以下代码实例中,当输入的数字是0,将跳出以下的语句;当输入的数字和猜想的相等是,跳出整个循环;
"""while 猜数字游戏"""
num = 5
print("猜数字游戏,范围1-10!")
guess = 0
while 1:
temp = input("请输入你要猜想的数字:")
if temp.strip().isdigit():
guess = int(temp)
if guess == 0:
print("请输入大于0的数字!")
continue
elif guess > num:
print("你输入的数字大了!")
elif guess < num:
print("你输入的数字小了!")
else:
print("恭喜你,猜对了,真棒!")
break
else:
print("请输入的不是数字!")
3、使用python来解决数学难题
abcd*9=dcba
"""abcd*9=dcba"""
for a in range(1,10):
for b in range(0,10):
for c in range(0,10):
for d in range(0,10):
if int(a*1000 + b*100 + c*10 + d)*9 == int(d*1000 + c*100 + b*10 + a):
print("a={0},b={1},c={2},d={3}".format(a, b, c, d))
计算结果是:a=1,b=0,c=8,d=9
阶乘
"""计算阶乘"""
num = input("计算正数的阶乘,请输入一个正数:")
total = 1
if num.strip().isdigit():
num = num.strip()
if int(num) >= 0:
if int(num) == 1:
total = 1
print("{0}的阶乘是:{1}".format(num, total))
else:
for i in range(2,int(num)+1):
total *= i
print("{0}的阶乘是:{1}".format(num, total))
else:
print("请输入一个大于零的数字!")
else:
print("您输入的不是数字")
计算阶乘的和
"""计算阶乘的和"""
print("计算正数的阶乘的和:1!+2!+...+n!")
num = input("请输入一个正数:")
total = 1
temp = 1
if num.strip().isdigit():
num = num.strip()
if int(num) >= 0:
if int(num) <= 1:
total = 1
print("{0}的阶乘是:{1}".format(num, total))
else:
for i in range(2,int(num)+1):
temp *= i
total += temp
print("1!+2!+...+!{0}阶乘的和是:{1}".format(num, total))
else:
print("请输入一个大于零的数字!")
else:
print("您输入的不是数字")
计算一串字符串中包含的数字、字母、其他字符的总数
"""计算一串字符串中包含的数字、字母、其他字符的总数"""
strinput = input("请输入一串字符:")
totalNum = 0
totalPha = 0
totalOth = 0
totalSpa = 0
for i in strinput:
if i.isdigit():
totalNum += 1
elif i.isalpha():
totalPha += 1
elif i.isspace():
totalSpa += 1
else:
totalOth += 1
print("输入的字符串有--> 数字:{0}个,字母:{1},空格:{2},其他字符:{3}".format(totalNum, totalPha, totalSpa, totalOth))