Rookie learning python next day - the three processes (sequence, selection, circulation)

Three processes (program control flow):

软件		代码量很大的		本质:为人类服务的,解决生活问题
静音的开启:我说话,同学们听见的声音,打开麦克风
静音关闭:关闭系统的麦克风
当不同选项被选择的时候,程序代码的走向就会发生变化。

First, the sequence:

	从上到下,从左而右

Second, the selection structure (branched structures):

	当代码遇到了不同的情况时,不同的选择,代码的实现是不一样的。
	python中没有switch语句
	
	#条件运算的结果必须是bool,表示条件是否成立
	1、单分支结构
		if  条件:		#python是通过强制缩进来判断语句之间的层次关系
			# 强制缩进!!!
			#条件满足的情况下,执行缩进的的代码
			#四个空格不等于一个tab!!!
			强调:一定要确定一种缩进
			单分支的缺点:
				只能处理条件成立的情况,不能处理条件不成立的情况
#接受一个控制台上的年龄,判断该用户是否成年
age = int(input("请输入您的年龄:"))

#判断age是否成年
if age >= 18:
	#此时缩进体内的,只有条件成立的情况下会执行
	print("恭喜你,成年了")
	print("可以多行吗")
	2、双分支结构
		if   condition :
			# 写条件成立的代码
		else :
			# 条件不成立的代码
age = int(input("请输入您的年龄:"))

if age < 18:
	print("对不起,你还没有成年,回家写作业去")
else:
	print("成年了,去狂欢吧")
	3、多(三)分支结构
		if  	条件1:
			# 条件成立
		elif	 条件2:
			# 条件2 成立的时候
		elif	条件3:
			# 条件3成立的时候
		......
		else:
			# 表示剩余条件
		学生有成绩0~100分之间
		实现成绩判定系统,要求如下:
		1、如果成绩100,那么输出--恭喜你,满分
		2、90~100	优秀
		3、80~90		良好
		4、70~80		一般
		5、60~70		合格
		6、0~60		不合格
		7、0			恭喜你,人生不容易
		8、大于100 or 小于0	输错了	
score = float(input("请输入您的成绩:"))
#判断
#判断score==100
if score == 100:
	print("恭喜你,满分")
elif score >=90 and score < 100:
	print("优秀")
elif score >=80 and score < 90:
	print("良好")
elif score >=70 and score < 80:
	print("一般")
elif score >=60 and score < 70:
	print("合格")
elif score >0 and score <60:
	print("不合格")
elif score == 0:
	print("恭喜你,人生不容易")
else:
	print("输错了")

Third, circulation:

while循环:	
	语法结构:
	while    condition :
			# 缩进的所有代码就是我们的循环体
	while  条件:
			#一行或者多行代码,代表要循环的代码
			#循环体
	案例:	
#输出一百次hello world
#定义一个标识符,说明循环次数
index = 0
while index < 100:
	print("hello world",index)
	index += 1
	求1~100的和:
	使用循环
#求0~100的和
#开始的那个数
index = 0
#记录和
sum = 0
while index <= 100 :	
	#累计和
	sum += index
	index +=1
print("得到0~100的和,和的结果是:"sum)
	求1~100的积,使用while循环完成:	
#求1~100的积
index = 1
sum = 1
while index <= 100:	
	sum *= index
	index +=1
print("1~100的积是:"sum)	
	求0~100范围内偶数的和	:	
index = 0
sum = 0
while index <= 100:
	#判断index是不是偶数
	if index % 2 == 0
		#肯定index是偶数
		sum += index
	#index += 必须放在if同级,否则会构成一个死循环
	index += 1
print("0~100以内偶数的和为:"sum)
1、continue关键字
	一般是在循环中使用,目的就是跳过本次循环,执行下次循环(当然条件必须成立)
	在python程序中,在循环中一旦遇到continue关键字,本次循环就会终止。		
#打印0~100内的数字,但不打印3的倍数
index = 0
while index <= 100:
	index += 1
	if index % 3 == 0
		continue
	else:
		print(index)
2、break关键字
	中断,当循环体中,遇到break关键字,那么无论条件是否满足,循环直接终止		
#打印0~49
index = 0
while index <= 100:
	if index == 50:
		#break终止了循环
		break
	print(index)
	index += 1
print("-----game over-----")
for循环
	for    in	结构,类似于其它语言中的foreach,或者for加强
	in关键字  容器
	for   in   容器:
			#循环体
			#for in 本质就是不断迭代容器,每次就会迭代一个容器中元素,直到迭代完成	
#users 是一个列表
users = ["王""赵""李""张"]
#for 循环的使用
for u in users :
	#continue break在for循环中也可以使用
	#if u == "赵":
		#continue
		#break
	print(u)
print("----game over----")
	使用for循环完成1~100的和:
	range的全局函数:
		至少要有一个参数
		一个参数,默认从0开始:range(num)		0 ~ num-1   #  [0, num)	一定要注意:前闭后开的区间		
		两个参数:range(10,21),两个参数时,相当于我们指定了开始值
		三个参数:range(5,100,2),此时,每两个数,生成一个数    #步长为2
#输出0~99
for i in range(100):
	print(i)	
#输出1~100的和
for i in range(1,101)
	sum += i
print(sum)
	打印直角三角形图:
layer = int(input("请输入您要打印的层数:"))

for i in range(layer):
	#控制了行
	#每行有多少的*
	for j in range(1,i+1):
		print("*",end="")
	#当里卖弄的循环结束时
	#让程序换行
	print("")
print函数,标准输出函数,sep参数默认是“ ”,end=“\n"	,默认换行
print("hello world", end="")    #不换行

Print multiplication table:

for i in range(1,10):
	for j in range(1,i+1):
		res = i * j
		if res < 10:
			#为了代码的美观,可以适当调节空格
			print("%s x %s = %s " %(j,i,(res),end="")
		else:
			print("%s x %s = %s  " %(j,i,res),end="")
	print()

Cycling added:

在python中,循环结束后,可以跟着else
	while:
		pass
	else:
	------
	for:
		pass
	else:
此时这个else是属于循环的,当循环正常结束(没有被break打断),进入到else
	当循环被打断,则不会进这段代码。
index = 0
while index < 100:
	#if index == 50:
	#	break
	print(index)
	index += 1
else:
	print("循环正常结束了")
print("循环结束了")

Published 21 original articles · won praise 8 · views 1231

Guess you like

Origin blog.csdn.net/weixin_44029504/article/details/104486757