Python言語ベース2:プロセス制御文

30557 ワード

Python言語基礎2:フロー制御文学習ノート
Python                  。
        ,           ,               
           :      ,    

条件判定文(if文)
  1:
          ,           。                  。
#        
num = int(input('       '))
print('      :',num)
if num % 2 == 0 :
    print('       ')
else :
    print('       ')
  2:
          ,             。
             4     100  ,     400  ,        
#          
year = int(input('    :'))
print('     :',year)
if year % 4 ==0 and year % != 0 or year % 400 == 0:
    print('   ')
else :
    print('    ')
  3:
        5  ,5             ?
          ,              10.5 ,            。
      5               10.5+10.5+4+4+4 = 33 

          ,           ,                 。
            ,         
#       
dog_year = float(input('      :'))
print('        :',dog_year)
if dog_year < 1:
    print('         ')
elif dog_year <= 2 :
#    dog_year = dog_year * 10.5
    dog_year *=10.5
    print('     :',dog_year ,' ')
else:
    dog_year = 21 + (dog_year-2)*4
    print('     :',dog_year ,' ')

#     
dog_year = float(input('      :'))
like_person_year = 0
if dog_year >0 :
    if dog_year<=2 :
        like_person_year = dog_year*10.5
        print('     :',like_person_year)
    else:
        like_person_year = 2*10.5
        like_person_year +=(dog_year-2)*4
        print('     :',like_person_year)
else:
    print('     ')
  4:
                :
            100 ,'    BMW'
            [80-99] ,'    iphone'
            [60-79] ,'       '
           ,       
#       
score = int(input('      :'))
if score < 0 or score >100 :
    print('         ')
elif score == 100 :
    print('    BMW')
elif  score >=80 :
    print('    Honor')
elif score >= 60 :
    print('       ')
else:
    print('      ,      ')

#   :
score = int(input('      '))
if 0<=score<100 :
    if score == 100 :
        print('      ')
    elif score >= 80 :
        print('      ')
    elif score >=60 :
        print('      ')
    else :
        print('    ')
else:
    print('     ')

  5:
         ,    ,    。          ,          :
         :180cm  ;  :1000   ;  :500  ;
                   , :'       '
                    , :'  ,    ,    。'
                  , :'  !'
        (    ,        )
#    
print(' :180  , :1000   , :500  ')
height = int(input('    :'))
wealth = int(input('    :'))
loa = int(input('    :'))
print(' :%s , :%s, :%s ' % (height,wealth,loa))
if height>=180 and height>=1000 and loa>=500 :
    print('   ')
elif height>=180 or height>=1000 or loa>=500 :
    print('  ,        ')
else :
    print('      ')

ループステートメント
#      
#       ,               
i= 0
#      ,                
while i <  10:
    print (i)
#      ,          
    i +=1
else :
    print('else     ')
  1:
     100         
#  100        
i = 0
result = 0
while i < 100 :
    if i%2 !=0:
        result += i
    i +=1
print('100      :', result)
  2:
     100    7     ,    
#  100    7     ,    
i=7
count =0
result=0
while i < 100 :
    result += i
    count +=1
    i +=7
print(result,count)
  3:
             n   (n≥3 ),           n          (  :1**3 + 5**3 + 3**3 = 153)。
     1000         
#  1000       。
i=100
while i < 1000 :
    a = i//100
    b = i//10%10
    c = i%10
    if a**3+b**3+c**3 == i:
        print(a,b,c,i)
    i+=1  #      
  4:    
              ,        。      1        ,1         。
#          num   
num = int(input('        1   '))
i = 2
#       ,    num     ,     
flag = True
while i < num:
    #   num   i  
    #   num  i  ,   num    ,    flag   false
    if num % i ==0:
        #       ,   num    ,    flag   False
        flag = False
    i += 1

if flag :
    print(num,'   ')
else:
    print(num,'    ')

ループネスト
              
       ,       。
#        ,       。
#     
i = 0
while i < 5:
    j = 0
    while j < 5:
        print('* ',end='')
        j +=1
    print()
    i += 1

#      
i = 0
while i < 5:
    j = 0
    while j < i+1:
        print('* ' ,end='')
        j +=1
    print()
    i += 1

  1:
      99   
    1*1=1
    1*2=2 2*2=4
    1*3=3 2*3=6 3*3=9
    ...                 9*9=81
#   99   
i = 1
while i < 10:
    j = 1
    while j < i+1 :
        print(j,'*',i,'=',i*j ,end='')# end='':print       ,     。end=' '        ,   。
        j += 1
    print( ) #  
    i += 1

  2:
     100       
#  100      
#        , 1-100     
i = 2
while i <= 100:

    #      ,  i   ,  i   
    flag = True

    #   i     
    #         i   
    j = 2
    while j < i:
        #   i   j  
        if i % j == 0:
            # i  j  ,  i    ,  flag False
            flag = False
        j += 1
    #        
    if flag :
        print(i)
    i += 1

パフォーマンスの最適化
#     time  ,        
from time import*
begin = time()
i = 2
while i <= 10000:
    flag = True
    j = 2
    while j <= i**0.5: #     ,     j < i
        if i % j == 0:
            flag = False
            #       ,  i    ,  ,             
            #   break       ,        
            break #      ,   : break
        j += 1
    if flag :
        pass
    i += 1

end = time()
print(end - begin)  #