Getting Started with Python - Three Process

Three processes

  • Sequential structure: sequentially line by line in the order of execution of the code. From left to right, top to bottom .
  • Selection structure: program code for performing various processes in accordance with different process conditions.
  • Loop structure: the program according to the specified conditions, if the condition is satisfied repeats the processing code specifies an end.

significance

To design a software, its code amount is great, based on the emergence of three different conditions, different logic execution process.

A. Order process

Sequentially line by line in the order of execution of the code. From left to right, top to bottom.

II. Selection Process

Select structured statement --if statement

# expression 是一个表达式判断条件,表达式执行结果为 True 或者 False
if expression:
	# 选择结构中执行的代码,代码缩进 4 个空格,也就是一个 Tab 键[强制规范缩进]
	执行的代码

Branch Case

res = input("请输入你 1-7 数字,表示今天星期几:");
res = input("请输入你 1-7 数字,表示今天星期几:");

If a single branch

if age >= 18:
 # 此时缩进体内,只有条件成立的情况下才会执行
	print("恭喜您,您已经成年了")

Dual branch if-else

 if res == "1":
	print("今天星期一")
else:
	print("今天不是星期一")

Multi-branch if-elif-else


if res == "1":
	print("今天星期一")
elif res == "2":
	print("今天星期二")
elif res == "3":
	print("今天星期三")
elif res == "4":
	print("今天星期四")
elif res == "5":
	print("今天星期五")
elif res == "6":
	print("今天星期六")
elif res== "7" :
	print("今天星期天")
else:
	print("你输入了错误的数字")

Note: Python does not provide switch-case grammatical structure.

III. Cyclic structure

  • The implementation of a process for repeating the same logic
  • python provides the structure for ... in loop and while loop structure
  • for ... in loop structure and focus on loop through the use of fixed data list
  • while loop structure focusing on the process conditions in the determination of the cycle to the execution cycle

for ... in ...... loop structure

for 变量 in 一组数据的列表:
 //直接使用变量,这里的变量每次就是一个列表中的元素

Cycle Case

users = ["吴彦祖","周杰伦","吴彦祖","余文乐"]
for u in users:
	print(u);

Code runs as follows:
Here Insert Picture Description

Similarly, we can also use the python built-in functions to dynamically generate lists, dynamic lists also can be recycled for processing.


# 求 0—100 的和:
sum = 0                    #申明一个变量为 0
                           #range()函数能生成一个从 0 开始的列表,每次加一。最后一个值比传递的参数少一
                           #如 range(5),则生成 0,1,2,3,4。注意
for i in range(101):
	sum += i
	print("0--100 的和是:%d"%sum)
        #运行结果是: 5050
        
# 求 1—10 的积:
sum = 1;
for i in range(10):
	sum *= (i+1)
	print("1--100 的积是:%d"%sum)
        #运行结果是: 3628800

while loop structure
is repeatedly executed when the conditions are satisfied, out of the loop when the condition is not satisfied.
Code Cases

# 以登录用户名和密码为例
username = ''         #输入用户名称
password = ""         #输入用户密码

status = False        #状态判断的标示符
while username != "admin" or password != "123456":
	if status: 
		print("您输入的用户名称或者密码错误!!,请再次输入!!")
	username = input("请输入您的用户名称:")    #输入用户密码

	password = input("请输入你的密码:")
	status = True
	print(username+"---"+password)
print("登录成功")

break keyword
is mainly used in the code in the loop body, for out of the loop when certain conditions are met.
Case study break

#计算1~100的和,如果一旦出现20,代码则跳出不再执行
sum = 0;
for i in range(101):
	if i == 20:
		break;
	sum += i;
# str 可以讲一个数字转换为字符串,那么字符串就可以拼接了,否则会报错的
# 如:print("此时 sum="+sum); #会报错
# 只能使用 print("此时 sum=%d"%sum);
print("此时 sum="+str(sum));

If you want to terminate the loop when once certain conditions are met, we can use this time to break the code to terminate the cycle continues.
continue keyword
is mainly into the next cycle after cycle after termination of this condition is met.

# 求 1-100 的和,但是遇到偶数不做任何处理
sum = 0;
for i in range(101):
	if i % 2 == 0:
		continue;
	sum += i;
print("1-100 的奇数的和是:"+str(sum));
# 结果为:2500
Released five original articles · won praise 9 · views 304

Guess you like

Origin blog.csdn.net/weixin_46169495/article/details/104433488