首页 > 编程语言 > 详细

Python复习02_分支循环02

时间:2021-02-18 23:29:32      阅读:40      评论:0      收藏:0      [点我收藏+]

接Python复习01_简要01


十、选择结构

(1)练习

age_ip=eval(input(‘please input your age:‘))
if age_ip >= 18:
	print("chengnian")
else:
	print(‘yes‘)

(2)比较(关系)运算符

  • == 检查两个值是否相等

  • != <= >= < >

(3)逻辑运算符

  • and 同真则真

  • or 有真则真

  • not 假则真,真则假

(4)elif 多条件判断

需求 根据分数 输出ABCD

sco=int(input(‘please input sco‘))
if sco < 60:
  print(‘D‘)
elif sco < 75:
  print(‘C‘)
elif sco < 85:
  print(‘B‘)
else:
  print(‘A‘)

十一、循环结构

(1)while循环嵌套

<1>需求

? *

? **

? ***

? ****

index = 1
while index < 6:
    i = 1
    while i <= index :
        print(‘*‘,end=‘‘)
        i+=1
    print(‘‘)
    index+=1

<2>需求 9*9乘法表

index = 1
while index < 10:
    i = 1
    while i <= index:
        print(i,‘x‘,index,‘=%d‘%(index*i),‘\t‘,end=‘‘,)
        i+=1
    print(‘‘)
    index +=1

(2)for循环

for i in range(0,3):
    print(‘hello world‘)
str1=‘hello world‘
for i in str1:
    print(i,‘hello world‘)

for c in ‘hello world‘:
    print(c,end=‘\\‘)

for i in range(5):
    print(i)

注意:range(start,stop,step)

(3)break

for i in ‘hello world‘:
	print(i,end=‘‘)
if i==‘ ‘:
	break
else:
  print(‘111111111111‘)

跳出整个循环后 else不执行

(4)continue

for i in ‘hello world‘:
	#print(i,end=‘‘) #在if之后方可生效
	if i == ‘ ‘:
		continue
	print(i,end=‘‘)
else:
	print(‘‘)
	print(‘ceshi‘)  #continue之后能够显示else内容.

Python复习02_分支循环02

原文:https://www.cnblogs.com/zhouDEblogs/p/14413225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!