Python简要整理02_分支(if)循环(while,for)

接Python_简要整理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内容.
posted @ 2021-02-18 19:34  Zhou_DE_blogs  阅读(80)  评论(0)    收藏  举报