控制语句执行流程

1.if 基础

1.1形式1:

if <条件>:
<语句>
案列1:
[root@shell python]# cat if.python

#!/bin/usr/python
x=input('please input a integer:')
x=int(x)
if x<0:
	x=-x
print(x)

[root@shell python]# python if.python
please input a integer:-5
5

1.2 形式2:

if <条件>:
<语句1>
Else:
<语句2>
[root@shell python]# cat if.python

#!/bin/usr/python
x=input('please input a integer:')
x=int(x)
if x<0:
   print('你输入了一个负数。')
else:
   print('你输入了一个正整数')

[root@shell python]# python if.python
please input a integer:0
你输入了一个正整数
[root@shell python]# python if.python
please input a integer:-5
你输入了一个负数。

1.3 形式3:

if <条件1>:
<语句1>
Elif <条件2>:
<语句2>
Else:
<语句3>
[root@shell python]# cat if.python

#!/bin/usr/python
x=input('please input a integer:')
x=int(x)
if x<0:
	print('你输入了一个负数。')
elif x==0:
	print('你输入了一个零.')
else:
	print('你输入了一个正整数')

[root@shell python]# python if.python
please input a integer:1
你输入了一个正整数
[root@shell python]# python if.python
please input a integer:0
你输入了一个零.
[root@shell python]# python if.python
please input a integer:-1
你输入了一个负数。

判断你的学习成绩
[root@shell python]# cat if.python

#!/bin/usr/python
x=input('请输入你的总分:')
x=float(x)
if x>=90:
	print('优秀')
elif x>=80:
	print('良好')
elif x>=60:
	print('合格')
else:
	print('不及格')

[root@shell python]# python if.python
请输入你的总分:50
不及格
[root@shell python]# python if.python
请输入你的总分:70
合格

1.4 形式4:

if语句的嵌套
if <条件>:
if<条件>:
<语句1>
elif <条件>:
<语句2>
else:
<语句3>
[root@python 20200812]# cat 03.py

#!/usr/bin/python
a = int(input("请输入一个整数:"))
if a > 0:
	if a > 1000:
		print("无法表示")
	else:
		print("可以表示")
	print("且大于0")
else:
	if a < -1000:
		print("无法表示")
	else:
		print("可以表示")
	print("且小于0")

[root@python 20200812]# python 03.py
请输入一个整数:10001
无法表示
且大于0
[root@python 20200812]# python 03.py
请输入一个整数:40
可以表示
且大于0

2.for基础

2.1形式:

For <循环变量> in <遍历对象>:
<语句1>
else:
<语句2>

[root@shell python]# cat for.python

#!/bin/usr/python
for i in [1,2,3,4,5]:
	print(i,"i的平方是:",i*i)
else:
	print('循环结束!')

[root@python 20200812]# python 04.py
1 的平方是: 1
2 的平方是: 4
3 的平方是: 9
4 的平方是: 16
5 的平方是: 25
循环结束!

2.2 for语句与break语句,continue语句

break语句作用是中断循环的执行,如果在for循环中执行了break语句,for语句的遍历就会立即终止,即使还有未遍历完的数据,还是会立即终止for循环语句。
continue语句作用是提前停止循环体的执行,开始下一轮循环。
[root@shell python]# cat for01.python

#!/bin/usr/python
for i in [1,2,3,4,5]:
	print(i)
	if i==2:      #当i等于2时,执行其后的continue语句
		continue  #执行本句后,其后的if语句和print()不会执行
	print(i,"的平方是:",i*i)
	if i==4:    #当i等于4时,执行其后缩进的break语句
		break    #执行break时,终止循环
else:
	print('循环结束!')

[root@shell python]# python for01.python
1
1 的平方是: 1
2
3
3 的平方是: 9
4
4 的平方是: 16

2.3 for与range()函数

range([start,] stop[,step])
start:起始数,默认值0;
stop:终止数,如果只有一个参数x,那么range生产一个0到x-1的整数列表。
Step: 可选参数,步长,即每次循环序列增长值。
[root@shell python]# cat forra.python

#!/bin/usr/python
for i in range(4):
	print(i)
for i in range(0,7,2):
	print(i)

[root@shell python]# python forra.python
0
1
2
3
0
2
4
6

2.4 for语句与内置迭代函数

迭代函数:
Enumerate(seq) #编号迭代
Sorted(seq) #排序迭代
Reversed(seq) #翻转迭代
Zip(seq1,seq2,……)

[root@shell python]# cat em.python

#!/bin/usr/python
for i,item in enumerate('abcd'):
	print('第%d个字符是:%s' %(i,item))
[root@shell python]# python em.python 
第0个字符是:a
第1个字符是:b
第2个字符是:c
第3个字符是:d

>>> for i in sorted([3,1,6,0]):
...     print(i)
... 
0
1
3
6

3.用while循环执行语句

运行不确定次数的循环
形式:
while <条件>:
<语句1>
else:
<语句2>
[root@shell python]# cat while.python
[root@python 20200812]# cat 07.py

#!/usr/bin/python
alst = [1,2,3,4,5]
total = len(alst)
i = 0
while i < total:
	print(i,"的平方是:",i*i)
	i = i + 1
else:
	print("循环结束!") 

[root@shell python]# python while.python
0 的平方是: 0
1 的平方是: 1
2 的平方是: 4
3 的平方是: 9
4 的平方是: 16
循环结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老哥爱学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值