• python-条件和循环


    if循环:有条件的执行,做出选择

    例1:

    1 a=42
    2 if a<=10:
    3    print('the number less than 10')
    4 else:
    5    print('thank you!')

    例2:    

    1 people='M'
    2 if people =='M':
    3    print('girl')#if下的缩进归if管
    4 else:
    5    print('boy')
    6 print('the end') #没有缩进不归if管
    [root@localhost ~]# python 1.py
    girl
    the end

    例3

    1 a=103
    2 if a>=100:
    3    print('the number is larger or equal to 100')
    4 else:
    5    print('the number is less than 100')    
    [root@localhost ~]# python 1.py
    the number is larger or equal to 100

     例4(if嵌套)

     1 a='F'
     2 b=30
     3 if a=='M':
     4    if b>=20:
     5       print('gentleman')
     6    else:
     7       print('boy')
     8 else:
     9    if b<20:
    10       print('girl')
    11    else:
    12       print('women')
    [root@localhost ~]# python 1.py
    women

    例5 分数分等级 0~60为no pass,60~70just pass,70~80good,80~90better,90~100best

    
    
     1 record=92
     2 if record >=60:
     3    if record>=70:
     4       if record>=80:
     5          if record>=90 and record<=100:
     6             print('best')
     7          else:
     8             print('better')
     9       else:
    10           print('good')
    11    else:
    12       print('just pass')
    13 else:
    14    print('no pass') 
    [root@localhost ~]# python 1.py
    best

    while循环:重复某个功能多次

    • 循环变量初始化(开始)——第一步
    • 循环条件(给出终止的条件)——第二步
    • 循环【可重复】语句()——第四步
    • 循环的修正(修正条件)——第三步

    例1

    1 greetings=1
    2 while greetings<=3:    #这里记住了是<=而不是<-,不要和R混淆了
    3    print('hello!'*greetings)
    4    greetings=greetings+1
    [root@localhost ~]# python 2.py
    hello!
    hello!hello!
    hello!hello!hello!

    例2:打印100到200间的偶数

     1 a=100
     2 while a<=200:
     3    if a%2 == 0:
     4       print(a)
     5    a=a+1
     6 ####方法二 7 a=100
     8 while a<=200:
     9    print(a)
    10    a=a+2

    例3:输出1-100间3的倍数

    1 a=1
    2 while a<=100:
    3     if a%3==0:
    4        print(a)
    5     a=a+1

    例4:求1到100的和

     1 a=1
     2 sum=0
     3 while a<=100:
     4     if a==100:
     5        print(sum)
     6     sum=sum+a
     7 a=a+1
     8 ########
     9 a=1
    10 sum=0
    11 while a<=100:
    12     a=a+1
    13     sum=sum+a
    14 print(sum)
    15 (正确的方法)###########
    16 a=1
    17 sum=0
    18 while a<=100:
    19     sum=sum+a
    20     a=a+1
    21 print(sum)
    22 ###########
    23 a=1
    24 sum=0
    25 while a<=100:
    26     sum=sum+a
    27     a=a+1
    28     print(sum)  #注意print的位置缩进

    例5:求1到100间的奇数和

    1 a=1
    2 sum=0
    3 while a<=100:
    4     if a%2==1:
    5        sum = sum+a
    6     a = a+1
    7 print(sum)
    [root@localhost ~]# python 1.py
    2500

                                    

    for循环:遍历列表中的每一个元素

    例1

    1 names=['a','b','c']
    2 for name in names:
    3   print('hello!'+name)
    [root@localhost ~]# python 1.py
    hello!a
    hello!b
    hello!c                                    

    例2

    1 n=10
    2 for i in range(n):
    3   print(i)

    题1:a=100,b=200,求a,b之间所有奇数的和

  • 相关阅读:
    source insight 4.0.086破解
    Java IO流关闭问题的深入研究
    iOS开发UI篇—UITableview控件使用小结
    iOS开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
    iOS开发UI篇—实现UItableview控件数据刷新
    iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
    iOS开发UI篇—推荐两个好用的Xcode插件(提供下载链接)
    iOS开发UI篇—UITableviewcell的性能优化和缓存机制
    iOS开发UI篇—UITableview控件基本使用
  • 原文地址:https://www.cnblogs.com/wujiadong2014/p/4915966.html
Copyright © 2020-2023  润新知