• Python编程Day4——if判断、while循环、for循环


    、if判断

    语法一:

    if条件:
    代码块1
    代码块2
    代码块3
     1 示例:
     2 sex='female'
     3 age=18
     4 is_beautiful=True
     5 if sex =='female'and age>16  and age<20  and is_beautiful:
     6     print('开始表白。。。')
     7 else:
     8     print('阿姨好。。。')
     9     
    10 print('other  code1...')
    11 print('other  code2...')
    12 print('other  code3...')
    13 
    14 执行结果:第一个条件通过则输出:开始表白、other  code1......,第一个条件不通过则输出:阿姨好
     1 示例:
     2 sex='female'
     3 age=18
     4 is_beautiful=True
     5 if_sex =='female'and age>16  and age<20  and is_beautiful:
     6     print('开始表白。。。')
     7 print('other  code1...')
     8 print('other  code2...')
     9 print('other  code3...')
    10 执行结果:开始表白、other  code1.....
    语法二:
    if条件:
    代码块1
    代码块2
    代码块3

    else:条件不成立时执行的子代码块
    代码块1
    代码块2
    代码块3
     1 示例:
     2 sex='female'
     3 age=18
     4 is_beautiful=True
     5 if sex =='female'and age>16  and age<20  and is_beautiful:
     6     print('开始表白。。。')
     7 else:
     8     print('阿姨好。。。')
     9     
    10 print('other  code1...')
    11 print('other  code2...')
    12 print('other  code3...')
    13 
    14 执行结果:第一个条件通过则输出:开始表白、other  code1......,第一个条件不通过则输出:阿姨好

    语法三:
    if条件1:
    if条件2:
    代码块1
    代码块2
    代码块3
     1 示例:
     2 sex = 'female'
     3 age = 18
     4 is_beautiful = True
     5 is_sucessful=True
     6 height=1.70
     7 
     8 if sex == 'female' and age > 16 and age < 20 and is_beautiful  and height >1.60  and  height <1.80:
     9     print('开始表白。。。')
    10     if is_sucessful:
    11           print('在一起。。。')
    12      else:
    13           print('什么爱情不爱情的。。。。。')
    14 else:
    15 print('阿姨好。。。')
    16 
    17 print('other  code1...')
    18 print('other  code2...')
    19 print('other   code3...')
     1 # 注意在代码过长的情况下可以使用\来进行分隔:
     2 # is_beautiful=\
     3 #     True
     4 # print(is_beautiful)
     5 # 输出结果:True
     6 '''
     7 sex = 'female'
     8 age = 18
     9 is_beautiful = True
    10 is_sucessful=True
    11 height=1.70
    12 
    13 if sex == 'female' and age > 16 and age < 20 and is_beautiful  \
    14         and height >1.60  and  height <1.80:
    15     print('开始表白。。。')
    16     if is_sucessful:
    17           print('在一起。。。')
    18     else:
    19           print('什么爱情不爱情的。。。。。')
    20 else:
    21     print('阿姨好。。。')
    22 
    23 print('other  code1...')
    24 print('other  code2...')
    25 print('other  code3...')
    语法四:
    if条件1:
    代码块1
    代码块2
    代码块3
    elif条件2:
    代码块1
    代码块2
    代码块3
    elif条件3:
    代码块1
    代码块2
    代码块3
    else:
    代码块1
    代码块2
    代码块3
     1 示例:如果:成绩>=90,那么:优秀
     2 
     3        如果成绩>=80且<90,那么:良好
     4 
     5        如果成绩>=70且<80,那么:普通
     6 
     7        其他情况:很差
     8 
     9 score=input('请输入成绩/please input your score:   ')
    10 score=int(score)
    11 if score>=90:
    12     print('优秀')
    13 elif score>=80  and   score<90:
    14     print('良好')
    15 elif score>=70 and  score<80:
    16     print('普通')
    17 else:
    18      print('很差')

     二、while循环

    语法:
    while 条件
    代码1
    代码2
    代码3

    1 示例:
    2 while  True:
    3     name=input('please input your name:')
    4     pwd=input('please input your password:')
    5 
    6     if name =='egon' and  pwd =='123':
    7         print('login successful')
    8     else:
    9          print('username  or password error')注:现这个循环是在持续运行中
     1 方式一:条件改为False,在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会结束。
     2 示例:
     3 tag=True
     4 while tag:
     5     name=input('please input your name:')
     6     pwd=input('please input your password:')
     7 
     8     if name =='egon' and  pwd =='123':
     9         print('login successful')
    10         tag=False
    11     else:
    12          print('username  or password error')
    13      print('===>>>')
     1 方式二:while+break
     2 break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环
     3 
     4 示例:
     5 while  True:
     6     name=input('please input your name:')
     7     pwd=input('please input your password:')
     8 
     9     if name =='egon' and  pwd =='123':
    10         print('login successful')
    11         break           #运行正确则结束本层循环
    12     else:
    13          print('username  or password error')
    14 
    15     print('===>>>>')  # (这个无法执行)
     1 方式三:
     2 while+continue 3 结束本次循环,直接进入下一次循环
     4 
     5 count=1
     6 while count<6:     #count=1,2,3,4,
     7 if count == 4:
     8 continue
     9 print(count)
    10 count +=1            #现执行结果在4这个数进行死循环
    11 
    12 count=1
    13 while count<6:     #count=1,2,3,4,5
    14     if count == 4:
    15     count += 1
    16     continue
    17 print(count)
    18 count += 1
    19     
    20 while  True:
    21     name=input('please input your name:')
    22     pwd=input('please input your password:')
    23 
    24     if name =='egon' and  pwd =='123':
    25         print('login successful')
    26         break           #运行正确则结束本层循环
    27     else:
    28         print('username  or password error')
    29         continue        #结束本次循环
    30     print('===>>>>')  # (这个无法执行)

    三、for循环

    1 for循环的强大之处在于循环取值
    2 l=['a','b','c','d','e']
    3 i=0
    4 while i< len(l):
    5    print(l[i])
    6     i +=1
    7 
    8 for x in l:  #x='b'
    9 print(x)
    1 dic={'name':'egon','age':18,'gender':'male'}
    2 for x in dic:
    3      print(x,dic[x])
     1 1、for+else
     2 names=['egon','kevin111_dsb','alex_dsb','mac_dsb']
     3 for name in names:
     4     if name=='kevin_dsb':
     5         break
     6     print(name)
     7 else:
     8     print('=======>')  #  执行结果是正常执行完所有姓名并结束
     9     '''
    10 '''
    11 names=['egon','kevin_dsb','alex_dsb','mac_dsb']
    12 for name in names:
    13     if name=='kevin_dsb':
    14         break
    15     print(name)
    16 else:
    17     print('=======>')  #  执行结果是输出egon
    18     '''
    19 '''
    1 2、for+continue
    2 
    3 nums=[11,22,33,44,55]
    4 for x in nums:
    5     if x == 22 or x == 44:
    6         continue
    7     print(x)  #执行结果是11、33、55
     1 3、for+range()
     2 
     3 range的用法  (cmd中执行结果)
     4 >>> range(1,5)
     5 [1, 2, 3, 4]
     6 >>> for i in range(1,5):
     7 ...     print(i)
     8 ...
     9 1
    10 2
    11 3
    12 4
    13 >>> range(1,5,1)
    14 [1, 2, 3, 4]
    15 >>> range(1,5,2) # 1 3
    16 [1, 3]
    1 示例:for i in range(1,5,2):  #range是只顾头不顾尾,2代表步长指1,1+2=3,3+2=5(不包含5所以不输出)
    2           print(i)  #  执行结果是1,3
    3 
    4 for i in range(5)   #  >>>>0,1,2,3,4
    5 print(i)  #  >>>>0,1,2,3,4
    1 4、for嵌套
    2 for i in range(3):   # >>>>i=0,1,2
    3     for j in range(4):#  >>>>j=0,1,2,3
    4         print(i,j)
    5         
    6 # 最终执行结果:  如i=0(不变)j=0        0 0      1 0     2 0
    7                 i = 0(不变)j=1        0 1      1 1     2 1
    8                 i = 0(不变)j=2        0 2      1 2     2 2
    9                 i = 0(不变)j=3        0 3      1 3     2 3
  • 相关阅读:
    python--模块导入与执行
    python--re模块
    python--递归函数
    CSRF攻击与防御
    XSS跨站脚本攻击
    HTTP协议详解以及URL具体访问过程
    Git服务器搭建
    浅谈PHP异常处理
    常用的几个PHP加密函数
    PHP将数据导出Excel表中(投机型)
  • 原文地址:https://www.cnblogs.com/zhouqinmei/p/10574958.html
Copyright © 2020-2023  润新知