python入门基础——三大流程***


*一 顺序结构:**程序都是从上到下的顺序执行
二 选择结构:

1.单if情况:
*在有特殊的一种情况符合一种条件。
格式如下:
if 条件:
条件成立执行的语句
流程图:
在这里插入图片描述
举例代码:

i = int(input("please enter a num:"))
if i == 10:
	print("say hello")
print("say bye")

运行结果

E:\python\2020-2-17\dama\two>python if.py
please enter a num:10
say hello

E:\python\2020-2-17\dama\two>python if.py
please enter a num:3

E:\python\2020-2-17\dama\two>python if.py
please enter a num:10
say hello

2.双if情况:
适合于出现两种情况,eg:抛硬币的正反
格式:
if 条件:
条件成立执行的语句
else:
条件不成立执行的语句
流程图
在这里插入图片描述
代码演示:

i = int(input("please enter a age:"))
if i >= 18:
	print("你是成年人")
else:
	print("你不是成年人")

运行结果:

E:\python\2020-2-17\dama\two>python if.py
please enter a num:10
你不是成年人

E:\python\2020-2-17\dama\two>python if.py
please enter a num:18
你是成年人

3.多if情况
格式:
if 条件1:
执行条件1成立的语句
elif 条件2:
执行条件2成立的语句
elif 条件3:
执行条件3成立的语句

else:
执行的语句
print()
流程图;
在这里插入图片描述
注:一旦执行到中间条件成立的语句,说明之前的条件不成立,和后面的条件不在执行,直接出选择流程。
如图所示
举例代码:

score = int(input("please enter student score:"))
if score == 100:
	print("A+")
elif score >=90 and score < 100:
	print("A")
elif score >= 80 and score < 90:
	print("B")
elif score>=70 and score < 80:
	print("C")
elif score >= 60 and score < 70:
	print("D")
else:
	print("成绩不合格")
print("欢迎使用")

运行结果:

E:\python\2020-2-17\dama\two>python if.py
please enter student score:54
成绩不合格
欢迎使用
E:\python\2020-2-17\dama\two>python if.py
please enter student score:89
B
欢迎使用
E:\python\2020-2-17\dama\two>python if.py
please enter student score:100
A+
欢迎使用

总结:
1.他们的条件判断都是布尔类型(结果值为True或False),因此条件也成为布尔表达式。
2.注意缩进,if 语句结束后会自动缩进,如果手动缩进,建议使用tab键(也可以按四个空格),但是一旦你第一次使用什么方式缩进后面必须统一。注:四个空格并不等于一次tab键。

三,循环
目的:主要解决重复性执行的代码
如果你想要的去把一个东西重复的显示很多次,或者打印一串连续数字会很方便,简洁。
循环流程图:
在这里插入图片描述
python中有两种循环方式:
1.while
格式:
while 条件:
循环体

代码

index = 0
while index < 5:
	print("hello world", index)
	index += 1

运行结果

E:\python\2020-2-17\dama>python while.py
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4

2.for
格式:

for 变量 in range(): #in是关键字
循环体
range()全局函数:它产生一个序列
它的使用:
(1)

for i in range(5):    #只写一个参数,则默认从0开始
	print(i,end="")	

运行结果

01234

(2)

for i in range(1,6):
	print(i,end="")	

运行结果

E:\python\2020-2-17\dama>python while.py
12345

(3)
range(2,8, 2) 指的是:[2,8) 步长为2,不写的话默认为1

for i in range(2,8,2):
	print(i,end="")	

运行结果

246

(4) 步长为负1, 此时左边的数要比右边大,而步长为正,则就要左边的数比右边的数小。

for i in range(9,3,-1):
	print(i,end="")	

运行结果

987654

代码

for index in range(0,5):
	print("hello", index)
	index += 1

如果学过c语言,for index in range(0,5): 可以理解为 for(i = 0 ;i < 5 ;i++)。
运行结果

E:\python\2020-2-17\dama>python while.py
hello 0
hello 1
hello 2
hello 3
hello 4


for 变量 in 容器:

users = ["haha", "嘻嘻", "换换", "消消"]
for u in users:
	print(u)

运行结果:

E:\python\2020-2-17\dama>python while.py
haha
嘻嘻
换换
消消

关键字:
continue

遇到continue关键字,本次循环就会终止,不影响下一次的循环。

users = ["haha", "嘻嘻", "换换", "消消"]
for u in users:
	if u == "嘻嘻":
		continue
	print(u)
print("over")

运行结果

E:\python\2020-2-17\dama>python while.py
haha
换换
消消
over

break
当循环遇到break,不管条件是否满足,循环直接终止。

users = ["haha", "嘻嘻", "换换", "消消"]
for u in users:
	if u == "嘻嘻":
		break
	print(u)
print("over")

运行结果:

E:\python\2020-2-17\dama>python while.py
haha
over

注:以上两个关键字,条件必须满足,才会执行。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值